Start integrating gameloop.py into the bot
This commit is contained in:
parent
c09ddf4bda
commit
e61e45794c
3 changed files with 105 additions and 16 deletions
|
@ -1,7 +1,10 @@
|
||||||
[server]
|
[server]
|
||||||
host = irc.freenode.net
|
host = irc.freenode.net
|
||||||
port = 6667
|
port = 6667
|
||||||
nick = o3-base
|
nick = taash-e-aakramak
|
||||||
username = o3-base
|
username = o3-base
|
||||||
realname = IRC bot based on o3-base
|
realname = IRC bot based on o3-base
|
||||||
channels = ##ingsoc
|
channels = ###cards
|
||||||
|
|
||||||
|
[nickserv]
|
||||||
|
password =
|
||||||
|
|
96
botcmd.py
96
botcmd.py
|
@ -1,9 +1,86 @@
|
||||||
|
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)
|
# initialize(*, config)
|
||||||
# Called to initialize the IRC bot
|
# Called to initialize the IRC bot
|
||||||
# Runs before even logger is brought up, and blocks further bringup until it's done
|
# 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
|
# config is a configpatser.ConfigParser object containig contents of bot.conf
|
||||||
def initialize(*, config):
|
def initialize(*, config):
|
||||||
...
|
global nickserv_pass, irc_chan
|
||||||
|
nickserv_pass = config['nickserv']['password']
|
||||||
|
irc_chan = config['server']['channels'].split()[0].encode()
|
||||||
|
|
||||||
# on_connect(*, irc)
|
# on_connect(*, irc)
|
||||||
# Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else
|
# Called after IRC bot has connected and sent the USER/NICk commands but not yet attempted anything else
|
||||||
|
@ -11,14 +88,22 @@ def initialize(*, config):
|
||||||
# Blocks the bot until it's done, including PING/PONG handling
|
# Blocks the bot until it's done, including PING/PONG handling
|
||||||
# irc is the IRC API object
|
# irc is the IRC API object
|
||||||
def on_connect(*, irc):
|
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)
|
# on_quit(*, irc)
|
||||||
# Called just before IRC bot sends QUIT
|
# Called just before IRC bot sends QUIT
|
||||||
# Blocks the bot until it's done, including PING/PONG handling
|
# Blocks the bot until it's done, including PING/PONG handling
|
||||||
# irc is the IRC API object
|
# irc is the IRC API object
|
||||||
def on_quit(*, irc):
|
def on_quit(*, irc):
|
||||||
...
|
stop_gameloop()
|
||||||
|
|
||||||
# handle_message(*, prefix, message, nick, channel, irc)
|
# handle_message(*, prefix, message, nick, channel, irc)
|
||||||
# Called for PRIVMSGs.
|
# Called for PRIVMSGs.
|
||||||
|
@ -29,7 +114,10 @@ def on_quit(*, irc):
|
||||||
# irc is the IRC API object
|
# irc is the IRC API object
|
||||||
# All strings are bytestrings
|
# All strings are bytestrings
|
||||||
def handle_message(*, prefix, message, nick, channel, irc):
|
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)
|
# handle_nonmessage(*, prefix, command, arguments, irc)
|
||||||
# Called for all other commands than PINGs and PRIVMSGs.
|
# Called for all other commands than PINGs and PRIVMSGs.
|
||||||
|
|
14
gameloop.py
14
gameloop.py
|
@ -88,15 +88,15 @@ def get_event():
|
||||||
else:
|
else:
|
||||||
print('?')
|
print('?')
|
||||||
|
|
||||||
class Error: pass
|
def send(text):
|
||||||
|
|
||||||
def game():
|
|
||||||
def send(text):
|
|
||||||
print(text)
|
print(text)
|
||||||
|
|
||||||
def notice(nick, text):
|
def notice(nick, text):
|
||||||
print('\t', nick, text)
|
print('\t', nick, text)
|
||||||
|
|
||||||
|
class Error: pass
|
||||||
|
|
||||||
|
def game(send, notice, get_event):
|
||||||
def error(message):
|
def error(message):
|
||||||
send('Error: %s' % message)
|
send('Error: %s' % message)
|
||||||
|
|
||||||
|
@ -702,7 +702,5 @@ def game():
|
||||||
while state != quit:
|
while state != quit:
|
||||||
state = state()
|
state = state()
|
||||||
|
|
||||||
send('Quiting')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
game()
|
game(send, notice, get_event)
|
||||||
|
|
Loading…
Reference in a new issue