Guessing game, discord.py bot(猜谜游戏,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 机器人
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01