packages/ircbot: organize code in hooks
This commit is contained in:
parent
5e787f49ce
commit
dff97b97a5
5 changed files with 78 additions and 7 deletions
1
packages/servers/ircbot/hooks/.gitignore
vendored
Normal file
1
packages/servers/ircbot/hooks/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/__pycache__
|
7
packages/servers/ircbot/hooks/fistbump.py
Normal file
7
packages/servers/ircbot/hooks/fistbump.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import main
|
||||||
|
|
||||||
|
class EventHandler(main.EventHandler):
|
||||||
|
def on_message(bot, e):
|
||||||
|
if e.message == '.fistbump':
|
||||||
|
msg = f'vroooooooooooo fiiiist, {e.sender}! :^)'
|
||||||
|
bot.send_message(e.channel, msg)
|
7
packages/servers/ircbot/hooks/quit.py
Normal file
7
packages/servers/ircbot/hooks/quit.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import main
|
||||||
|
|
||||||
|
class EventHandler(main.EventHandler):
|
||||||
|
def on_message(bot, e):
|
||||||
|
if e.message == '.quit':
|
||||||
|
bot.send_message(e.channel, 'exiting...')
|
||||||
|
bot.emit('quit')
|
7
packages/servers/ircbot/hooks/reload.py
Normal file
7
packages/servers/ircbot/hooks/reload.py
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import main
|
||||||
|
|
||||||
|
class EventHandler(main.EventHandler):
|
||||||
|
def on_message(bot, e):
|
||||||
|
if e.message == '.reload':
|
||||||
|
bot.send_message(e.channel, 'reloading hooks...')
|
||||||
|
bot.emit('reload-hooks')
|
|
@ -1,34 +1,83 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
import justirc
|
import justirc
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
class EventHandler(object):
|
||||||
|
def on_message(bot, event):
|
||||||
|
...
|
||||||
|
|
||||||
|
def on_reload(bot):
|
||||||
|
...
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = dict(nick='smith', debug=False)
|
config = dict(
|
||||||
|
debug=False,
|
||||||
|
nick='smith',
|
||||||
|
channel='#general',
|
||||||
|
server='irc.privatevoid.net',
|
||||||
|
port=6697,
|
||||||
|
tls=True,
|
||||||
|
)
|
||||||
run_bot(config)
|
run_bot(config)
|
||||||
|
|
||||||
|
def shutdown_bot_hooks(bot):
|
||||||
|
for name, hook in bot.hooks:
|
||||||
|
try:
|
||||||
|
hook.EventHandler.on_reload(bot)
|
||||||
|
except Exception as e:
|
||||||
|
print(f'exception running hook {name}: {e}')
|
||||||
|
|
||||||
def run_bot(c):
|
def run_bot(c):
|
||||||
bot = justirc.IRCConnection()
|
bot = justirc.IRCConnection()
|
||||||
|
|
||||||
|
bot.db = () # TODO: store a database handle here
|
||||||
|
bot.hooks = [] # storage for all bot hooks
|
||||||
|
|
||||||
if c['debug']:
|
if c['debug']:
|
||||||
@bot.on('packet')
|
@bot.on('packet')
|
||||||
def new_packet(e):
|
def new_packet(e):
|
||||||
print(e.packet)
|
print(e.packet)
|
||||||
|
|
||||||
|
@bot.on('reload-hooks')
|
||||||
|
def reload_hooks(e):
|
||||||
|
shutdown_bot_hooks(bot)
|
||||||
|
bot.hooks.clear()
|
||||||
|
|
||||||
|
for path in filter(lambda h: h[-3:] == '.py', os.listdir('hooks')):
|
||||||
|
name = '.'.join(['hooks', path.split('.py')[0]])
|
||||||
|
if name in sys.modules.keys():
|
||||||
|
del sys.modules[name]
|
||||||
|
try:
|
||||||
|
mod = importlib.import_module(name, package=name)
|
||||||
|
bot.hooks.append((name, mod))
|
||||||
|
except Exception as e:
|
||||||
|
print(f'failed to load hook {name}: {e}')
|
||||||
|
|
||||||
|
@bot.on('quit')
|
||||||
|
def quit(e):
|
||||||
|
shutdown_bot_hooks(bot)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
@bot.on('connect')
|
@bot.on('connect')
|
||||||
def connect(e):
|
def connect(e):
|
||||||
bot.send_line(f'NICK {c["nick"]}')
|
bot.send_line(f'NICK {c["nick"]}')
|
||||||
bot.send_line(f'USER {c["nick"]} 8 * {c["nick"]}')
|
bot.send_line(f'USER {c["nick"]} 8 * {c["nick"]}')
|
||||||
|
bot.emit('reload-hooks')
|
||||||
|
|
||||||
@bot.on('welcome')
|
@bot.on('welcome')
|
||||||
def welcome(e):
|
def welcome(e):
|
||||||
bot.join_channel("#general")
|
bot.join_channel(c['channel'])
|
||||||
|
|
||||||
@bot.on('message')
|
@bot.on('message')
|
||||||
def message(e):
|
def message(e):
|
||||||
message = e.message.lower()
|
for name, hook in bot.hooks:
|
||||||
if message == '.fistbump':
|
try:
|
||||||
message = f'vroooooooooooo fiiiist, {e.sender} :vvvv)))'
|
hook.EventHandler.on_message(bot, e)
|
||||||
bot.send_message(e.channel, message)
|
except Exception as e:
|
||||||
|
print(f'exception running hook {name}: {e}')
|
||||||
|
|
||||||
bot.connect('irc.privatevoid.net', port=6697, tls=True)
|
bot.connect(c['server'], port=c['port'], tls=c['tls'])
|
||||||
bot.run_loop()
|
bot.run_loop()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue