Discord.py - 当 B 发送回复 A 原始消息的消息时,如何从 B 的消息中获取 A 的 ID?

时间:2021-04-10 06:39:28

标签: python discord.py

我正在尝试制作一个不和谐的机器人来自动玩我朋友的机器人游戏。当我朋友的机器人在您使用特定命令后发送消息回复您的消息时。我希望能够检查机器人发送的消息是否专门回复了我的消息。这是我到目前为止所得到的:

@bot.event
async def on_message(message):
    if message.author.id == botId:
        content = str(message.content)
        reference = message.reference
        if reference.message_id.author.id == myId:
            word = content.split('`')[1].split('`')[0]
            await bot.send_message(message.channel, word)

我知道 reference.message_id.author.id 不起作用,因为 author.idreference.message_id 不兼容,但我找不到可以使用它的东西。< /p>

错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\user\OneDrive\Desktop\User\Projects\Programming\Python\Discord Bot\Waterhole Dryer\bot.py", line 30, in on_message
    if reference.message_id.author.id == myId:
AttributeError: 'int' object has no attribute 'author'

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,所以......我找到了一些解决方法......

@bot.event
async def on_message(message):
    if message.author.id == botId:
        guild = bot.get_guild(guildId)
        channel = guild.get_channel(message.reference.channel_id)
        msg = await channel.fetch_message(message.reference.message_id)
        if msg.author.id == myId:
            word = content.split('`')[1].split('`')[0]
            await channel.send(word)

基本上,我创建了一个消息对象。我希望这可以帮助其他人解决我的问题。