【问题标题】:Discord.py make discord bot react to message with several emotesDiscord.py 使 discord bot 对带有多个表情的消息做出反应
【发布时间】:2021-03-25 13:14:43
【问题描述】:

我知道如何让机器人对消息做出反应,但我被我的项目卡住了,而且我经常遇到麻烦。我想执行一个语法为-react [message ID] [emote1] [emote2] [emote 3] [emote ...] 的命令,该命令使用我在ID 后面放置的表情对属于ID 的消息做出反应。

我知道如何让机器人用相同的表情对相同的消息做出反应,并尝试用代码让它对其他消息做出反应:

@client.command()
async def react(ctx, ID):
    emotes = ['❤️', '????']
    msg = message.fetch_message(ID)
    for emote in emotes:
        await ctx.msg.add_reaction(emoji=emote)

但这总是在msg = message.fetch_message(ID) 中输出NameError: name 'message' is not defined,另外我一点也不知道如何使它与自己选择的表情一起使用。

所以,我的主要问题是:

  1. 如何让机器人对特定消息做出反应? (已解决)
  2. 如何使用自己指定的表情来制作它?

更新:第一个问题解决了,我只需要添加

message_id = ID
    msg = await ctx.fetch_message(message_id)

但我仍然坚持让它与用户给定的表情做出反应,而不是硬编码的。

【问题讨论】:

  • 想一想:message 是在哪里定义的?此外,如果您已经收到消息,为什么要从该消息中调用fetch_message?它也是ctx.message 而不是ctx.msg,如果你想从你的变量msg 中调用add_reaction,你只需使用msg,而不是ctx.msg
  • 我设法让机器人对来自我提供的 ID 的消息做出反应,只需添加 message_id = ID msg = await ctx.fetch_message(message_id),但我仍然坚持让它对用户给定的表情做出反应,而不是硬编码的

标签: discord bots discord.py response


【解决方案1】:
  1. 您没有定义 message 变量,这就是它抛出该错误的原因。
  2. ID arg 是一个字符串,id 必须是整数
  3. ctx 没有属性 msg 它是 message

此外,要实现表情符号,您可以简单地将它们作为元组传递并使用 for 循环遍历它们。

@client.command()
async def react(ctx, ID: int, *emojis):
    message = ctx.fetch_message(ID)

    for emoji in emojis:
        await message.add_reaction(emoji)
        # Or if you want to add the reactions to the message which invoked the command
        await ctx.message.add_reaction(emoji)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2019-06-07
    • 2020-07-31
    • 2021-05-12
    • 2018-07-24
    • 2019-05-24
    • 2021-10-05
    相关资源
    最近更新 更多