nxy/bot/admin.py

107 lines
3.1 KiB
Python
Raw Normal View History

2017-05-15 22:26:10 +00:00
# -*- coding: utf-8 -*-
2017-05-16 10:06:29 +00:00
import irc3
from git import Repo
2017-08-22 13:57:57 +00:00
from docopt import Dict
2017-05-15 22:26:10 +00:00
from irc3.plugins.command import command
2017-05-16 10:06:29 +00:00
from irc3.utils import IrcString
2017-05-15 22:26:10 +00:00
2017-08-22 15:43:48 +00:00
from . import MODULE, Bot, Plugin
2017-07-04 13:22:20 +00:00
2017-05-15 22:26:10 +00:00
@command(permission='admin', show_in_help_list=False)
2017-08-22 15:43:48 +00:00
def reload(bot: Bot, mask: IrcString, target: IrcString, args: Dict):
2017-06-30 11:16:39 +00:00
"""Reloads a plugin or the whole bot.
2017-05-16 12:42:21 +00:00
%%reload [<plugin>]
2017-05-15 22:26:10 +00:00
"""
2017-05-16 12:42:21 +00:00
plugin = args.get('<plugin>')
if plugin:
2017-08-21 16:27:33 +00:00
bot.reload('{}.{}'.format(MODULE, plugin))
2017-07-31 14:10:24 +00:00
bot.privmsg(target, 'Reloaded plugin "{}"'.format(plugin))
2017-05-16 12:42:21 +00:00
else:
bot.reload()
bot.privmsg(target, 'Reloaded the bot')
2017-07-04 13:22:20 +00:00
2017-07-31 14:10:24 +00:00
@irc3.event(irc3.rfc.CONNECTED)
def connected(bot, srv, me, data):
bot.mode(me, '+B')
2017-07-31 14:10:24 +00:00
2017-07-04 13:22:20 +00:00
class Admin(Plugin):
2017-08-22 15:43:48 +00:00
def __init__(self, bot: Bot):
super().__init__(bot)
2017-08-22 15:43:48 +00:00
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>
"""
2017-08-22 15:43:48 +00:00
channel = args.get('<channel>', target)
self.bot.join(channel)
self.bot.notice(mask.nick, 'Joined channel {}'.format(channel))
2017-08-22 16:02:33 +00:00
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>]
"""
2017-08-22 15:43:48 +00:00
channel = args.get('<channel>', target)
2017-08-22 16:04:02 +00:00
self.bot.part(channel)
2017-08-22 15:43:48 +00:00
self.bot.notice(mask.nick, 'Parted channel {}'.format(channel))
2017-08-22 16:02:33 +00:00
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>]
"""
2017-08-22 15:43:48 +00:00
channel = args.get('<channel>', target)
2017-08-22 15:43:48 +00:00
self.bot.part(channel)
self.bot.join(channel)
self.bot.notice(mask.nick, 'Cycled channel {}'.format(channel))