如何同时使用discord bot命令和事件?

M. *_* T. 4 bots python-3.x discord discord.py

我需要制作一个机器人来侦听服务器中写入的消息,同时接受命令。

# Create the Discord client
client = discord.Client()
client = commands.Bot(command_prefix = '!')
client.remove_command('help')

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


@client.event                                               #ricerca messaggi 
async def on_message(message):
    # Ignore messages made by the bot
    if(message.author == client.user):
        return
    a = ''
    a += message.embeds[0]["description"]
    if a == 'abcdef':
        print(' aaaaa ')



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


client.run('token')
Run Code Online (Sandbox Code Playgroud)

我怎样才能让它工作?我认为问题在于机器人在第一个事件中继续循环……我阅读了 sub_process 但我不明白如何使用它。

M.v*_*aal 7

您需要在 on_message 的末尾添加 process_commands()。这是因为覆盖默认的 on_message 会禁止命令运行

@client.event                                               #ricerca messaggi 
async def on_message(message):
    # Ignore messages made by the bot
    if(message.author == client.user):
        return
    a = ''
    a += message.embeds[0]["description"]
    if a == 'abcdef':
        await message.channel.send(' aaaaa ')
    await client.process_commands(message)
Run Code Online (Sandbox Code Playgroud)