简体   繁体   English

如果 Discord.py Bot 得到 dm

[英]If Discord.py Bot gets dm

Dear stackoverflow community.亲爱的 stackoverflow 社区。 I want to add a little 'Easter egg' to my discord.py bot.我想为我的 discord.py 机器人添加一点“复活节彩蛋”。 If one gets a DM from a user, he should reply something like "No private messages while at work".如果一个人从用户那里得到一个 DM,他应该回复类似“工作时没有私人信息”这样的话。 Is that somehow possible?这有可能吗? Unfortunately, I have no idea how to do this.不幸的是,我不知道该怎么做。 That's why I can't attach code.这就是为什么我无法附加代码。 Please help me: )请帮我: )

You can have an on_message event, which will fire each time there is any new message which your bot can read.您可以有一个on_message事件,每当您的机器人可以读取任何新消息时,该事件就会触发。 Then you can check if the message is a direct 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)

Also don't forget to add bot.process_commands(message) add the end of on_message so your commands still work.另外不要忘记添加bot.process_commands(message)添加on_message的末尾,这样您的命令仍然有效。 (Could again be client instead of bot for you) (可以再次成为客户而不是机器人)

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM