Download ArticleDownload Article

If you've been on Internet Relay Chat (IRC), chances are you've encountered a bot at some point. Bots are independent programs or scripts that connect to a network in the same way that a human would. They can be programmed to respond to user commands or even chat. In this guide, you can find what your options are for building an IRC bot as well as how to build one from scratch.

Method 1
Method 1 of 2:

Evaluating Options

Download Article
  1. How.com.vn English: Step 1 Consider installing a client script.
    Sometimes you just want a simple task done and don't want it to be an independent program. In that case, you can attach a script to an IRC client. This is pretty common to do with mIRC, which has a robust scripting engine and a wide variety of available scripts. This is the easiest option and highly recommended if you don't have much or any programming experience. For the rest of this guide, the instructions require some knowledge of computer programming to follow.
  2. How.com.vn English: Step 2 Consider a pre-existing codebase for your bot.
    There exist many open source and free programs that can help you set up your own customized bot quickly. One such example is Eggdrop, the oldest IRC bot still being maintained.
    Advertisement
  3. How.com.vn English: Step 3 Consider writing your own bot.
    For advanced IRC users and developers who already know their way around a programming language, this is a great option. You can use pretty much any language you want as long as it has socket support, but popular ones to use include Python, Lua, PHP, C, and Perl. If you don't know any of these but you do know another language, that's not a problem. You can usually find examples on the web in any language you want. For this article, we'll demonstrate using PHP. To use PHP, you'll need to have PHP-CLI installed on your computer or server.
    • PHP can be downloaded from php.net
    • PHP scripts can be executed from the command line. For additional information and help using PHP, see this PHP manual page.
  4. Advertisement
Method 2
Method 2 of 2:

Developing Your Own Bot

Download Article
  1. How.com.vn English: Step 1 Gather the connection details.
    You'll need to get the following information in order to connect successfully to the network.
    • Server: The domain name of the server used to connect to IRC, such as chat.freenode.net
    • Port: In most cases, this is 6667, but if you're not sure, check your own IRC client or the network's website.
    • Nickname: The nickname your bot should use. Keep in mind some special characters are usually not allowed (@#!~).
    • Ident: The ident field appears after the nickname when someone performs a WHOIS like this: nickname!ident@hostname
    • GECOS: This field typically holds a user's real name or a general description of the bot but you can put whatever you want in there.
    • Channel: You usually want your bot to be present in one or more channels. On most networks, these are prefixed with '#' but it might be something else.
  2. How.com.vn English: Step 2 Initialize the configuration in your script.
    The most basic way to do this is by naming a few variables according to the configuration names above. You could also store them in a config file and parse them out, but for right now we'll just stick with the absolute necessities.
  3. How.com.vn English: Step 3 Connect to the network.
    To do this, you'll need to open a socket to the server on the specified port. You should also add some error handling code in this part in case the connection fails for whatever reason. In this case, PHP provides us some neat functions to handle the error effectively.
  4. How.com.vn English: Step 4 Register your bot.
    This means supplying your nickname, ident, and GECOS to the server, not registering with NickServ. To do this, just write the NICK and USER commands to the server, followed by a carriage return and newline. It is imperative that you do it exactly as shown, because that is how it is specified in RFC1459, the specification for the IRC protocol. [1]
    • Note that the middle two parameters (in this case, * and 8) must be specified, but they are ignored by the server. Those two are only used between linked servers, not by a directly connecting client.
  5. How.com.vn English: Step 5 Keep fetching data from the socket using a loop.
    If you didn't use a loop, your script would immediately terminate and the bot would be basically useless. In order to stay connected, you have to fetch data from the server, check for any input in the stream you want, and respond to it if so. Here, we're grabbing data using socket_read() to grab any data available for us. If there is, we keep on doing whatever's in the loop. It might be helpful to also output the raw data to the console so you can see what's going on from the bot's perspective.
  6. How.com.vn English: Step 6 Write a ping handler.
    This is important. If you don't respond to pings in a timely manner, the server will disconnect you. Let's take care of that first. Pings look like this when sent from the server: PING :rajaniemi.freenode.net. The server doesn't have to put its name after the ':', it can supply whatever it wants. You *must* repeat back exactly what the server said, except using PONG.
  7. How.com.vn English: Step 7 Join your channels.
    Okay, so we have a bot that connects to the network and responds to pings, but otherwise does nothing. For people to see and use your bot, it should be in a channel (otherwise you'd have to tell it to respond to private messages).
    • To do this, we'll check for server status codes 376 or 422. 376 means the MOTD (message of the day) finished. 422 means there wasn't any MOTD to send. That MOTD is just something the server sends when you connect, but it is a good indicator for when we can start joining channels.
    • You'll need to issue a JOIN command. This command can be followed by one or more channels separated by a comma.
    • Notice that the data the server sends is conveniently delimited by spaces. This way we can split the data and reference it using an array index.
  8. How.com.vn English: Step 8 Respond to channel messages.
    Now for the fun part. Your bot's joined the channel, so now you can use it for what you wanted to. Let's create an example command called @moo.
    • Note the offset where messages begin (this applies to both channels and private messages). It is always in the same place.
    • You can handle commands with spaces in them by splicing the chunked data back together ($d). That's beyond the scope of this article.
    • If the target is a channel (such as #botters-test), then you reply to that. If it's a private message, this bit will be your bot's nickname! You must then reply using the sender's nickname, not yours (otherwise you'd be talking to yourself, and that's just silly).
  9. How.com.vn English: Step 9 Extend your bot.
    You can add many new features using the above implementation. There are many other commands that can be issued to the IRC network, such as managing ops, kicking and banning, setting the topic, among many other features.
  10. Advertisement

Community Q&A

Search
Add New Question
  • Question
    After I add them in notepad+, where do I add these codes?
    How.com.vn English: flying 8lack
    flying 8lack
    Community Answer
    Put them on a web server (such as Apache) which supports PHP. You can install XXAMP on your local computer, or host them on anther web server.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      • You can produce the result of the "/me" command by prefixing your messages like this:
        • PRIVMSG #channel :\001ACTION text here\001.
        • \001 means ASCII character 1 and will be interpreted as such in a double quoted PHP string. Alternatively, you can use chr(1) outside of the string.
      • Colors can be produced in a message by prefixing "\003" (ASCII code 3) followed by a number for a color. 0 = white, 1 = black, 2 = blue, 3 = green, 4 = red [...]. See mIRC's page for more colors.
      • As a matter of courtesy, get consent of channel owners and IRC operators before bringing your bot online. Not all networks and channels have a welcome policy toward bots, even well-behaved ones.
      Show More Tips
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 39 people, some anonymous, worked to edit and improve it over time. This article has been viewed 222,516 times.
      How helpful is this?
      Co-authors: 39
      Updated: September 8, 2022
      Views: 222,516
      Categories: Internet
      Thanks to all authors for creating a page that has been read 222,516 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement