简体   繁体   中英

(discord.py) How can I make my bot randomly reply to DMs?

For example, when someone DMs my bot, it says "hello" or "why are you trying to DM a bot..?" I tried this with this code:

@client.event
async def on_message(message: discord.Message):
    if message.guild is None and not message.author.bot:
        with open('dmresponses.txt') as input_file:
            long_list = [line.strip() for line in input_file]
        await message.author.send(random.choice(long_list))

and it worked. But, my commands like "m.help" and "m?about" stopped working. What is the proper way of doing this?

When you add an on_message event, you need to process the commands:

@client.event
async def on_message(message): # no need to define the type
    await client.process_commands(message)
    # rest of the code here

References:

  • Bot.process_commands() - "Without this coroutine, none of the commands will be triggered. If you choose to override the on_message() event, then you should invoke this coroutine as well."
  • on_message

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