【问题标题】:I'm trying to create a bookmark bot that DMs a message to the user who reacts with an emoji我正在尝试创建一个书签机器人,向对表情符号做出反应的用户发送消息
【发布时间】:2023-03-04 10:16:01
【问题描述】:

也就是说,如果用户对带有“书签”表情符号的任何消息做出反应,我希望机器人通过 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'))

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

任何帮助将不胜感激:)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    我认为这是导致您的问题的原因:

    来自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')
    

    【讨论】:

    • 非常感谢,帮了很多忙,现在可以了!下一个问题:)
    猜你喜欢
    • 2021-10-31
    • 2021-07-24
    • 2020-11-20
    • 2021-07-04
    • 2020-07-30
    • 2020-09-19
    • 2021-06-12
    • 2021-04-11
    • 1970-01-01
    相关资源
    最近更新 更多