Writing a simple IRC bot in C#, won't connect to a channel

jefe323

Distinguished
Feb 14, 2010
89
0
18,590
3
I've written an IRCBot class in C# and it connects fine to the server, but it fails to join a channel. Can anyone spot whats wrong (i've edited out the channel and the password for the user)

[cpp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace CronusV3
{
class IRCBot
{
TcpClient IRCConnection = null;
IRCConfig config;
NetworkStream ns = null;
StreamReader sr = null;
StreamWriter sw = null;

public IRCBot(IRCConfig config)
{
this.config = config;
try
{
IRCConnection = new TcpClient(config.server, config.port);
IRCConnection.ReceiveBufferSize = 1024;
}
catch
{
Console.WriteLine("Communication Error!");
}
try
{
ns = IRCConnection.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns) { NewLine = "\r\n", AutoFlush = true };

sendData("PASS", "******");
sendData("NICK", config.nick);
sendData("USER", config.nick + " irc.esper.net " + " irc.esper.net" + " :" + config.name);
sendData("JOIN", "#*****");

IRCWork();
}
catch
{
Console.WriteLine("Communitaction Error!");
}
finally
{
if (sr != null)
sr.Close();
if (sw != null)
sw.Close();
if (ns != null)
ns.Close();
if (IRCConnection != null)
IRCConnection.Close();
}
}

public void sendData(string cmd, string param)
{
if (param == null)
{
sw.WriteLine(cmd);
sw.Flush();
Console.WriteLine(cmd);
}
else
{
sw.WriteLine(cmd + " " + param);
sw.Flush();
Console.WriteLine(cmd + " " + param);
}
}

public void IRCWork()
{
string[] ex;
string data;
bool shouldRun = true;
while (shouldRun)
{
data = sr.ReadLine();
Console.WriteLine(data);
char[] charSeparator = new char[] { ' ' };
ex = data.Split(charSeparator, 5);

if (ex[0] == "PING")
{
sendData("PONG", ex[1]);
}

if (ex.Length > 4) //is the command received long enough to be a bot command?
{
string command = ex[3]; //grab the command sent

switch (command)
{
case ":!join":
sendData("JOIN", ex[4]); //if the command is !join send the "JOIN" command to the server with the parameters sent my the user
break;
case ":!say":
sendData("PRIVMSG", ex[2] + " " + ex[4]); //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4])
break;
case ":!quit":
sendData("QUIT", ex[4]); //if the command is quit, send the QUIT command to the server with a quit message
shouldRun = false;
break;
}
}

if (ex.Length > 3)
{
string command = ex[3];

switch (command)
{
case ":!part":
sendData("PART", ex[2]);
break;
}
}
}
}
}
}
[/cpp]
 
Thread starter Similar threads Forum Replies Date
M Apps General Discussion 5
M Apps General Discussion 2
H Apps General Discussion 3
A Apps General Discussion 1
Y Apps General Discussion 1
L Apps General Discussion 5
O Apps General Discussion 1
V Apps General Discussion 1
M Apps General Discussion 9
S Apps General Discussion 2
A Apps General Discussion 1
P Apps General Discussion 1
L Apps General Discussion 3
A Apps General Discussion 1
T Apps General Discussion 1
Y Apps General Discussion 1
I Apps General Discussion 2
C Apps General Discussion 3
K Apps General Discussion 4
A Apps General Discussion 6
Similar threads
Solved! re-write certain part of .mp4 file using ffmpeg
Write a menu driven program which has following options 1. Factorial 2. Prime or not 3. Odd or even. 4. Exit. Once the menu
Write a program in C++ using classes and function overloading, to calculate volume of Cube, Cuboid and a Cylinder, the program
Write a C program that computes and prints out an approximate number of primes not exceeding a given number, x .
Write a program to calculate commission for the input valu of sales amount
Could you please help me write a cpp code for an assignment I have in school for juice
how to write a calculator program that will ask the user if he/she wann continue using loop
Write basic statement of To add values of X and Y in Z
Does anyone know if there is software to write mkv files to an UHD blu ray?
online4. Write the program to declare Single dimensional array that stores (5) integer values and perform following operations
Write a program for a multiple choice survey.
1st question : 210 points. Write a program to simulate a keyboard with given keys and their operation. You need to print the f
I turned in an assignment late because MyWritingLab took awhile to process my temp access code
palindrome is a word, phrase, or sequence that reads the same backwards as forwards. Given a palindrome write a program to pri
Write a C++ program to help the Administration of a football league to manipulate the list of players registered in different
write a program to create a frame by creating an object to the subclass of an frame class
1. Write a program that outputs the greatest common divisor of two positive integers. For example, if you input two positive
How to remove write protect from photos already on computer
AutoHotKey Script writing for n00bs.
3. Write a C# application that asks the user to input ‘A’ and ‘B’ and prints the sum of squares of all even numbers be

ASK THE COMMUNITY

TRENDING THREADS