Removed add/del from mcmaniac (regex event adds now)
This commit is contained in:
parent
4fdc951718
commit
63eb1e3889
@ -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,48 +15,36 @@ 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>')
|
index = args.get('<index>')
|
||||||
item = args.get('<mcmaniac>')
|
if index:
|
||||||
if cmd and item:
|
index = parse_int(index)
|
||||||
if self.guard.has_permission(mask, 'admin'):
|
if not index:
|
||||||
if cmd == 'add':
|
return
|
||||||
self.cur.execute('insert into mcmaniacs (item) values (?)',
|
index, order, _ = index
|
||||||
[item])
|
order = 'id {order}'.format(order=order)
|
||||||
if cmd == 'del':
|
extra = 'offset ?'
|
||||||
index, order, op = parse_int(item, select=False)
|
binds = [index]
|
||||||
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:
|
else:
|
||||||
index = args.get('<index>')
|
order = 'random()'
|
||||||
if index:
|
extra = ''
|
||||||
index = parse_int(index)
|
binds = []
|
||||||
if not index:
|
self.cur.execute('''select item,
|
||||||
return
|
(select count(id) from mcmaniacs b where a.id >= b.id) as idx,
|
||||||
index, order, _ = index
|
(select count(id) from mcmaniacs) as len
|
||||||
order = 'id {order}'.format(order=order)
|
from mcmaniacs a order by {order} limit 1 {extra}
|
||||||
extra = 'offset ?'
|
'''.format(order=order, extra=extra), binds)
|
||||||
binds = [index]
|
result = self.cur.fetchone()
|
||||||
else:
|
if result:
|
||||||
order = 'random()'
|
return '[{idx}/{len}] {item}'.format(**result)
|
||||||
extra = ''
|
|
||||||
binds = []
|
@event(r'(?i)^:(?P<mask>\S+) PRIVMSG \S+ :(?P<msg>.*(?P<item>Mc\S+iaC).*)')
|
||||||
self.cur.execute('''select item,
|
def check(self, mask: str, msg: str, item: str):
|
||||||
(select count(id) from mcmaniacs b where a.id >= b.id) as idx,
|
nick = IrcString(mask).nick
|
||||||
(select count(id) from mcmaniacs) as len
|
if nick != self.bot.nick and msg != '.reload mcmaniac':
|
||||||
from mcmaniacs a order by {order} limit 1 {extra}
|
self.cur.execute('insert into mcmaniacs (item) values (?)', [item])
|
||||||
'''.format(order=order, extra=extra), binds)
|
self.con.commit()
|
||||||
result = self.cur.fetchone()
|
self.bot.notice(nick, '[McManiac] added "{}"'.format(item))
|
||||||
if result:
|
|
||||||
return '[{idx}/{len}] {item}'.format(**result)
|
|
||||||
|
@ -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,8 +81,7 @@ 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:
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user