【问题标题】:How to use discord bot commands and event both?如何同时使用不和谐机器人命令和事件?
【发布时间】:2019-05-11 08:25:36
【问题描述】:

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

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

【问题讨论】:

标签: python-3.x bots discord discord.py


【解决方案1】:

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

【讨论】:

  • 您的答案应该详细,因为它没有适当的理由为什么应该这样。
【解决方案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)

【讨论】:

  • 我试过了,但它不起作用..也许我的代码有误......你能用我的例子写下它吗?
  • 我添加了示例,但我不完全确定您想对消息中的嵌入做什么。
猜你喜欢
  • 2018-11-10
  • 2018-07-21
  • 2017-05-31
  • 2020-03-05
  • 2021-10-21
  • 2022-01-15
  • 2021-06-17
  • 1970-01-01
  • 2021-03-28
相关资源
最近更新 更多