2018-11-24 21:22:44 +00:00
|
|
|
"""
|
|
|
|
Contains code used by other kbass_* plugins
|
|
|
|
"""
|
2018-11-24 21:14:04 +00:00
|
|
|
from uniborg import util
|
|
|
|
|
|
|
|
|
2018-11-28 14:19:14 +00:00
|
|
|
async def get_target_message(borg, event):
|
2018-11-28 14:07:49 +00:00
|
|
|
"""
|
|
|
|
If the event is a reply, returns the reply message if it's from us
|
|
|
|
If event is not a reply, then it tries to return the most recent message
|
|
|
|
from us
|
|
|
|
"""
|
|
|
|
target = await event.get_reply_message()
|
|
|
|
if event.is_reply and target.from_id == borg.uid:
|
|
|
|
return target
|
2018-11-28 14:16:01 +00:00
|
|
|
if not target:
|
|
|
|
return await util.get_recent_self_message(borg, event)
|
2018-11-28 14:07:49 +00:00
|
|
|
|
|
|
|
|
2018-11-24 21:14:04 +00:00
|
|
|
def self_reply_cmd(borg, pattern):
|
|
|
|
def wrapper(function):
|
|
|
|
@borg.on(util.admin_cmd(pattern))
|
|
|
|
async def wrapped(event, *args, **kwargs):
|
|
|
|
await event.delete()
|
2018-11-28 14:19:14 +00:00
|
|
|
target = await get_target_message(borg, event)
|
2018-11-24 21:14:04 +00:00
|
|
|
if not target:
|
|
|
|
return
|
|
|
|
return await function(event, target, *args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def self_reply_selector(borg, cmd):
|
|
|
|
def wrapper(function):
|
|
|
|
@borg.on(util.admin_cmd(cmd + r"( [+-]?\d+)?$"))
|
|
|
|
async def wrapped(event, *args, **kwargs):
|
|
|
|
await event.delete()
|
|
|
|
reply = await event.get_reply_message()
|
|
|
|
if not reply:
|
|
|
|
return
|
|
|
|
num_offset = int(event.pattern_match.group(1) or 0)
|
|
|
|
reverse = num_offset > 0
|
|
|
|
|
|
|
|
targets = [reply] if reverse else []
|
|
|
|
targets.extend(await borg.get_messages(
|
|
|
|
await event.get_input_chat(),
|
|
|
|
limit=abs(num_offset),
|
|
|
|
offset_id=reply.id,
|
|
|
|
reverse=reverse
|
|
|
|
))
|
|
|
|
if not reverse:
|
|
|
|
# reverse the list because we want things to always be in
|
|
|
|
# history order
|
|
|
|
targets.reverse()
|
|
|
|
targets.append(reply)
|
|
|
|
|
|
|
|
return await function(event, targets, num_offset, *args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
return wrapper
|