【问题标题】:Python add custom reaction for messagePython为消息添加自定义反应
【发布时间】:2019-03-12 02:55:00
【问题描述】:

我想为多个命令添加多个自定义反应,或者如果我们添加反应列表,它将从该列表中添加随机反应。那么该怎么做呢。

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)

随机反应

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)

【问题讨论】:

  • the comments here 中,您提到您正试图让机器人对其对命令的响应添加反应。您可以在此处添加该代码吗?
  • a:abc:78768768768 不是您的表情符号之一,因此您不能那样使用它。相反,只需将该字符串直接应用于add_reactionfor emoji in reactions: await bot.add_reaction(reply, emoji)
  • 终于为Emoji NameEmoji ID 工作了,谢谢。
  • @PatrickHaugh 感谢您的帮助,现在Add Emoji by NameAdd Emoji by ID 一切正常,现在我尝试在添加反应之前进行随机反应。但它不工作。我在我的问题 3rd 代码中添加了上面的代码。
  • emojiresult = random.sample(reactions, k=len(reactions)) 改为

标签: python-3.x discord.py


【解决方案1】:

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

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

【讨论】:

  • 这可能意味着get 未能在server.emojis 中找到表情符号。你可以试试get(bot.get_all_emojis(), name=emoji_name)
  • 你确定你有正确的表情符号名称吗?我去了服务器设置>表情符号页面,并在“别名”下使用了没有:的名称,它工作正常。它不适用于 unicode 表情符号,仅适用于自定义表情符号。如果您希望它与 unicode emoji 一起使用,则应将其更改为 bot.add_reaction(reply, emoji or emoji_name) 并使用 reactions 列表中的 unicode 字符
  • 是的,适用于自定义表情符号。如果我将自定义表情符号放在 1 个服务器中,它可以在所有服务器上工作吗?如果另一台服务器具有相同名称的表情符号会发生什么。
  • 是的,它应该可以跨服务器工作。如果你担心同名的表情符号,你应该使用表情符号id而不是它的名字
  • 好的,如果我在reactions = ['5654654', '65654654', '5656565'] 中添加表情符号 ID,那么我应该更改代码中的任何内容吗?
【解决方案2】:
for r in reactions:
    await bot.add_reaction(message, r)

【讨论】:

  • I made a gist 因为 cmets 很难格式化
  • 我收到语法错误我在我的问题中更新我的新代码。
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 2018-07-23
  • 2021-12-18
  • 2019-05-07
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
相关资源
最近更新 更多