Skip to content

Instantly share code, notes, and snippets.

@lahwran
Forked from MooseElkingtons/Bot.py
Last active December 12, 2015 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lahwran/4695802 to your computer and use it in GitHub Desktop.
Save lahwran/4695802 to your computer and use it in GitHub Desktop.
__author__ = "moose"
import Logger
import socket
class Bot(object):
logger = None
config = None
irc = None
readbuffer = ""
server = "irc.freenode.net"
port = 6667
name = "Pycicle"
channels = "#Pycicle"
server_pwd = None
use_pwd = False
def __init__(self, config):
self.logger = Logger("../logs/logs.dat")
self.config = config
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self):
self.reload_config()
self.irc.connect(self.server, self.port)
self.logger.log('[CONNECT] Connecting to ',self.server,':',self.port)
if self.use_pwd:
self.send_raw('QUOTE PASS ',self.server_pwd)
self.logger.log('[CONNECT] Setting Nick to ',self.name)
self.send_raw('NICK ',self.name)
def disconnect(self):
self.logger.log('[DISCONNECT] Disconnected from ',self.server,':',self.port)
self.irc.close()
def reload_config(self):
self.server = self.config['irc-server']
self.port = int(self.config['irc-port'])
self.name = self.config['bot-name']
self.server_pwd = self.config['server-pwd']
self.channels = self.config['auto-join']
self.use_pwd = self.server_pwd == None
def run(self):
while True:
self.readbuffer = self.readbuffer + self.irc.recv(1024)
temp = self.readbuffer.split("\n")
for l in temp:
l = l.rstrip().split()
if l[0] == 'PING':
self.on_pong(l[1:])
elif l[0].startswith(':') and l[1] == 'PRIVMSG':
pms = l.join()
pmdata, pm = pms.split(":", 2)
pmsender, pmrec = (pmdata[:1] + pmdata[2:]).split(' ', 2)
self.on_message(pmsender, pmrec, pm)
elif l[0] == 'NOTICE':
self.on_notice(l[1:])
elif l[0] == 'KICK':
ks = l[1:].join()
kchan, krec = ks[:2].split(' ', 2)
kreason = ks[2:]
self.on_kick(kchan, krec, kreason)
elif l[0].startswith(':') and l[1] == 'JOIN':
juser = l[:1].replace(':', '')
jchan = l[2:].replace(':', '')
self.on_join(juser, jchan)
elif l[0].startswith(':') and l[1] == 'PART':
puser = l[:1].replace(':', '')
pchan = l[2:].replace(':', '')
self.on_part(puser, pchan)
elif l[0].startswith('MODE'):
mdata = l[:2]
mchan = l[1]
self.on_mode(mdata, mchan)
def on_mode(self, data, channel):
self.logger.log('[MODE ',channel,'] set mode ',data)
def on_join(self, user, channel):
self.logger.log('[JOIN ',channel,'] ',user)
def on_kick(self, channel, recipient, reason):
self.logger.log('[KICK ',channel,'] Kicked ',recipient,' for: ',reason)
def on_message(self, sender, recipient, message):
self.logger.log('[PRIVMSG ',sender,' -> ',recipient,'] '+message)
def on_part(self, user, channel):
self.logger.log('[PART ',channel,'] ',user)
def on_pong(self, l):
self.send_raw('PONG ',l[0])
def send_raw(self, raw):
self.irc.send(raw,'\r\n')
def send_message(self, message, channel):
self.send_raw('PRIVMSG ',channel,' :',self.parse_colors(message))
def parse_controls(self, message):
nm = message.replace('^c', '\x03')
nm = nm.replace('^b', '\x02')
return nm
def strip_controls(self, message):
nm = message.replace('\x03', '')
nm.replace('\x02', '')
__author__ = "Moose Elkingtons"
import os
class IRCConfiguration(object):
def __init__(self, directory):
self.settings = {}
try:
dir = directory.replace('%user.home%', os.path)
self.file = open(dir)
except IOError as e:
print 'Invalid or Corrupt File: ', dir
print e
s = self.file.readline()
while s != None:
if(not s.startswith('#')):
split = s.split('=')
self.settings[split[0]] = split[1]
def __getitem__(self, key):
return self.settings[key]
def __setitem__(self, key, value):
self.settings[key] = value
'''
@author: Moose Elkingtons
'''
import bot
import irc_configuration
class PyBot(bot.Bot):
command_prefix = '!'
game_running = False
game_status = False
# Game Variables
czar = "NoOne"
players = {}
white_cards = {}
def __init__(self):
config = irc_configuration.IRCConfiguration('../settings.cfg')
self.command_prefix = config['command-prefix']
self = Bot(config)
self.connect()
def on_message(self, sender, recipient, message):
command = message[0].lower().replace(self.command_prefix, '', 1)
if command == 'join':
if not self.game_status:
self.send_message(recipient, sender,': There is no active game at the moment! ',
'Type ',self.command_prefix,'start to start a game!')
else:
if not self.game_running:
self.send_message(recipient, '^b',sender,'^b has started a new game of ',
'Cards Against Humanity - A Game for Horrible People!')
self.send_message(recipient, 'If you do not know how to play, type ',
'^b^c04',self.command_prefix,'howto^b^c to know the rules',
' of Cards Against Humanity.')
if __name__ == "__main__":
bot = PyBot()
bot.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment