I'm trying to add to my python IRC bot a function that when I type "join #channel-name" on IRC, the bot will join the channel.
Here's my code:
Of course the following code is incorrect :
line23:
line56:
I would like to know what should I put there so the bot can join the channel.
Any help would be very appreciated.
Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import socket server = "irc.freenode.net" # IRC server channel = "#syrius-test" # Channel botnick = "syrius-bot" # Nickname of the bot master = "syrius_" # Nickname of the bot's master exitcode = "bye " + botnick #Text that we will use to make the bot quit def ircwrite(message): global ircsock ircsock.send( str (message).encode( 'latin-1' , 'ignore' )) def ping(): ircwrite( "PONG :pingis\n" ) def sendmsg(chan , msg): ircwrite( "PRIVMSG " + chan + " :" + msg + "\n" ) def joinchan(channel): ircsock.send(bytes( "JOIN " + channel + "\n" )) def join(): ircsock.send(bytes( "JOIN %s" )) def hello(): ircwrite( "PRIVMSG " + channel + " :Hello!\n" ) def quitting(): ircwrite( "PRIVMSG " + channel + " :Okay boss, leaving now.\n" ) ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ircsock.connect((server, 6667 )) ircwrite( "USER " + botnick + " " + botnick + " " + botnick + " :IRC bot coded by syrius.\n" ) ircwrite( "NICK " + botnick + "\n" ) joinchan(channel) while 1 : ircmsg = ircsock.recv( 2048 ).decode() # receive data from the server ircmsg = ircmsg.strip( '\n\r' ) # removing any unnecessary linebreaks. print (ircmsg) # Here we print what's coming from the server name = ircmsg.split( '!' , 1 )[ 0 ][ 1 :] # We split out the name if ircmsg.find( ":Hello " + botnick) ! = - 1 : # If we can find "Hello Mybot" it will call the function hello() hello() if ircmsg.find( "PING :" ) ! = - 1 : # if the server pings us then we've got to respond! ping() if name.lower() = = master.lower() and ircmsg.find( ":quit " + botnick) ! = - 1 : quitting() ircsock.send(bytes( "QUIT \n" , "UTF-8" )) if name.lower() = = master.lower() and ircmsg.find( ":join %s" ) ! = - 1 : join() main() |
line23:
Quote:def join():
ircsock.send(bytes("JOIN %s"))
line56:
Quote:if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:
join()
I would like to know what should I put there so the bot can join the channel.
Any help would be very appreciated.