EmoteManager/utils/converter.py

39 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# © 20182020 io mintz <io@mintz.cc>
#
# Emote Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Emote Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Emote Manager. If not, see <https://www.gnu.org/licenses/>.
import functools
from discord.ext.commands import BadArgument
_emote_type_predicates = {
'all': lambda _: True,
'static': lambda e: not e.animated,
'animated': lambda e: e.animated}
# this is kind of a hack to ensure that the last argument is always converted, even if the default is used.
def emote_type_filter_default(command):
old_callback = command.callback
@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)
command.callback = callback
return command