如果 Discord.py Bot 收到 dm
If Discord.py Bot gets dm
尊敬的 Whosebug 社区。
我想向我的 discord.py 机器人添加一点 'Easter egg'。如果一个人从用户那里得到一个 DM,他应该回复类似“工作时没有私人信息”这样的话。这有可能吗?不幸的是,我不知道该怎么做。这就是为什么我无法附加代码。请帮助我:)
您可以有一个 on_message
事件,每当您的机器人可以读取任何新消息时都会触发该事件。然后你可以检查消息是否是直接消息:
@bot.event # could be client instead of bot for you
async def on_message(message):
if message.author == bot.user: # Don't reply to itself. Could again be client for you
return
if isinstance(message.channel,discord.DMChannel): #If you want this to work in a group channel, you could also check for discord.GroupChannel
await message.channel.send("No private messages while at work")
await bot.process_commands(message)
另外不要忘记添加 bot.process_commands(message)
添加 on_message
的末尾,这样您的命令仍然有效。 (可以再次成为客户端而不是机器人)
参考文献:
尊敬的 Whosebug 社区。 我想向我的 discord.py 机器人添加一点 'Easter egg'。如果一个人从用户那里得到一个 DM,他应该回复类似“工作时没有私人信息”这样的话。这有可能吗?不幸的是,我不知道该怎么做。这就是为什么我无法附加代码。请帮助我:)
您可以有一个 on_message
事件,每当您的机器人可以读取任何新消息时都会触发该事件。然后你可以检查消息是否是直接消息:
@bot.event # could be client instead of bot for you
async def on_message(message):
if message.author == bot.user: # Don't reply to itself. Could again be client for you
return
if isinstance(message.channel,discord.DMChannel): #If you want this to work in a group channel, you could also check for discord.GroupChannel
await message.channel.send("No private messages while at work")
await bot.process_commands(message)
另外不要忘记添加 bot.process_commands(message)
添加 on_message
的末尾,这样您的命令仍然有效。 (可以再次成为客户端而不是机器人)