Make SyncBot remember things.

This commit is contained in:
AZ Huang 2014-02-20 19:27:49 +08:00
parent 08c9bfe5b2
commit 92938c95a5

View file

@ -17,6 +17,7 @@ IRC_PORT = 6667
NAME = NICK = IDENT = REALNAME = "SyncBot" NAME = NICK = IDENT = REALNAME = "SyncBot"
CHANNEL = '#tox-ontopic' CHANNEL = '#tox-ontopic'
MEMORY_DB = 'memory.pickle'
class SyncBot(Tox): class SyncBot(Tox):
def __init__(self): def __init__(self):
@ -33,6 +34,10 @@ class SyncBot(Tox):
self.tox_group_id = None self.tox_group_id = None
self.irc_init() self.irc_init()
self.memory = {}
if exists(MEMORY_DB):
self.memory = pickle.load(MEMORY_DB)
def irc_init(self): def irc_init(self):
self.irc = socket.socket() self.irc = socket.socket()
@ -172,13 +177,25 @@ class SyncBot(Tox):
else: else:
self.ensure_exe(self.send_message, (friendid, message)) self.ensure_exe(self.send_message, (friendid, message))
def send_both(self, content):
self.sent = content
self.ensure_exe(self.group_message_send, (self.tox_group_id, content))
self.irc_send('PRIVMSG %s :%s\r\n' % (CHANNEL, content))
def handle_command(self, cmd): def handle_command(self, cmd):
if cmd[1:] in ['syncbot', 'echobot']: cmd = cmd[1:]
self.ensure_exe(self.group_message_send, if cmd in ['syncbot', 'echobot']:
(self.tox_group_id, self.get_address())) self.send_both(self.get_address())
self.sent = self.get_address() elif cmd.startswith('remember '):
self.irc_send('PRIVMSG %s :%s\r\n' % args = cmd[9:].split(' ')
(CHANNEL, self.get_address())) subject = args[0]
desc = ' '.join(args[1:])
self.memory[subject] = desc
pickle.dump(self.memory, MEMORY_DB)
self.send_both('Remembering ^%s: %s' % (subject, desc))
elif self.memory.has_key(cmd):
self.send_both(self.memory[cmd])
t = SyncBot() t = SyncBot()
t.loop() t.loop()