【发布时间】:2021-10-04 17:40:30
【问题描述】:
我正在尝试创建一个命令,让机器人响应您的消息 (!big :emoji:) 并使用更大的表情符号。有没有办法做到这一点?
更新:有没有办法使用所有表情符号而不只是自定义表情符号?
【问题讨论】:
标签: discord.py
我正在尝试创建一个命令,让机器人响应您的消息 (!big :emoji:) 并使用更大的表情符号。有没有办法做到这一点?
更新:有没有办法使用所有表情符号而不只是自定义表情符号?
【问题讨论】:
标签: discord.py
您只需获取自定义不和谐表情的网址,或使用 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)
【讨论】:
您可以使用指定为discord.Emoji 或str 的参数创建命令
discord.Emoji 用于自定义表情符号str 用于检查默认 unicode 表情符号 UNICODE_EMOJI
这是一个使用Emoji.url 和UNICODE_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")
【讨论】: