在执行用户命令后,如何在机器人发送的消息中添加反应表情符号?

时间:2020-05-30 23:39:46

标签: python-3.x discord.py

我收到此错误: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))

2 个答案:

答案 0 :(得分:2)

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

await ctx.message.add_reaction(emoji)

答案 1 :(得分:1)

在您的评论中添加帕特里克的回答。

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)

参考: