【问题标题】:How do I add a reaction emoji to the message the bot sends after doing the user's command?如何在执行用户命令后向机器人发送的消息中添加反应表情符号?
【发布时间】:2020-06-01 18:56:34
【问题描述】:

我收到此错误:discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'Bot' 对象没有属性'message'--尝试执行await self.client.message.add_reaction(emoji) 时。

我尝试将其更改为 await ctx.message.add_reaction(emoji),我意识到它是对用户发送的命令而不是机器人的新消息做出反应。

import discord
from discord.ext import commands

class MovieNight(commands.Cog):
    """Polls for Movie Night."""

    def __init__(self, client):
        self.client = client

    @commands.command(aliases=['m'])
    async def movie(self, ctx, year, *movie):
        movie_title = ' '.join(movie[:-1])
        await ctx.send(f"`{year}` - `{movie_title}` | **{movie[-1]}**")
        emoji = '????'
        await self.client.message.add_reaction(emoji)

def setup(client):
    client.add_cog(MovieNight(client))

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    self.client 不知道该消息,它作为invocation context 的一部分存储:

    await ctx.message.add_reaction(emoji)
    

    【讨论】:

    • “已存储”?我不明白这里说了什么。但是,我完全不确定如何去做我现在想做的事:我什至如何获取机器人的新消息?
    【解决方案2】:

    根据您在评论中所说的内容添加帕特里克的回答。

    await self.client.message.add_reaction(emoji) 不起作用,因为机器人不知道您指的是什么消息,并且client 没有名为message 的属性。

    添加反应需要discord.Message 对象,在您的情况下,可以是用户执行的命令(例如!movie 2020 movie title),您可以通过ctx.message 检索该命令,或者您正在制作机器人的消息发送。

    如果你想从机器人发送的消息中获取消息对象,你可以将它分配给一个变量:

    msg = await ctx.send(f"`{year}` - `{movie_title}` | **{movie[-1]}**")
    

    这使您可以添加反应或访问您想要的任何其他消息属性:

    emoji = '?'
    await msg.add_reaction(emoji)
    

    参考资料:

    【讨论】:

    • 自己整理的很棒,很高兴听到它!祝机器人好运:^)
    猜你喜欢
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2021-10-31
    • 2022-06-11
    • 1970-01-01
    • 2023-03-04
    • 2021-04-11
    相关资源
    最近更新 更多