EmoteManager/utils/converter.py

30 lines
913 B
Python
Raw Normal View History

2021-07-11 03:03:32 +00:00
# © lambda#0987 <lambda@lambda.dance>
# SPDX-License-Identifier: AGPL-3.0-or-later
2020-05-12 23:55:08 +00:00
2020-06-01 05:47:10 +00:00
import functools
2021-09-07 15:14:44 +00:00
from nextcord.ext.commands import BadArgument
2020-06-01 05:47:10 +00:00
2019-10-15 21:59:34 +00:00
_emote_type_predicates = {
2021-09-07 15:14:44 +00:00
'all': lambda _: True,
'static': lambda e: not e.animated,
'animated': lambda e: e.animated}
2019-10-15 21:59:34 +00:00
2020-06-01 05:47:10 +00:00
# this is kind of a hack to ensure that the last argument is always converted, even if the default is used.
2021-09-07 15:14:44 +00:00
2020-06-01 05:47:10 +00:00
def emote_type_filter_default(command):
2021-09-07 15:14:44 +00:00
old_callback = command.callback
2020-06-01 05:47:10 +00:00
2021-09-07 15:14:44 +00:00
@functools.wraps(old_callback)
async def callback(self, ctx, *args):
image_type = args[-1]
try:
image_type = _emote_type_predicates[image_type]
except KeyError:
raise BadArgument(
f'Invalid emote type. Specify one of "all", "static", or "animated".')
return await old_callback(self, ctx, *args[:-1], image_type)
2020-06-01 05:47:10 +00:00
2021-09-07 15:14:44 +00:00
command.callback = callback
return command