IRC Bot package #83

Closed
max wants to merge 16 commits from pkg-ircbot into master
5 changed files with 78 additions and 7 deletions
Showing only changes of commit dff97b97a5 - Show all commits

View file

@ -0,0 +1 @@
/__pycache__

View 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)

View 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')

View 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')

View file

@ -1,34 +1,83 @@
import os
import sys
import justirc
import importlib
class EventHandler(object):
def on_message(bot, event):
...
def on_reload(bot):
...
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)
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):
bot = justirc.IRCConnection()
bot.db = () # TODO: store a database handle here
bot.hooks = [] # storage for all bot hooks
if c['debug']:
@bot.on('packet')
def new_packet(e):
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')
def connect(e):
bot.send_line(f'NICK {c["nick"]}')
bot.send_line(f'USER {c["nick"]} 8 * {c["nick"]}')
bot.emit('reload-hooks')
@bot.on('welcome')
def welcome(e):
bot.join_channel("#general")
bot.join_channel(c['channel'])
@bot.on('message')
def message(e):
message = e.message.lower()
if message == '.fistbump':
message = f'vroooooooooooo fiiiist, {e.sender} :vvvv)))'
bot.send_message(e.channel, message)
for name, hook in bot.hooks:
try:
hook.EventHandler.on_message(bot, e)
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()
if __name__ == '__main__':