From 63eb1e3889fc757e8efb5df9d9d585c76ff3d487 Mon Sep 17 00:00:00 2001 From: mrhanky Date: Sun, 28 May 2017 12:46:35 +0200 Subject: [PATCH] Removed add/del from mcmaniac (regex event adds now) --- nxy/plugins/mcmaniac.py | 73 +++++++++++++++++------------------------ nxy/utils.py | 5 ++- 2 files changed, 33 insertions(+), 45 deletions(-) diff --git a/nxy/plugins/mcmaniac.py b/nxy/plugins/mcmaniac.py index 71a6ca7..23de7d7 100644 --- a/nxy/plugins/mcmaniac.py +++ b/nxy/plugins/mcmaniac.py @@ -2,6 +2,7 @@ from docopt import Dict as DocOptDict from irc3.plugins.command import command from irc3.utils import IrcString +from irc3 import event from . import DatabasePlugin from ..utils import parse_int @@ -14,48 +15,36 @@ class McManiac(DatabasePlugin): # noinspection PyUnusedLocal @command(options_first=True) def mcmaniac(self, mask: IrcString, channel: IrcString, args: DocOptDict): - """Get, add or delete McManiaCs - - %%mcmaniac + """Get a random McManiaC or by index. + %%mcmaniac [] """ - cmd = args.get('') - item = args.get('') - if cmd and item: - if self.guard.has_permission(mask, 'admin'): - if cmd == 'add': - self.cur.execute('insert into mcmaniacs (item) values (?)', - [item]) - if cmd == 'del': - index, order, op = parse_int(item, select=False) - if not index: - return - self.cur.execute('''delete from mcmaniacs where id = - (select id from mcmaniacs a where ? = (select count(id) - from mcmaniacs b where a.id {op} b.id) order by id {order}) - '''.format(op=op, order=order), [index]) - self.con.commit() - else: - self.bot.notice(mask.nick, 'Permission denied') + index = args.get('') + if index: + index = parse_int(index) + if not index: + return + index, order, _ = index + order = 'id {order}'.format(order=order) + extra = 'offset ?' + binds = [index] else: - index = args.get('') - if index: - index = parse_int(index) - if not index: - return - index, order, _ = index - order = 'id {order}'.format(order=order) - extra = 'offset ?' - binds = [index] - else: - order = 'random()' - extra = '' - binds = [] - self.cur.execute('''select item, - (select count(id) from mcmaniacs b where a.id >= b.id) as idx, - (select count(id) from mcmaniacs) as len - from mcmaniacs a order by {order} limit 1 {extra} - '''.format(order=order, extra=extra), binds) - result = self.cur.fetchone() - if result: - return '[{idx}/{len}] {item}'.format(**result) + order = 'random()' + extra = '' + binds = [] + self.cur.execute('''select item, + (select count(id) from mcmaniacs b where a.id >= b.id) as idx, + (select count(id) from mcmaniacs) as len + from mcmaniacs a order by {order} limit 1 {extra} + '''.format(order=order, extra=extra), binds) + result = self.cur.fetchone() + if result: + return '[{idx}/{len}] {item}'.format(**result) + + @event(r'(?i)^:(?P\S+) PRIVMSG \S+ :(?P.*(?PMc\S+iaC).*)') + def check(self, mask: str, msg: str, item: str): + nick = IrcString(mask).nick + if nick != self.bot.nick and msg != '.reload mcmaniac': + self.cur.execute('insert into mcmaniacs (item) values (?)', [item]) + self.con.commit() + self.bot.notice(nick, '[McManiac] added "{}"'.format(item)) diff --git a/nxy/utils.py b/nxy/utils.py index f825bb0..d4ce693 100644 --- a/nxy/utils.py +++ b/nxy/utils.py @@ -72,7 +72,7 @@ def time_delta(text: str) -> timedelta: return timedelta(**{unit: num}) -def parse_int(val: str, select: bool = True) -> tuple: +def parse_int(val: str) -> tuple: try: val = int(val) if val is not 0: @@ -81,8 +81,7 @@ def parse_int(val: str, select: bool = True) -> tuple: val *= -1 else: order, op = 'asc', '>=' - if select: - val -= 1 + val -= 1 return val, order, op except ValueError: pass