nxy/bot/admin.py
2017-08-22 18:07:38 +02:00

107 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
import irc3
from git import Repo
from docopt import Dict
from irc3.plugins.command import command
from irc3.utils import IrcString
from . import MODULE, Bot, Plugin
@command(permission='admin', show_in_help_list=False)
def reload(bot: Bot, 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: Bot):
super().__init__(bot)
self.repo = Repo(self.bot.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>
"""
channel = args.get('<channel>', target)
self.bot.join(channel)
self.bot.notice(mask.nick, 'Joined channel {}'.format(channel))
conf = self.bot.load_config()
channels = conf.get('autojoins', [])
if channel not in channels:
channels.append(channel)
conf['autojoins'] = channels
self.bot.save_config(conf)
@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>]
"""
channel = args.get('<channel>', target)
self.bot.part(channel)
self.bot.notice(mask.nick, 'Parted channel {}'.format(channel))
conf = self.bot.load_config()
channels = conf.get('autojoins', [])
if channel in channels:
channels.remove(channel)
conf['autojoins'] = channels
self.bot.save_config(conf)
@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>]
"""
channel = args.get('<channel>', target)
self.bot.part(channel)
self.bot.join(channel)
self.bot.notice(mask.nick, 'Cycled channel {}'.format(channel))