我正在尝试创建一个书签机器人,该机器人向对表情符号做出反应的用户发送一条消息



所以说,如果用户对任何消息的反应是"书签";表情符号,我希望机器人通过DM向用户发送该消息。

这是我的第一个机器人,如果能给我一点帮助,我将不胜感激。这是我到目前为止所拥有的-我只想让机器人现在简单地识别书签表情符号并在频道中回复,然后我会在此基础上向用户发送DM。

import discord
from discord.ext import commands
from discord import Color, Embed, Member, Message, RawReactionActionEvent, User
import os
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

bot = commands.Bot("!")
@staticmethod
def _payload_has_bookmark_emoji(payload: RawReactionActionEvent) -> bool:
#Test if the RawReactionActionEvent payload contains a bookmark emoji.
if str(payload.raw.emoji) == "🔖":
return True

@commands.Cog.listener()
async def on_raw_reaction_add(self, payload: RawReactionActionEvent) -> None:

channel = discord.utils.get(self.bot.get_all_channels(), id=payload.channel_id)
message = await channel.fetch_message(payload.message_id)
member = discord.utils.get(message.guild.members, id=payload.user_id)
if self._payload_has_bookmark_emoji(payload) is True:
print("Hello!")
await message.channel.send('Hello!')
client.run(os.getenv('TOKEN'))

运行此代码并通过对带有书签表情符号的消息做出反应来进行测试并没有任何作用,所以我不确定哪里出错了。

如有任何帮助,我们将不胜感激:(

我认为这导致了您的问题:

来自on_raw_reaction_add:的不和谐API参考

当消息添加了反应时调用。与on\reaction_add((不同,无论内部消息缓存的状态如何,都会调用它。这需要启用Intents.reactions。

以下是如何启用Intents.reactions:

intents=discord.Intents.default()
intents.reactions=True
client=discord.Client(intents=intents)

此外,当您将开发人员门户连接到服务器时,请确保您在OAuth中检查了开发人员门户的正确权限。对于调试,如果你拥有服务器,你可以赋予机器人管理员角色,并用登录权限将其连接到服务器。

以下是我用来DM用户的代码(供您稍后使用(:

DMChannel=user.dm_channel
if DMchannel is None:
DMchannel=await user.create_dm()
await DMchannel.send('DM goes here')

相关内容

最新更新