I had an urge to write a simple IRC chat bot with .NET core. Why? Just because I could. I wanted a quick project for the night, so I figured out how to make it happen.

This book covers the basics of TCP/IP Sockets.

Before you get started, you'll need to have the .NET core SDK installed on your computer. It will work with Windows, Linux, or macOS. I have only tested this IRC bot with Windows, but I didn't use any Windows-specific libraries, so it should cross-compile.

First, I created a folder on my computer called "irc-bot". Then, from within that folder, using Powershell (you can use the command prompt, too), I ran:

dotnet new console

Then, I typed:

dotnet add package System.Net.Sockets

This adds the NuGet package for the required TCP sockets that we'll need in order to communicate with the IRC server.

Here's the code for the IRC bot:

using System;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace irc_bot
{
    class Program
    {
        private const string server = "irc.freenode.net";
        private const string gecos = "CerkitBot v1.0 (cerkit.com)";
        private const string nick = "YourBotNick";
        private const string password = "YOUR_IRC_PASSWORD";
        private const string ident = "cerkitbot";
        private const string channel = "#CHANNEL_NAME";

        static void Main(string[] args)
        {
            using(var client = new TcpClient())
            {
                Console.WriteLine($"Connecting to {server}");
                client.Connect(server, 6667);
                Console.WriteLine($"Connected: {client.Connected}");

                using(var stream = client.GetStream())
                using(var writer = new StreamWriter(stream))
                using(var reader = new StreamReader(stream))
                {
                    writer.WriteLine($"USER {ident} * 8 {gecos}");
                    writer.WriteLine($"NICK {nick}");
                    // identify with the server so your bot can be an op on the channel
                    writer.WriteLine($"PRIVMSG NickServ :IDENTIFY {nick} {password}");
                    writer.Flush();

                    while(client.Connected)
                    {
                        var data = reader.ReadLine();

                        if(data != null)
                        {
                            var d = data.Split(' ');
                            Console.WriteLine($"Data: {data}");

                                if(d[0] == "PING")
                                {
                                    writer.WriteLine("PONG");
                                    writer.Flush();
                                }

                            if(d.Length > 1)
                            {          
                                
                                switch(d[1])
                                {
                                    case "376":
                                    case "422":
                                    {
                                        writer.WriteLine($"JOIN {channel}");
                                        
                                        // communicate with everyone on the channel as soon as the bot logs in
                                        writer.WriteLine($"PRIVMSG {channel} :Hello, World!");
                                        writer.Flush();
                                        break;
                                    }
                                    case "PRIVMSG":
                                    {
                                        if(d.Length > 2)
                                        {
                                            if(d[2] == nick)
                                            {
                                                // someone sent a private message to the bot
                                                var sender = data.Split('!')[0].Substring(1);
                                                var message = data.Split(':')[2];
                                                Console.WriteLine($"Message: {message}");
                                                // handle all your bot logic here
                                                writer.WriteLine($@"PRIVMSG {sender} :Hello, thank you for talking to me.");
                                                writer.Flush();
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

That's pretty much it. I saved it (using Visual Studio Code) and then ran the following command to build it for Windows 10 (64-bit):

dotnet build -r win10-x64

I learned about how to do this from here.

You can read a list of valid runtimes here.

CerkitBot logged into the #cerkit IRC channel
Message logs from the CerkitBot console
sending CerkitBot a private message
CerkitBot's response
Message as it appeared to CerkitBot. CerkitBot also logged the actual parsed message.

As you can see, getting a simple IRC chat bot up and running in .NET core is very easy to do. The hardest part was registering the Bot's nick with freenode and figuring out how to properly send the IDENTIFY message.

Then, it was just a matter of granting the correct privileges to the bot from my channel Founder account.