python-3.x - Python为消息添加自定义 react

标签 python-3.x discord.py

我想为多个命令添加多个自定义 react ,或者如果我们添加 react 列表,它将从该列表中添加随机 react 。那么如何做到这一点。

from discord.utils import get

按名称添加表情符号。
reactions = ['emoji_name_1', 'emoji_name_2', 'emoji_name_3']

@bot.command(pass_context=True)
async def ping1(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    for emoji_name in reactions:
        emoji = get(bot.get_all_emojis(), name=emoji_name)
        await bot.add_reaction(reply, emoji)

通过 ID 添加表情符号。
reactions = ['a:abc:78768768768', 'a:def:768768766', 'a:ghi:878768787687']

@bot.command(pass_context=True)
async def ping2(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    for emoji in emojilist:
        await bot.add_reaction(reply, emoji)

随机 react
reactions = ['a:abc:78768768768', 'a:def:768768766', 'a:ghi:878768787687']

@bot.command(pass_context=True)
async def ping2(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    emojiresult = random.shuffle(reactions)
    for emoji in emojiresult:
        await bot.add_reaction(reply, emoji)

最佳答案

您需要捕获要发送的消息,然后对该消息调用 add_reaction,而不是作为参数传递给 messageon_message

from discord.utils import get

reactions = ['123', '456', '💖']

@commands.command(pass_context=True)
async def ping(self, ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await self.bot.say(msg)
    for emoji_id in reactions:
        emoji = get(ctx.server.emojis, name=emoji_id)
        await bot.add_reaction(reply, emoji or emoji_id)  
        # If emoji is None, then emoji_id is likely a unicode emoji

关于python-3.x - Python为消息添加自定义 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52685273/

相关文章:

python-3.x - 在Python中写入远程服务器上的文件

python - discord.py:如何获取用户名的首字母?

list - 换行符不适用于嵌入消息中的列表 - Discord.py

python - discord.py 检查嵌入标题的内容

python - 如何在保留 '\' 的同时替换 '\n'?

python-3.x - heroku python datetime不包括fromisoformat

python - Heroku - 没有网络进程在运行

python-3.x - 如何在 python3 urllib.request 中禁用自动重定向?

python - 在 Signal Handler 中调用 Async 函数,Asyncio

python - discord.py 对 cog 中每个命令的错误处理