【问题标题】:discord.py - Guessing game no response (updated)discord.py - 猜谜游戏无响应(更新)
【发布时间】:2021-08-02 14:55:16
【问题描述】:

当我在我的 discord 服务器上键入 $play 时,以下代码应该会运行,但当我运行它时没有任何反应..

client = commands.Bot(command_prefix='$')

@client.command(name="play")
async def play(ctx):
  def check(m):
    return m.author == ctx.author and m.channel == ctx.message.channel and m.content.isdigit()

  number = random.randint(1,100)
  await ctx.send('I have a number in mind between 1 and 100, guess it')

  for i in range(0,5):
    guess = await client.wait_for("message", check=check)
    
    if int(guess.content) > number:
      await ctx.send("The number is greater")
    elif int(guess.content) < number:
      await ctx.send("The number is smaller")
    elif int(guess.content) == number:
      await ctx.send("You guessed the number!!.")
    else:
      return ("It has to be a positive integer between 1 to 100")
  else:
    await ctx.send("You lost, type $play to play again.")

【问题讨论】:

  • 您是否导入了以下内容? from discord.ext import commands
  • @Dominik 我有;这是entirety of the code;猜谜游戏从第 45 行开始
  • 你能更深入地解释发生了什么吗?当我复制你的代码时它工作正常,除了它总是告诉我输入 1000000 时数字更大
  • @loloToster 你输入$play 来运行代码了吗?当我在我的 Discord 服务器的频道上键入 $play 时,什么也没有发生。也没有错误。我记得当我弄乱代码时它确实运行了一次,同样的事情发生了;它总是表现得更好。
  • 它显示得更大,因为你搞砸了 &gt;&lt; 标志它们应该被反转。关于不响应,您可以将print("test") 放在play 命令的顶部以检查它是否甚至响应$play

标签: python discord discord.py


【解决方案1】:

您的 on_message 事件似乎正在阻止命令工作。

将以下内容添加到您的 on_message 事件中:

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  # Your next events

    await client.process_commands(message) # Process commands
  • 我们添加这个是因为您覆盖了正常的on_message 事件。
  • process_commands 确保 commands 被识别。

更多信息请见docs

【讨论】:

  • 终于成功了!其他命令仍然有效,但由于某种原因,当我尝试运行它们时,它会在控制台上显示这一点,即使一切仍然正常工作:``discord.ext.commands.errors.CommandNotFound: Command "hello" is not found(当我使用 $hello 时)
  • 问题是关于为什么commands 不起作用,我给出了一个有效的答案。我建议您为此提出一个新问题或自己找到解决方案。不久前我对完全相同的代码做了一个回答:stackoverflow.com/questions/67361326/…
猜你喜欢
  • 2021-08-01
  • 2021-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-15
  • 2020-11-22
  • 2014-05-12
相关资源
最近更新 更多