如何让机器人响应某个消息?

时间:2018-05-07 13:56:45

标签: discord.py

所以,我一直在尝试让我的机器人响应特定的关键字但是当我这样做时,机器人要么没有响应,要么给我一堆错误。我尝试了一些方法,但我运气不好。如果任何人能够使它工作将很高兴这是我尝试使用的方法之一。

    if message.content == "keyword":
    await client.send_message(message.channel, "Response {0.author.mention}")

1 个答案:

答案 0 :(得分:0)

您可以使用命令扩展名或on_message事件中的两种方式执行此操作。

以下是如何执行此操作的示例。在这个例子中,当用户键入“!ping”时,机器人将以“Pong”响应。如果消息包含单词“foo”,则机器人将以“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')