简体   繁体   中英

How do I trigger my bot to DM a user after an on_message event discord.py

I'm trying to make my bot message a user directly if they type a certain word, for example the letter 'E', but I can't figure out how to do this. Any help is appreciated!

I'm guessing this is what you mean:

@bot.event
async def on_message(message):
   if(message.content == 'E'):
      await message.author.send('You typed E!')

Here few points to note that:
message.channel.send(<message>) function is used for public responses like on sever. message.author.send(<message>) function is used for private responses or for direct message(DM).

@bot.event
async def on_message(message):
   if(message.content == 'E'):
      await message.author.send('You typed E!')`enter code here`

You can use the Member.create_dm() method

@bot.event
async def on_message(message):
    if message.content.lower() == "e":
        dmchannel = await message.author.create_dm()
        await dmchannel.send("You typed E!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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