Python Forum
IRC bot written in Python - join channel function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IRC bot written in Python - join channel function
#1
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():
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.
Reply
#2
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...ith-socket
Recommended Tutorials:
Reply
#3
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
Reply
#4
do you mean?
bytes("JOIN %s".format(channel)
Recommended Tutorials:
Reply
#5
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.
Reply
#6
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:
Recommended Tutorials:
Reply
#7
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.
Reply
#8
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)))
Recommended Tutorials:
Reply
#9
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 !
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020