乱数推測ゲームを起動するコマンドを作成しようとしています。明らかに、私は最初の数行で立ち往生しています。私は自分がうまくいくと思うものを書きましたが、それは明らかに間違っているかもしれません。Discordサーバー上のメッセージをintに変更したいので、ifステートメントで機能します。
discord.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サーバーでコマンドを実行するたびに、シェルで次のエラーが発生します。
discord.ext.commands.errors.CommandInvokeError:コマンドで例外が発生しました:TypeError:int()引数は、「メッセージ」ではなく、文字列、バイトのようなオブジェクト、または数値である必要があります
私はそれが私に何を言っているのかよくわかりません。前述のように、「メッセージ」を整数にしたいのですが、エラーが発生します。しかし、助けていただければ幸いです![まだ初心者ですが、厳しすぎないでください:(]
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")
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加