如何同时使用不和谐机器人命令和事件? [英] How to use discord bot commands and event both?

查看:28
本文介绍了如何同时使用不和谐机器人命令和事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to make a bot that listen for messages written in a server, and at the same time accept commands.

# 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 但我不明白如何使用它.

How can I make it works? I think the problem is that the bot continue cycling in the first event... I read about sub_process but I do not understand how to use it.

推荐答案

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

You will need to add process_commands() at the end of your on_message. 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)

这篇关于如何同时使用不和谐机器人命令和事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆