首页 > 解决方案 > (Discord.py) 让我的机器人只响应 DM

问题描述

所以我设法让我的 Bot 工作的基础知识很好。但是,当我尝试发送消息时,例如 execute /ping,无论命令在哪个 Channel 执行,包括 DM,机器人都会回复它!然后我编写了一个语句,使其仅响应服务器上某些 Discord 频道中的命令,这似乎确实有效。但是,当我尝试将 channels 数组更改为 时discord.DMChannel,这似乎不起作用。有人可以帮我解决这个问题吗?

channels = ["general"]
if str(message.channel) in channels:
    if message.content == "/ping":
        await message.channel.send("Pong")

但是,如果我将其更改为:

channels = [discord.DMChannel]
if str(message.channel) in channels:
    if message.content == "/ping":
        await message.channel.send("Pong")

机器人不响应 DMChannel 消息。

标签: pythondiscord.py

解决方案


您可以尝试设置机器人意图,但请记住这些会影响整个机器人,而不仅仅是一个命令。

...

my_intents = discord.Intents.default()
my_intents.guild_messages = False # turn off messages from guilds, so you only get messages from DM channels
# optionally turn on members or presences here if you need them
client = discord.Client(..., intents=my_intents) # or discord.Bot() or whatever

...

推荐阅读