如何同时使用不和谐的bot命令和事件?

时间:2018-12-10 12:25:58

标签: python-3.x bots 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')

我如何使其起作用? 我认为问题在于该机器人在第一次事件中会继续循环... 我读到有关sub_process的信息,但我不知道如何使用它。

2 个答案:

答案 0 :(得分:2)

您将需要在on_message的末尾添加process_commands()。 This is because overriding the default on_message forbids commands from running

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

答案 1 :(得分:-1)

而不是 @client.event() 只需执行 @client.listen() 并且它应该可以工作并删除 client = discord.Client()