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 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
"""Get a random McManiaC or by index.
%%mcmaniac <cmd> <mcmaniac>
%%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')
index = args.get('<index>')
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('<index>')
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<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})
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