Discord.py - 机器人没有响应

时间:2021-05-30 16:24:59

标签: python discord discord.py bots

我正在 Python 上构建一个 Discord 机器人,但在代码中遇到了问题。 这是我的完整代码:

import discord
from discord import message
from discord.ext import commands

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

gret_words = ['hi', 'grets', 'greetings', 'mornin', 'hey']

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.command(pass_context=True)
async def hello(ctx):
    author = ctx.message.author
    await ctx.send(f'Hello, {author.mention}! My name is Bot-3P0!')

async def on_message(message):
    msg = message.content.lower()

    if msg in gret_words:
        await message.channel.send("Nice to see you!")

####################


client.run('TOKEN')

但我的问题是,当我从 gret_words 列表中输入 Messenger 一个词时,机器人实际上没有反应!我将不胜感激!

1 个答案:

答案 0 :(得分:2)

您需要将 on_message 标记为事件。只需在 @client.event 之上添加 async def on_message(message) 就可以了!编辑:您还需要将 client.process_commands() 添加到您的 on_message()