dr.botzo/ircbot/ircplugins/ircmgmt.py
Brian S. Stephan 9c1109107b relate channels to their server
this is necessary for supporting multiple irc servers in one bot config.
this also has the side effect of requiring some code in ircbot and
markov which autocreates channels to also include the server (retrieved
via the connection). this will again help keep channels coherent for
multi-server arrangements

the twitter bot change here is untested but seems like the right idea (I
haven't used the twitter package in forever)
2021-04-25 11:13:10 -05:00

61 lines
2.6 KiB
Python

"""Provide some commands for basic IRC functionality."""
import logging
from ircbot.lib import Plugin, has_permission
from ircbot.models import IrcChannel
log = logging.getLogger('ircbot.ircplugins.ircmgmt')
class ChannelManagement(Plugin):
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
def start(self):
"""Set up the handlers."""
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!join\s+([\S]+)',
self.handle_join, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!part\s+([\S]+)',
self.handle_part, -20)
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!quit\s*(.*)',
self.handle_quit, -20)
super(ChannelManagement, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_join)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_part)
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_quit)
super(ChannelManagement, self).stop()
def handle_join(self, connection, event, match):
"""Handle the join command."""
if has_permission(event.source, 'ircbot.manage_current_channels'):
channel = match.group(1)
# put it in the database if it isn't already
chan_mod, c = IrcChannel.objects.get_or_create(name=channel, server=connection.server_config)
log.debug("joining channel %s", channel)
self.connection.join(channel)
return self.bot.reply(event, "Joined channel {0:s}.".format(channel))
def handle_part(self, connection, event, match):
"""Handle the join command."""
if has_permission(event.source, 'ircbot.manage_current_channels'):
channel = match.group(1)
# put it in the database if it isn't already
chan_mod, c = IrcChannel.objects.get_or_create(name=channel, server=connection.server_config)
log.debug("parting channel %s", channel)
self.connection.part(channel)
return self.bot.reply(event, "Parted channel {0:s}.".format(channel))
def handle_quit(self, connection, event, match):
"""Handle the join command."""
if has_permission(event.source, 'ircbot.quit_bot'):
self.bot.die(msg=match.group(1))
plugin = ChannelManagement