猜谜游戏,discord.py 机器人

Guessing game, discord.py bot(猜谜游戏,discord.py 机器人)

本文介绍了猜谜游戏,discord.py 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个不和谐的机器人猜谜游戏(使用 python),但它一直失败.我查看了许多类似的帖子,但没有发现任何帮助.

Im trying to make a discord bot guessing game (using python) but it keeps failing. I have looked at many similar posts and haven't found anything that helps.

@bot.command(name='play')
async def play(ctx):
  number = random.randint(1,100)
  await ctx.send('I have a number in mind between 1 and 100, guess')
  for i in range(0,5):
    guess = await client.wait_for('message') **(see below the code)
        if guess.content == number:
          await ctx.send('You got it!')
        elif guess.content < number:
          await ctx.send('Higher!')
        elif guess.content > number:
          await ctx.send('Lower!')
      else:
        await ctx.send("You lost, type $play to play again.")

** 通常它会说: , check=check,但这似乎不起作用,它说,检查未定义或类似的东西

** normaly it would say: , check=check, but that doesnt seem to work, it says, check not defined or something like that

代码似乎卡在:for i in range(0,5):,或:guess = await client.wait_for('message')

有人可以帮忙吗?或者发布一个有效的猜谜游戏+解释?

could someone help?, or post a guessing game that works + explanation?

推荐答案

正如评论中提到的,你需要包含你自己的check.如下所示:

As mentioned in the comments, you need to include your own check. This looks like the following:

    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel

这确保只有来自命令作者的消息被识别和接受.我们将其包含在代码中.

This ensures that only the message from the author of the command is recognized and accepted. We include this in the code.

另外,你不能使用 </>number,你会得到如下错误:

Also, you can't use </> number, you will get the following error:

TypeError: '<' not supported between instances of 'str' and 'int'.

要解决这个问题,只需将 number 更改为 str:

To fix this, simply change number to a str:

</> str(number)

可能的代码是:

@bot.command(name='play')
async def play(ctx):
    def check(m):
        return m.author == ctx.author and m.channel == ctx.message.channel

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

    for i in range(0, 5):
        guess = await bot.wait_for('message', check=check)

        if guess.content == number:
            await ctx.send('You got it!')

        elif guess.content < str(number):
            await ctx.send('Higher!')

        elif guess.content > str(number):
            await ctx.send('Lower!')

        else:
            return # Or something else
        
    else:
        await ctx.send("You lost, type $play to play again.")

这篇关于猜谜游戏,discord.py 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:猜谜游戏,discord.py 机器人