Removed add/del from mcmaniac (regex event adds now)

This commit is contained in:
mrhanky 2017-05-28 12:46:35 +02:00
parent 4fdc951718
commit 63eb1e3889
No known key found for this signature in database
GPG Key ID: 67D772C481CB41B8
2 changed files with 33 additions and 45 deletions

View File

@ -2,6 +2,7 @@
from docopt import Dict as DocOptDict from docopt import Dict as DocOptDict
from irc3.plugins.command import command from irc3.plugins.command import command
from irc3.utils import IrcString from irc3.utils import IrcString
from irc3 import event
from . import DatabasePlugin from . import DatabasePlugin
from ..utils import parse_int from ..utils import parse_int
@ -14,30 +15,10 @@ class McManiac(DatabasePlugin):
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
@command(options_first=True) @command(options_first=True)
def mcmaniac(self, mask: IrcString, channel: IrcString, args: DocOptDict): def mcmaniac(self, mask: IrcString, channel: IrcString, args: DocOptDict):
"""Get, add or delete McManiaCs """Get a random McManiaC or by index.
%%mcmaniac <cmd> <mcmaniac>
%%mcmaniac [<index>] %%mcmaniac [<index>]
""" """
cmd = args.get('<cmd>')
item = args.get('<mcmaniac>')
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')
else:
index = args.get('<index>') index = args.get('<index>')
if index: if index:
index = parse_int(index) index = parse_int(index)
@ -59,3 +40,11 @@ class McManiac(DatabasePlugin):
result = self.cur.fetchone() result = self.cur.fetchone()
if result: if result:
return '[{idx}/{len}] {item}'.format(**result) return '[{idx}/{len}] {item}'.format(**result)
@event(r'(?i)^:(?P<mask>\S+) PRIVMSG \S+ :(?P<msg>.*(?P<item>Mc\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))

View File

@ -72,7 +72,7 @@ def time_delta(text: str) -> timedelta:
return timedelta(**{unit: num}) return timedelta(**{unit: num})
def parse_int(val: str, select: bool = True) -> tuple: def parse_int(val: str) -> tuple:
try: try:
val = int(val) val = int(val)
if val is not 0: if val is not 0:
@ -81,7 +81,6 @@ def parse_int(val: str, select: bool = True) -> tuple:
val *= -1 val *= -1
else: else:
order, op = 'asc', '>=' order, op = 'asc', '>='
if select:
val -= 1 val -= 1
return val, order, op return val, order, op
except ValueError: except ValueError: