IRC,Greetbot source(python)

4 replies [Last post]
Shinobi
Offline
SX Crew
Joined: 2013/08/21

Here is a very simple Welcome Bot, I designed for our channel. It greets new members. The reason it was made was, because we constantly had bots joining. When you spoke to them more than once, they would quit. So I developed this welcome bot to not only kick the bots, but greet new members. It also informs them about the channel. I am now releasing the source. I also added in an exclude list for people, who did not want to be messaged every time they entered. It also saves the list. The bot was coded the day it was needed. Not much time was spent on it. I decided to release it for new programmers in python who wish to learn about IRC, sockets, parsing, and working with open/saves files. It will work on Linux and Windows. It is not OS dependent. Feel free to edit, modify it, build upon it or claim it as your own. Any questions feel free to join our IRC server and join the channel #soldierx to ask me any questions.

import socket
import time

Server = 'irc.soldierx.com'
Nick = 'WBSX'
Channel = '#soldierx'
Name = 'Welcome'
User ='greet'
IgnoreMsg = 'You are now being ignored.'
ExcludeList=['RaT','lattera','Shinobi']

File1 = open('ExcludeList.txt', 'r')
ExcludeList = File1.read().splitlines()
File1.close()
IRCSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IRCSock.connect((Server, 6667))

IRCSock.send('USER  Welcome Welcome Welcome :' + str(Name) +'\r\n')
IRCSock.send('NICK '+ str(Nick) +'\r\n')

while true:
    time.sleep(0.5)
    Data = IRCSock.recv(9000)

    if Data.find('End of /MOTD command') != -1:
        IRCSock.send('JOIN '+ str(Channel) +'\r\n')

    if Data.find ( 'PING' ) != -1:
        IRCSock.send ( 'PONG ' + Data.split() [ 1 ] + '\r\n' )

    if Data.find ('.ignore') != -1:
        WhoSaid = Data.split('!')[]
        WhoSaid = WhoSaid.replace(':', ' ')
        WhoSaid = WhoSaid.replace(' ', '')
        WhoSaid = WhoSaid.strip(' \t\n\r')
        if WhoSaid in ExcludeList:
            pass
        else:
             ExcludeList.insert(,WhoSaid)
             File1 = open("ExcludeList.txt", "w")
             for Who in ExcludeList:
                 File1.write(Who+'\n')
             File1.close()
             print ExcludeList
             IRCSock.send('PRIVMSG ' + WhoSaid + ' :' + str(IgnoreMsg) + '\r\n')

    if Data.find ('JOIN :'+ Channel +'') != -1:
        WhoSaid = Data.split('!')[]
        WhoSaid = WhoSaid.replace(':', ' ')
        WhoSaid = WhoSaid.replace(' ', '')
        WhoSaid = WhoSaid.strip(' \t\n\r')
        if WhoSaid in ExcludeList:
            pass
        else:
            Msg1 ='Hello ' + WhoSaid + ' and  welcome to the Official SoldierX Channel.'
            Msg2 ='Please be patient ' + WhoSaid + ' our channel can be slow at times.'
            Msg3 ='To Remove yourself, from being welcomed type .ignore in the channel.'
            IRCSock.send('PRIVMSG ' + WhoSaid + ' :' + Msg1 +  '\r\n')
            IRCSock.send('PRIVMSG ' + WhoSaid + ' :' + Msg2 +  '\r\n')
            IRCSock.send('PRIVMSG ' + WhoSaid + ' :' + Msg3 +  '\r\n')