不和.py重写|我怎么把它变成一个整数?

2024-09-30 16:26:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试做一个命令,激活一个随机数猜谜游戏。很明显,我被困在前几行了。我写了我认为可行的东西,但它可能是明显错误的。我希望它将discord服务器上的消息更改为int,这样它就可以在if语句中工作。在

这是我第一次做机器人不和.py,所以我遇到了很多障碍。我不能完全确定错误在告诉我什么,所以我无法尝试任何修复。代码如下:

async def numgame(context):
    number = random.randint(1,100)
    for guess in range(0,5):
        await context.send('Pick a number between 1 and 100')
        Message = await client.wait_for('message')
        Message = int(Message)
        if Message.cleant_content > number:
            await context.send(guess + ' guesses left...')
            asyncio.sleep(1)
            await context.send('Try going lower')
            asyncio.sleep(1)
       elif Message.clean_content < number:
            await context.send(guess + ' guesses left...')
            asyncio.sleep(1)
            await context.send('Try going higher')
            asyncio.sleep(1)
        else:
            await context.send('You guessed it! Good job!')
    if number != Message:
        await context.send('Tough luck!')

每当我在discord服务器中执行命令时,shell都会显示以下错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: int() argument must be a string, a bytes-like object or a number, not 'Message'

我不太清楚它在告诉我什么。如前所述,我希望“Message”是一个整数,但是我得到了错误。但我们会很感激你的帮助! [还是初学者,不要太苛刻:(]


Tags: 服务器sendasyncionumbermessageforif错误
1条回答
网友
1楼 · 发布于 2024-09-30 16:26:21

wait_for('message')返回一个Message对象,int还不知道如何处理。您需要将Message.content转换为int。下面是您的代码以及一些其他更改:

def check(message):
    try:
        int(message.content)
        return True
    except ValueError:
        return False

@bot.command()
async def numgame(context):
    number = random.randint(1,100)
    for guess in range(0,5):
        await context.send('Pick a number between 1 and 100')
        msg = await client.wait_for('message', check=check)
        attempt = int(msg.content)
        if attempt > number:
            await context.send(str(guess) + ' guesses left...')
            await asyncio.sleep(1)
            await context.send('Try going lower')
            await asyncio.sleep(1)
       elif attempt < number:
            await context.send(str(guess) + ' guesses left...')
            await asyncio.sleep(1)
            await context.send('Try going higher')
            await asyncio.sleep(1)
        else:
            await context.send('You guessed it! Good job!')
            break 
    else:
        await context.send("You didn't get it")

相关问题 更多 >