nxy/bot/plugins/admin.py

96 lines
2.7 KiB
Python
Raw Normal View History

2017-05-16 00:26:10 +02:00
# -*- coding: utf-8 -*-
2017-07-04 15:22:20 +02:00
import logging
2017-05-16 12:06:29 +02:00
import irc3
from git import Repo
2017-08-22 15:57:57 +02:00
from docopt import Dict
2017-05-16 00:26:10 +02:00
from irc3.plugins.command import command
2017-05-16 12:06:29 +02:00
from irc3.utils import IrcString
2017-05-16 00:26:10 +02:00
2017-07-04 15:22:20 +02:00
from . import MODULE, Plugin
from .. import REPO_DIR
2017-07-04 15:22:20 +02:00
logger = logging.getLogger(__name__)
2017-05-16 00:26:10 +02:00
@command(permission='admin', show_in_help_list=False)
2017-08-22 15:57:57 +02:00
def reload(bot: irc3.IrcBot, mask: IrcString, target: IrcString, args: Dict):
2017-06-30 13:16:39 +02:00
"""Reloads a plugin or the whole bot.
2017-05-16 14:42:21 +02:00
%%reload [<plugin>]
2017-05-16 00:26:10 +02:00
"""
2017-05-16 14:42:21 +02:00
plugin = args.get('<plugin>')
if plugin:
2017-08-21 18:27:33 +02:00
bot.reload('{}.{}'.format(MODULE, plugin))
2017-07-31 16:10:24 +02:00
bot.privmsg(target, 'Reloaded plugin "{}"'.format(plugin))
2017-05-16 14:42:21 +02:00
else:
bot.reload()
bot.privmsg(target, 'Reloaded the bot')
2017-07-04 15:22:20 +02:00
2017-07-31 16:10:24 +02:00
@irc3.event(irc3.rfc.CONNECTED)
def connected(bot, srv, me, data):
bot.mode(me, '+R')
2017-07-04 15:22:20 +02:00
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))