nxy/bot/plugins/admin.py
2017-08-22 16:46:33 +02:00

96 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
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__)
@command(permission='admin', show_in_help_list=False)
def reload(bot: irc3.IrcBot, mask: IrcString, target: IrcString, args: Dict):
"""Reloads a plugin or the whole bot.
%%reload [<plugin>]
"""
plugin = args.get('<plugin>')
if plugin:
bot.reload('{}.{}'.format(MODULE, plugin))
bot.privmsg(target, 'Reloaded plugin "{}"'.format(plugin))
else:
bot.reload()
bot.privmsg(target, 'Reloaded the bot')
@irc3.event(irc3.rfc.CONNECTED)
def connected(bot, srv, me, data):
bot.mode(me, '+R')
class Admin(Plugin):
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>]
"""
origin = args.get('<origin>', '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 <nick>
"""
self.bot.nick = args['<nick>']
@command(permission='all_permissions')
def join(self, mask: IrcString, target: IrcString, args: Dict):
"""Let the bot join a channel
%%join <channel>
"""
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 [<channel>]
"""
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 [<channel>]
"""
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('<channel>') or target)
if channel.is_channel:
getattr(self.bot, func)(channel)
self.bot.notice(mask.nick, '{}ed channel {}'.format(func, channel))