gpt4 book ai didi

discord.py - 用 discord.py 重写的数字猜谜游戏

转载 作者:行者123 更新时间:2023-12-04 04:08:56 27 4
gpt4 key购买 nike

我试过这段代码,机器人说猜但没有回应我的猜测。

@commands.command()
async def game(self, ctx):
number = random.randint(0, 100)
for guess in range(0, 5):
await ctx.send('guess')
Message = await bot.wait_for('message')
Message = int(Message)
if Message.cleant_content > number:
await ctx.send('bigger')
elif Message.cleant_content < number:
await ctx.send('smaller')
else:
await ctx.send('true')

最佳答案

因为您使用的是 cogs,所以我想您已经将机器人初始化为 self.bot而不仅仅是 bot .

此外,您正在将消息对象 转换为整数,并尝试访问名为cleant_content 的整数的属性。 .

你的代码应该是这样的:

@commands.command()
async def game(self, ctx):
number = random.randint(0, 100)
for i in range(0, 5):
await ctx.send('guess')
response = await self.bot.wait_for('message')
guess = int(response.content)
if guess > number:
await ctx.send('bigger')
elif guess < number:
await ctx.send('smaller')
else:
await ctx.send('true')

为了便于阅读,我还更改了一些变量名。如果需要,您可以添加一些检查来检查他们的猜测是否真的是一个数字。

此外,我更改了原意为 clean_content 的内容至 content相反,作为 clean_content完全没有它的目的,因为你不能转换 < , @ , # , ! , >等到一个整数,这意味着无论哪种方式都会出错。我希望这是有道理的。


引用资料:

关于discord.py - 用 discord.py 重写的数字猜谜游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62075804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com