python - (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 消息。
解决方案
您可以尝试设置机器人意图,但请记住这些会影响整个机器人,而不仅仅是一个命令。
...
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
...
推荐阅读
- javascript - 在数组javascript中查找最小索引
- xcode - 使用 fastlane 时 Xcode 找不到任何配置文件
- oracle - 为什么它说 ORA-14006:无效的分区名称?
- c - 使用 while 循环跟踪最大输入值
- ios - 如何在 iOS 中制作动画材料设计 TextField
- html - 图像在桌面浏览器上具有正确的纵横比,但在移动浏览器上具有错误的纵横比
- jquery - 交互式块引导
- python - 生成多个变量的函数的分组依据
- python - Discord.py Raspberry Pi 线程问题
- html - 使 HTML 更加模块化的简单解决方案