diff --git a/bot/plugins/admin.py b/bot/plugins/admin.py index 8757013..63c2ede 100644 --- a/bot/plugins/admin.py +++ b/bot/plugins/admin.py @@ -2,11 +2,13 @@ import logging import irc3 +from git import Repo from docopt import Dict from irc3.plugins.command import command from irc3.utils import IrcString from . import MODULE, Plugin +from .. import REPO_DIR logger = logging.getLogger(__name__) @@ -31,7 +33,63 @@ def connected(bot, srv, me, data): bot.mode(me, '+R') -@irc3.plugin class Admin(Plugin): - # TODO: add admin functions (join, part, etc) - pass + def __init__(self, bot: irc3.IrcBot): + super().__init__(bot) + self.repo = Repo(REPO_DIR) + + @command(permission='all_permissions') + def pull(self, mask: IrcString, target: IrcString, args: Dict): + """Pull from the git repo + + %%pull [] + """ + origin = args.get('', 'origin') + + try: + self.repo.remote(origin).pull() + except ValueError as ex: + return '[GIT] {}'.format(ex) + + self.bot.reload() + return '[GIT] Pulled from {} and reloaded the bot'.format(origin) + + @command(permission='all_permissions') + def nick(self, mask: IrcString, target: IrcString, args: Dict): + """Change the nick of the bot (not permanent) + + %%nick + """ + self.bot.nick = args[''] + + @command(permission='all_permissions') + def join(self, mask: IrcString, target: IrcString, args: Dict): + """Let the bot join a channel + + %%join + """ + self.join_part('join', mask, target, args) + + @command(permission='all_permissions') + def part(self, mask: IrcString, target: IrcString, args: Dict): + """Let the bot part a given or the current channel + + %%part [] + """ + self.join_part('part', mask, target, args) + + @command(permission='all_permissions') + def cycle(self, mask: IrcString, target: IrcString, args: Dict): + """Let the bot part and join a given or the current channel + + %%cycle [] + """ + self.join_part('part', mask, target, args) + self.join_part('join', mask, target, args) + + def join_part(self, func: str, mask: IrcString, target: IrcString, args: Dict): + channel = IrcString(args.get('') or target) + + if channel.is_channel: + getattr(self.bot, func)(channel) + self.bot.notice(mask.nick, '{}ed channel {}'.format(func, channel))