我收到此错误: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))
答案 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)
参考:
discord.Message
Message.add_reaction()
TextChannel.send()
-在这里您可以看到它返回了已发送的消息