Discord Python await message.channel.send 不能在代码中完成吗?



我一直在编写discord bot,我有一些东西要做,但现在我一直在使用wait message.channel.send,这里有错误:

Traceback (most recent call last):
File "C:Users*****PycharmProjectsdiscordAPIvenvlibsite-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:/Users/*****/PycharmProjects/discordAPI/main.py", line 37, in on_raw_reaction_add
await message.channel.send('ADD Reaction To Be **immortal**  Rank')
NameError: name 'message' is not defined

这是代码:


role = None
client = discord.Client()

@client.event
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'Hi':
await message.channel.send('Hii, how are you')
if message.content == '>message set':
await message.channel.send('ADD Reaction To Be **immortal**  Rank')
await message.channel.send('ADD Reaction To Be **coolguy**  Rank')
await message.channel.send('ADD Reaction To Be **mortal**  Rank')

@client.event
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == 833768364215762974:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
print(payload.emoji.name)
global role
if payload.emoji.name == '✅':
role = discord.utils.get(guild.roles, name='immortal')
if role is not None:
print(role.name)
await message.channel.send('anything')
# cant do await message here

@client.event
async def on_raw_reaction_add_remove(payload):
pass

client.run('*******')

为什么我不能在那里使用wait?感谢的所有帮助

因为您首先需要get消息对象。

message = await guild.get_channel(payload.channel_id).fetch_message(payload.message_id)

现在你可以用发送信息了

await message.channel.send()

同样,如果你只使用消息发送消息,你可以通过获取频道来完成。

最新更新