Home > other >  If Discord.py Bot gets dm
If Discord.py Bot gets dm

Time:03-06

Dear stackoverflow community. I want to add a little 'Easter egg' to my discord.py bot. If one gets a DM from a user, he should reply something like "No private messages while at work". Is that somehow possible? Unfortunately, I have no idea how to do this. That's why I can't attach code. Please help me : )

CodePudding user response:

You can have an on_message event, which will fire each time there is any new message which your bot can read. 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. (Could again be client instead of bot for you)

References:

  • Related