IRC bot module.

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

IRC bot module.

Postby RainbowShibe » Wed Jan 01, 2014 8:23 pm

I will lable all of the strings you will need,i will the non-threaded version, i will also label weither they need to be a string or not. I will also include the help file fore the !help command

Nick = Your bots nick

Server = the server of wich it will be connected to

Channel = the channel it will be connected to (it will change when you change the channel too)

Password = the nicks password (you may have to edit this if the server uses something other than nickserv)

Owner = the owners nick

Connection = this has to be socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Deftimeout = the default timeout (aka socket.getdefaulttimeout())

Deftimeoutstr = the deftimeout as a string

Newtimeout = i usually set this as "A new string has not been set"

Host = your host, i never really knew what exactly to set this as, and i found it didnt matter much so i usually set it as "None" or "none" (has to be a string)

Here is the Non-threaded version:
The only function you will need from this is bot, so i would just do from botScript import bot

botScript.py:

Code: Select all
#!/usr/bin/python
import socket
import sys
import string
import random
import time
import os
def bot(nick, server, channel, password, owner, connection, deftimeout, deftimeoutstr, newtimeout, host):
   irc = connection
   open = os.open('/home/alucero565/MyFiles/python/bots/help', os.O_RDONLY)
   file = str(os.stat('/home/alucero565/MyFiles/python/bots/help').st_size)
   file = file.strip('L')
   file = int(file)
   file = os.read(open, file)
   help = str(file)
   connected = True
   irc.settimeout(360)
   while 1:
      recieve=irc.recv(1024)
      print(recieve)
      if recieve.find('PING ') !=-1:
         message2=recieve
         message1=recieve.split()[1]
         irc.send('PONG '+message1+'\r\n')
         print(message2)
         print("PONG ", recieve.split()[1])
      if recieve.find(':hello '+nick) !=-1:
         c=recieve.split(':hello '+nick)
         r=c[1].strip()
         irc.send('PRIVMSG '+channel+' :hello\r\n')
      if recieve.find(nick+' :!disconnect 1597 ') !=-1:
         c=recieve.split(' :!disconnect 1597 ')
         a=c[1].strip()
         irc.send('QUIT :'+a+'\r\n')
         logopen.close()
         break
      if recieve.find(':!say ') !=-1:
         c=recieve.split(':!say ')
         a=c[1].strip()
         irc.send('PRIVMSG '+channel+' :'+a+'\r\n')
      if recieve.find(nick+' :!join ') !=-1:
         c=recieve.split(nick+' :!join ')
         a=c[1].strip()
         channel=str(a)
         irc.send(' JOIN '+a+'\r\n')
      if recieve.find(':!part ') !=-1:
         c=recieve.split(':!part ')
         a=c[1].strip()
         irc.send('PART '+a+'\r\n')
      if recieve.find(':!help') !=-1:
         irc.send('PRIVMSG '+channel+help)
      if recieve.find(':!nick '+nick+' ') !=-1:
         c=recieve.split(':!nick '+nick+' ')
         a=c[1].strip()
         nick=str(a)
         irc.send('NICK '+a+'\n')
      if recieve.find(':!me ') !=-1:
         c=recieve.split(':!me ')
         a=c[1].strip()
         irc.send('PRIVMSG '+channel+' :\001ACTION '+a+'\001\r\n')
      if recieve.find(':!deftimeout.read') !=-1:
         irc.send('PRIVMSG '+channel+' :'+deftimeoutstr+'\r\n')
      if recieve.find(':!deftimeout.set ') !=-1:
         c=recieve.split(':!deftimeout.set ')
         a=c[1].strip()
         if a!='None':
            if a!='none':
               t=float(c[1].strip())
               socket.setdefaulttimeout(t)
               deftimeout=(t)
               irc.send('PRIVMSG '+channel+' :The new default timeout is: '+str(t)+'\r\n')
               print('default timeout: '+str(t))
               deftimeoutstr=str(t)
         elif a=='None':
            irc.send('PRIVMSG '+channel+' :Cannot set to None\r\n')
         elif a=='none':
            irc.send('PRIVMSG '+channel+' :Cannot set to none\r\n')
      if recieve.find(':!deftimeout.reset') !=-1:
         socket.setdefaulttimeout(None)
         irc.send('PRIVMSG '+channel+' :Default Timeout: None\r\n')
      if recieve.find(':!commands.deftimeout') !=-1:
         irc.send('PRIVMSG '+channel+' :!deftimeout::   !deftimeout.read: reads the current default timeout   !deftimeout.set: sets a new default timeout (only use numbers)   !deftimeout.reset: resets the default timeout to infinite (aka None)\r\n')
      if recieve.find(channel) !=-1:
         value=random.randrange(1, 100)
         if value==50:
            irc.send('PRIVMSG '+channel+' :hello\r\n')
      if recieve.find(':!roll') !=-1:
         c=recieve.split(':!roll')
         a=c[1].strip()
         if a.find('.sides') !=-1:
            sides=a.split('.sides')
            amountofsides=float(sides[1].strip())
            roll=str(random.randrange(1, amountofsides))
            irc.send('PRIVMSG '+channel+' :'+roll+'\r\n')
         if a.find('.reg') !=-1:
            roll=str(random.randrange(1, 6))
            irc.send('PRIVMSG '+channel+' :'+roll+'\r\n')
      if recieve.find(':!identify ') !=-1:
         c=recieve.split(':!identify ')
         r=c[1].strip()
         irc.send('PRIVMSG NickServ :IDENTIFY '+r+'\r\n')
      if recieve.find(':!reset') !=-1:
         irc.send('PRIVMSG '+channel+' :this command is still in beta\r\n')


And the help file: (it isnt code but ill put a code tag in anyways)

Code: Select all
:Commands::   !help: displays this   !join: joins a channel   hello '+nick+': '+nick+' will say hello back   !disconnect: disconnects from the server, and can only be accessed by pming   !nick: changes nick   !me: the me command   !deftimeout (do !commands.deftimeout for more info)   !say: sais what you want it to   !part: parts the channel specified   !roll: rolls the dice\r\n


The threaded version does not work completely yet, i cannot figure out how to get the command checking part to start, although I will update it when it works.
MLP:FIM <3
Image
User avatar
RainbowShibe
 
Posts: 10
Joined: Wed Jan 01, 2014 12:39 am
Location: Somewhere Over The Rainbow

Re: IRC bot module.

Postby metulburr » Thu Jan 02, 2014 12:07 am

it would be better suited as a class instead ofa function with a list of if's
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1490
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 2 guests