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

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]
3 answers Last reply
More about writing simple connect channel
  1. nvm, got it working
  2. can you please post your working code? i can't make it work
    thanks in advance
  3. jefe323 said:
    nvm, got it working



    can you please post your working code? i can't make it work
    thanks in advance
Ask a new question

Read More

Programming C# Connection Configuration Apps