【问题标题】:Commands don't work. They used to work but they don't any more命令不起作用。他们曾经工作,但现在不再工作了
【发布时间】:2022-01-26 14:29:35
【问题描述】:

我添加了机器人状态,然后 命令不起作用。添加了一个回答者的答案,但它仍然不起作用(帮助不起作用但不是你好;-;)

import discord
from KeepAlive import keep_alive

client=discord.Client()

@client.event
async def on_ready():
  await client.change_presence(status=discord.Status.online,activity=discord.Game('Hey There! Do €help to start!'))
  print('We have logged in as {0.user}'.format(discord.Client))

@client.event
async def on_message(message):
  if message.author == client.user:
        return
  if message.content.startswith('$hello'):
    await message.channel.send('Hello!')
    
  if message.content.startswith('$help'):
    await message.channel.send('no help here!')

  await bot.process_commands(message)

   

keep_alive()
client.run('wont say token :)')

【问题讨论】:

  • 你在说什么?您似乎没有像他们在答案中显示的那样添加下划线。
  • 您可以尝试启用intents.message
  • 现在可以使用了。我如何回答问题?
  • @TechGamerExpert 只需单击旁边的复选标记,即可回答解决了您的问题或帮助您解决问题。

标签: python discord


【解决方案1】:

如果您谈论的是commands 而不是使用on_message 运行的“命令”,那么您必须添加await client.process_commands(message) (check this issue in documentation)。如果您的on_message 事件不起作用,那可能只是因为on_message 事件中缺少_

@client.event
async def on_message(message): # your forgot "_"
    if message.author == client.user: # discord.Client won't work. Use client.user instead
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')
    if message.content.startswith('$help'):
        await message.channel.send('no help here!')

    await client.process_commands(message) # add this line at the end of your on_message event

【讨论】:

    【解决方案2】:

    问题是你的消息功能,它有async def onmessage(message):,但正确的是:

    @client.event
    async def on_message(message):
    

    我建议定义前缀,然后将其与消息分开,这样您就不必在每个 if 中继续输入 $,并保存在命令之后编写的元素以供将来使用:

    PREFIX = "$"
    @client.event
    async def on_message(message):
        msg = message.content[1:].split(' ')
        command = msg[0]
        if command == "hello":
            await message.channel.send('Hello!')
    
    

    【讨论】:

      猜你喜欢
      • 2015-01-25
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 2021-11-04
      相关资源
      最近更新 更多