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


Messages In This Thread
IRC bot written in Python - join channel function - by syrius - Sep-12-2017, 08:12 PM

Forum Jump:

User Panel Messages

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