简体   繁体   English

Discord 机器人对消息添加反应 discord.py(无自定义表情符号)

[英]Discord bot adding reactions to a message discord.py (no custom emojis)

I've been trying to make a bot using discord.py add a reaction to a message using discord.py after reading this (which is not what I wanted because I am not using custom emojis) but it ends up giving this error:我一直在尝试使用 discord.py 创建一个机器人,在阅读这篇文章后使用 discord.py 添加对消息的反应(这不是我想要的,因为我没有使用自定义表情符号)但它最终给出了这个错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: InvalidArgument: message argument must be a Message

I tried this code我试过这段代码

@commands.command(pass_context=True)
async def emoji(ctx):
    msg = "working"
    await bot.say(msg)
    reactions = ['dart']
    for emoji in reactions: await bot.add_reaction(msg, emoji)

Any other related questions here on discord.py are just not helpful for this Any ideas on how to implement this discord.py 上的任何其他相关问题对此都没有帮助关于如何实现这个的任何想法

The error message is telling you what the error is: " message argument must be a Message "错误消息告诉您错误是什么:“ message argument must be a Message

In the line在行中

await bot.add_reaction(msg, emoji)

msg is a string, not a Message object. msg是一个字符串,而不是一个Message对象。 You need to capture the message you send and then add the reactions to that message:您需要捕获您发送的消息,然后向该消息添加反应:

@commands.command(pass_context=True)
async def emoji(ctx):
    msg = await bot.say("working")
    reactions = ['dart']
    for emoji in reactions: 
        await bot.add_reaction(msg, emoji)

Note that in later versions of discord.py , add_reaction has changed from bot.add_reaction(msg, emoji) to await msg.add_reaction(emoji) .请注意,在以后的版本discord.pyadd_reaction已经从bot.add_reaction(msg, emoji)await msg.add_reaction(emoji)

Adding on to this as I came across this question wondering how to add reactions in 2022, in case someone else runs into this.除此之外,我还遇到了这个问题,想知道如何在 2022 年添加反应,以防其他人遇到这个问题。

The method displayed in the answer above containing:上面答案中显示的方法包含:

@commands.command(pass_context=True)
async def emoji(ctx):
    msg = await bot.say("working")
    reactions = ['dart']
    for emoji in reactions: 
        await bot.add_reaction(msg, emoji)

This will no longer work and you will get an error like:这将不再有效,您将收到如下错误:

AttributeError: 'Bot' object has no attribute 'add_reaction'

Instead, you'll want to do something like:相反,您需要执行以下操作:

## EXAMPLE
@bot.command()
async def example(ctx):
    msg = await ctx.send("Hello")
    reaction = '👋'
    await msg.add_reaction(reaction)

For multiple reactions, I've also found that you need to do them separately:对于多重反应,我还发现您需要分别进行:

## EXAMPLE
@bot.command()
async def example(ctx):
    msg = await ctx.send("Hello")
    reaction1 = '👋'
    reaction1 = '🙂'
    await msg.add_reaction(reaction1)
    await msg.add_reaction(reaction2)

And, for anyone wondering about custom emojis: (retrieve the emoji ID by typing \ then clicking the emoji, or you can go to save the file of the custom emoji and copy the default value it gives you.)而且,对于任何想了解自定义表情符号的人:(通过键入\然后单击表情符号来检索表情符号 ID,或者您可以 go 保存自定义表情符号的文件并复制它为您提供的默认值。)

## EXAMPLE
@bot.command()
async def example(ctx):
    msg = await ctx.send("Hello")
    reaction1 = "<:yes:965728109200552036>"
    reaction1 = "<:no:965728137122050068>"
    await msg.add_reaction(reaction1)
    await msg.add_reaction(reaction2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM