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

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...