Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8148761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:42:34+00:00 2026-06-06T14:42:34+00:00

I have a python IRC bot that I’m starting to work on to practice

  • 0

I have a python IRC bot that I’m starting to work on to practice my python. However, for some servers, it won’t join a channel, though it logs on successfully. I also want it to react to user commands like !test, but I’m not sure how to do it. On the servers it works, it pings back successfully.

from socket import socket, AF_INET, SOCK_STREAM
network = raw_input("Server: ")
port = 6667
chan = raw_input("Channel: ")
nick = raw_input("Nick: ")
irc=socket(AF_INET,SOCK_STREAM)
irc.connect((network, port))
a=irc.recv (4096)
print a
irc.send('NICK ' + nick + '\r\n')
irc.send('USER john_bot john_bot bla :john_bot\r\n')
irc.send('JOIN :' + chan + '\r\n')
irc.send('PRIVMSG ' + chan + ' :Hello.\r\n')

def ircSend(msg):
     print(msg)
     irc.send(msg)

while True:
    data = irc.recv(4096)
    print data
    if data.find('PING') != -1:
       ircSend('PONG ' + data.split()[1] + '\r\n')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:42:35+00:00Added an answer on June 6, 2026 at 2:42 pm

    On some servers you have to respond to a PING with a PONG before you can actually do anything.

    ircSend('NICK ' + nick + '\r\n')
    ircSend('USER john_bot john_bot bla :john_bot\r\n')
    
    data = ircRecv() # explained later
    if data.find('PING') != -1:
        ircSend('PONG ' + data.split()[1] + '\r\n')
    
    ircSend('JOIN :' + chan + '\r\n')
    ircSend('PRIVMSG ' + chan + ' :Hello.\r\n')
    

    You don’t really need this

    a=irc.recv (4096)
    print a
    

    Sometimes the IRC server sends multiple lines at once (like MOTD or NAMES for example). This will handle it well as long as the total number of bytes does not exceed 4096 (some lines would split to two lines)

    data = irc.recv(4096)
    for line in data.split('\r\n'):
        # process the line
    

    If it is a problem that a line would cut in half (it can rarely be, like if a PING happens to be there), we can receive one line at time and leave the rest characters to socket’s buffer. However this may be a little less efficient (I haven’t tested it, so maybe it doesn’t matter at all)

    def ircRecv():
        line = ''
        while 1: # same as while True:
            character = irc.recv(1)
            if character == '\n':
                break # exit the loop
            elif character != '\r':
                line += character
        print line
        return line
    

    From page 8 of the IRC RFC:

    <message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
    <prefix>   ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
    <command>  ::= <letter> { <letter> } | <number> <number> <number>
    <SPACE>    ::= ' ' { ' ' }
    <params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
    <middle>   ::= <Any *non-empty* sequence of octets not including SPACE
               or NUL or CR or LF, the first of which may not be ':'>
    <trailing> ::= <Any, possibly *empty*, sequence of octets not including
                 NUL or CR or LF>
    <crlf>     ::= CR LF
    

    This means that you can always get of message’s sender easily and for sure like this:

    # put this inside the main loop
    # this will throw an IndexError when the connection is closed,
    # an empty line does not contain any spaces
    line = ircRecv()
    if line.split()[0].find('!') != -1:
        # the first character must be a colon because <command> can't include
        # an exclamation mark
        someOneElsesNick = line[1:line.find('!')]
        command = line.split()[1]
    

    Answer when someone greets!

        if command == 'PRIVMSG':
            destination = line.split()[2] # channel or bot's nick
            # <trailing>, in this case, the message
            message = line[line[1:].find(':')+2 : ] # everything after the 2nd colon
    
            # we add one since we don't want include the <trailing> colon in
            # the message and an other one because line[1:].find() is one smaller 
            # number than line.find() would be
    
            # if we receive a private message, we have to respond to the sender,
            # not to ourself
            if destination == nick:
                destination = someOneElsesNick
    
            if message.startswith('hi!'):
                ircSend('PRIVMSG ' + destination + ' :Hi, '
                + someOneElsesNick + '!\r\n')
    

    For more info, check out the IRC RFC: http://www.ietf.org/rfc/rfc1459.txt (especially sections 2.3.1 and 4). If you don’t want to deal with the IRC protocol, use Twisted 🙂 http://twistedmatrix.com/trac/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an IRC bot written in python that uses Twisted. It can print
For our company I'd like to have a Python based IRC bot which checks
So I'm starting to write a simple procedural Python IRC bot from scratch (i.e.
Currently I have a simple IRC bot written in python. Since I migrated it
I have been doing some work with python-couchdb and desktopcouch. In one of the
What I want to do is have a python script (that's a python IRC
I have Python nested list that I'm trying to organize and eventually count number
i have an issue i could use some help with, i have python list
I have used mechanize in Python with great success. However, I am trying to
I have a python script that is a http-server: http://paste2.org/p/89701 , when benchmarking it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.