IRC bot written in Python - join channel function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: IRC bot written in Python - join channel function (/thread-4917.html) |
IRC bot written in Python - join channel function - syrius - Sep-12-2017 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: 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()Of course the following code is incorrect : line23: Quote:def join(): line56: Quote:if name.lower() == master.lower() and ircmsg.find(":join %s") != -1: I would like to know what should I put there so the bot can join the channel. Any help would be very appreciated. RE: IRC bot written in Python - join channel function - metulburr - Sep-12-2017 Quote:if name.lower() == master.lower() and ircmsg.find(":join %s") != -1:What are you expecting this condition to be resulted to? Here is an example IRC bot https://python-forum.io/Thread-Basic-IRC-bot-with-socket RE: IRC bot written in Python - join channel function - syrius - Sep-12-2017 Hi metulburr, thank you for your quick reply. I would like that this condition would result the bot to join the IRC #channel-name when I type on IRC 'join #channel-name'. Thanks RE: IRC bot written in Python - join channel function - metulburr - Sep-12-2017 do you mean? bytes("JOIN %s".format(channel) RE: IRC bot written in Python - join channel function - syrius - Sep-13-2017 I've modified: ircsock.send(bytes("JOIN %s")) For: ircsock.send(bytes("JOIN %s".format(chan))) but still not working when I'm typing on IRC: join #channel-name Thanks. RE: IRC bot written in Python - join channel function - metulburr - Sep-13-2017 if you print out each of these conditions, are they what you expect them to be? Quote:if name.lower() == master.lower() and ircmsg.find(":join %s") != -1: RE: IRC bot written in Python - join channel function - syrius - Sep-14-2017 Hi metulburr, What you mean by "print out" ? I believe that there is an error in this line, the syntax must be wrong. Trying to figure out how to write this condition in order that when I type on IRC "join #channel-name" my bot will join the IRC channel. Thanks. RE: IRC bot written in Python - join channel function - metulburr - Sep-14-2017 replace bytes with str.encode def joinchan(channel): ircsock.send(str.encode("JOIN "+ channel + "\n")) def join(): ircsock.send(str.encode("JOIN %s"))It would also be better to replace all your string concatenations with format method such as def joinchan(channel): ircsock.send(str.encode("JOIN {}\n".format(channel))) RE: IRC bot written in Python - join channel function - syrius - Sep-14-2017 The variable 'joinchan' is for the bot joining the main channel when the bot is connected to the IRC server. I've tried it anyway with your suggestion but no luck yet :( I've tried it also with the variable 'join'. Should this line be modified : if name.lower() == master.lower() and ircmsg.find(":join %s") != -1: I think the syntax it's incorrect but not sure on how to write it exactly. Thanks for your help ! |