如何在discord.py中使on_message事件仅读取来自一个通道的消息

问题描述 投票:0回答:1

在我的代码中,我的不和谐机器人有这个 on_message 事件,我尝试让它只检测来自它工作的一个通道的消息,但不是我想要的方式。

就像现在,如果我在任何通道中输入任何内容,它都会在控制台中打印该文本,并且看起来它会用 ! 覆盖正常命令。前缀,因为当此事件位于代码中时,命令甚至不起作用。

有没有一种方法可以让它真正只从一个通道读取消息,而不是检查整个服务器上的消息?

活动详情如下:

@bot.event
async def on_message(message):
    print("This print shows in console because this event reads every channels messages.")
    if message.author == bot.user:
        return

    if message.channel.id != 1221858607000326186:
        return

    if 'vouch' in message.content.lower() and message.mentions:
        check_db_connection()
        mentioned_user = message.mentions[0]

        cursor.execute("SELECT * FROM midman_information WHERE midman_user LIKE %s", (f"%<@{mentioned_user.id}>%",))
        result = cursor.fetchone()

        if result:
            vouch_count = result[2] + 1
            cursor.execute("UPDATE midman_information SET vouch_count = %s WHERE midman_user = %s", (vouch_count, result[1]))
            db_config.commit()

            log_channel = bot.get_channel(1221858628173041674)
            embed = discord.Embed(title="Vouch Logged",
                                  description=f"{message.author.mention} has given a vouch to {mentioned_user.mention} | `{vouch_count}` vouches",
                                  color=0x2b2d31)
            embed.set_thumbnail(url=thumbnail)
            embed.set_footer(text="Lunar Services")
            await log_channel.send(embed=embed)
        else:
            await message.delete()

    await bot.process_commands(message)
discord.py
1个回答
0
投票

这里的事情是

@bot.on_message

只是 Discord 的 api gateway message create event 的包装器,当发送机器人所在的任何服务器中的消息时,discord 总是发送该事件,因此仅从一个通道读取消息的唯一方法是你做了什么:

 if message.channel.id != 1221858607000326186:
    return

所以,不,除了检查消息通道 ID 之外没有其他办法。

© www.soinside.com 2019 - 2024. All rights reserved.