【问题标题】:How do i make my bot reply with a bigger version of the emoji on discord.py?如何让我的机器人在 discord.py 上使用更大版本的表情符号回复?
【发布时间】:2021-10-04 17:40:30
【问题描述】:

我正在尝试创建一个命令,让机器人响应您的消息 (!big :emoji:) 并使用更大的表情符号。有没有办法做到这一点?

更新:有没有办法使用所有表情符号而不只是自定义表情符号?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您只需获取自定义不和谐表情的网址,或使用 twitter 表情 api 获取默认表情的图像

    @bot.command()
    async def emoji(ctx, emoji: Union[discord.Emoji, discord.PartialEmoji, str]):
        """Post a large size emojis in chat."""
        if not isinstance(emoji, str):  # if it is a custom discord emoji
            d_emoji = cast(discord.Emoji, emoji)
            ext = "gif" if d_emoji.animated else "png"
            url = d_emoji.url
            filename = f"{d_emoji.name}.{ext}"
        else:  # use the twitter emoji api
            try:
                cdn_fmt = "https://twemoji.maxcdn.com/2/72x72/{codepoint:x}.png"
                url = cdn_fmt.format(codepoint=ord(str(emoji)))
                filename = "emoji.png"
            except TypeError:
                return await ctx.send("That doesn't appear to be a valid emoji")
        try:  # read it with BytesIO so you dont need to save the image
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as resp:
                    image = os.BytesIO(await resp.read())
        except Exception:
            return await ctx.send("That doesn't appear to be a valid emoji")
        file = discord.File(image, filename=filename)
        await ctx.send(file=file)
    

    【讨论】:

    • 您也可以只发送自己的网址,使用“嵌入链接”-权限它也将显示为图像
    【解决方案2】:

    您可以使用指定为discord.Emojistr 的参数创建命令

    • discord.Emoji 用于自定义表情符号
    • str 用于检查默认 unicode 表情符号 UNICODE_EMOJI

    这是一个使用Emoji.urlUNICODE_EMOJI 的示例

    # from typing import Union
    # from emoji import UNICODE_EMOJI
    
    @bot.command()
    async def big(ctx, emoji: Union[discord.Emoji, str] = None):
        # custom emoji
        if isinstance(emoji, discord.Emoji):
            await ctx.send(emoji.url)
        # default emoji
        elif isinstance(emoji, str) and emoji in UNICODE_EMOJI:
            await ctx.send(emoji)
        else:
            await ctx.send("Not a valid emoji")
    

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 2021-03-25
      • 1970-01-01
      • 2020-11-07
      • 2021-04-28
      • 2020-06-29
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多