Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

discord.py - How to make bot respond to a certain msg?

So, I've been trying to make my bot respond to a specific keyword however when I do this the bot either doesn't respond or gives me a bunch of errors. I've tried a few methods of doing this but I didn't have much luck. If anybody can make it work would be glad here are one of the methods I tried using.

    if message.content == "keyword":
    await client.send_message(message.channel, "Response {0.author.mention}")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do this one of two ways, either with the commands extension, or within the on_message event.

Below is an example of how you can do this. With this example, when a user types "!ping" the bot will respond with "Pong". If a message contains the word "foo", then the bot will respond with "bar".

from discord.ext import commands

client = commands.Bot(command_prefix='!')

@client.event
async def on_ready():
    print('client ready')

@client.command()
async def ping():
    await client.say('Pong')

@client.event
async def on_message(message):
    if client.user.id != message.author.id:
        if 'foo' in message.content:
            await client.send_message(message.channel, 'bar')

    await client.process_commands(message)

client.run('TOKEN')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...