Python Forum

Full Version: Trying to figure out Python IRC
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys. I'm obviously quite new here. I'm currently making a games and trivia-like bot for a friend's irc channel and I'm kind of stuck on something I'm trying to have it do.

Simply put I'd like to have the bot respond to a person when they say "hellO <botnick>". I have had no problem with this so far. Then, what I would like is for that to trigger another word that if entered the bot would respond to it as well. I tried to set it up so that if someone said hello it would increase a variable from 0 to 1 and if that variable is above 0 the bot would respond to a second word.

Basically the idea is:

Me: Hello <bot>
Bot: Hello, Tass. Care to play with me?
(increase variable counter from 0 to 1)
Me: Play
(check if variable is 1. If so)
Bot: Let's go on an adventure together!
(decrease variable back to 0)
If I say "play" while the variable is 0 it just proceeds with the rest of the script.

My current code is to follow:

def main():
  joinchan(channel)
  ircsock.send(bytes("PRIVMSG "+ "CHANSERV :op "+ channel +" "+ botnick +"\n", "UTF-8"))
  while 1:
    ircmsg = ircsock.recv(2048).decode("UTF-8")
    ircmsg = ircmsg.strip('\n\r')
    print(ircmsg)
    if ircmsg.find("PRIVMSG") != -1:
      name = ircmsg.split('!',1)[0][1:]
      message = ircmsg.split('PRIVMSG',1)[1].split(':',1)[1]
      if len(name) < 17:
        if message.rstrip() == "!np":
          sendmsg(np)
        if message.rstrip() == "!bot":
          sendmsg(botinfo)
        if message.find("hello "+ botnick) != -1:
          counter = 1
          sendmsg("こにちは "+ name +", " + "have you come to play with me?")
        ##########################################################
        # The following two lines are what I'm trying to work with
        if message.rstrip() == "play":
          sendmsg("Let's go on an adventure ... together, 先輩!")
        if message[:5].find('.tell') != -1:
          target = message.split(' ', 1)[1]
          if target.find(' ') != -1:
            message = target.split(' ', 1)[1]
            target = target.split(' ')[0]
          else:
            target = name
            message = "Could not parse. The message should be in the format of ‘.tell [target] [message]’ to work properly."
          sendmsg(message, target)
        if name.rstrip() == name in admins and message.rstrip() == "!op":
          ircsock.send(bytes("MODE "+ channel +" +o "+ name +"\n", "UTF-8"))
        if name.lower() == adminname.lower() and message.rstrip() == exitcode:
          sendmsg("Sayonara, "+ adminname +"-san")
          ircsock.send(bytes("QUIT \n", "UTF-8"))
          return
    else:
      if ircmsg.find("PING :") != -1:
        ping()
main()
Everything in the coding seems to work fine and without error. The section I have bolded is the code I'm trying to have only active if the previous condition was met.

Thanks in advance to any help or to anyone who can kinda kick my ass towards the right direction.