tea_cah/botcmd.py

131 lines
3.3 KiB
Python

import threading
import time
import channel
import gameloop
nickserv_pass = None
irc_chan = None
game_channel = None
class GameLoop(threading.Thread):
def __init__(self, irc, chan, irc_chan):
self.irc = irc
self.chan = chan
self.irc_chan = irc_chan
threading.Thread.__init__(self)
def send(self, message):
self.irc.bot_response(self.irc_chan, message)
def notice(self, recipient, message):
self.irc.send_raw(b'NOTICE %s :%s' % (recipient.encode(), message.encode()))
def get_event(self):
event = self.chan.recv()
print(event)#debg
return event
def run(self):
try:
gameloop.game(self.send, self.notice, self.get_event)
except Exception as err:
self.send('Crash! (%s, %s)' % (type(err), repr(err)))
raise err #debg
finally:
self.chan.close()
def start_gameloop(irc):
global game_channel, irc_chan
if game_channel is not None:
return
chan = channel.Channel()
GameLoop(irc, chan, irc_chan).start()
game_channel = chan
def stop_gameloop():
global game_channel
if game_channel is None:
return
game_channel.send((gameloop.events.quit,))
game_channel = None
def send_event(event):
global game_channel
game_channel.send(event)
def parse_command(message, nick):
events = gameloop.events
if message == '!start':
send_event((events.start, nick))
elif message == '!kill':
send_event((events.kill,))
# initialize(*, config)
# Called to initialize the IRC bot
# Runs before even logger is brought up, and blocks further bringup until it's done
# config is a configpatser.ConfigParser object containig contents of bot.conf
def initialize(*, config):
global nickserv_pass, irc_chan
nickserv_pass = config['nickserv']['password']
irc_chan = config['server']['channels'].split()[0].encode()
# on_connect(*, irc)
# Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else
# Called for every reconnect
# Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object
def on_connect(*, irc):
global nickserv_pass
"""#debg
irc.msg(b'nickserv', b'IDENTIFY ' + nickserv_pass.encode())
time.sleep(20) # One day I will do this correctly. Today is not the day
"""#debg
stop_gameloop()
start_gameloop(irc)
# on_quit(*, irc)
# Called just before IRC bot sends QUIT
# Blocks the bot until it's done, including PING/PONG handling
# irc is the IRC API object
def on_quit(*, irc):
stop_gameloop()
# handle_message(*, prefix, message, nick, channel, irc)
# Called for PRIVMSGs.
# prefix is the prefix at the start of the message, without the leading ':'
# message is the contents of the message
# nick is who sent the message
# channel is where you should send the response (note: in queries nick == channel)
# irc is the IRC API object
# All strings are bytestrings
def handle_message(*, prefix, message, nick, channel, irc):
global irc_chan
if channel == irc_chan:
parse_command(message.decode(), nick.decode())
# handle_nonmessage(*, prefix, command, arguments, irc)
# Called for all other commands than PINGs and PRIVMSGs.
# prefix is the prefix at the start of the message, without the leading ':'
# command is the command or number code
# arguments is rest of the arguments of the command, represented as a list. ':'-arguments are handled automatically
# irc is the IRC API object
# All strings are bytestrings
def handle_nonmessage(*, prefix, command, arguments, irc):
...