添加和响应不和谐.py中的反应



代码:

import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')

async def on_message(self, message):
words = [] # List of words to look for
if message.author.id == self.user.id:
return
for i in words:
if i in message.content.lower():
await message.channel.send(f"Hey <@{message.author.id}>, i have noticed that in your message is a word on a list")
break

await message.add_reaction("✅")
await client.wait_for("reaction_add")
await message.delete()


client = MyClient()
client.run("TOKEN")

我如何让机器人向自己的消息添加反应,如果用户使用它,我如何删除自己的消息我确实在寻找答案,但上帝不和谐。我看到了6个不起作用的答案,所有的答案似乎都使用了不同的模块

如果答案很容易找到,我很抱歉,但我就是找不到

首先,您应该始终尝试在文档(discord.py文档(中找到答案。(离题:混乱的不是discord.py;很可能是你用来寻找答案的方法。(

TextChannel.send()方法返回已发送的消息。因此,您可以将返回值分配给一个变量。

对于另一个问题,有一个检测消息删除的事件侦听器on_message_delete()

import discord
class MyClient(discord.Client):
async def on_ready(self):
...
async def on_message(self, message):
words = []
if message.author.id == self.user.id:
return
for i in words:
if i in message.content.lower():
sent_message = await message.channel.send(
f"Hey {message.author.mention}, I have noticed that in your message is a word on a list"
)
break
await sent_message.add_reaction("reaction")
await message.add_reaction("✅")
await client.wait_for("reaction_add")
await message.delete()
async def on_message_delete(self, message):
# Do stuff here

client = MyClient()
client.run("TOKEN")

(附带说明的是,您可以使用Member.mention来提及/ping成员,而不是"<@{message.author.id}>"。(

在我看来,最好在事件方法之上使用@client.event方法decorator,而不是将它们放在自己的类中。您可以在顶部将客户端对象声明为client=discord.Client(),然后将@client.event放在事件处理方法之上。n_reaction_add方法可以具有要响应的reaction和message参数。

最新更新