亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我發送號碼時,discord.py 機器人沒有響應。猜數字游戲的一部分

當我發送號碼時,discord.py 機器人沒有響應。猜數字游戲的一部分

智慧大石 2024-01-04 09:47:11
我需要有關 Discord.py 的幫助我嘗試創建一個猜謎游戲。機器人會創建一個 1 到 20(包括 20)之間的隨機數,然后您嘗試在 6 次或少于 6 次的時間內猜測它。當您發送號碼時,它會告訴您該號碼是否太高/太低。這是我的代碼:@client.command()async def guessnumber(ctx):    user = ctx.author    await ctx.send(f"Hello {user}! I'm thinking of a number between 1 and 20. You are given 6 tries to find the number. Good luck!")    secretNumber = random.randint(1,20)    for guessesTaken in range(1,7):        guess = int(input())        if guess < secretNumber:            await ctx.send("Your guess is too low")        elif guess > secretNumber:            await ctx.send("Your guess is too high")        else:            break        if guess == secretNumber:        await ctx.send(f"GG! You correctly guessed the number!")    else:        await ctx.send(f"Nope, sorry, you took to many guesses. The number I was thinking of was {secretNumber}")但是,當我發送命令時,它會發送開頭部分,但是當我發送數字時,它不會響應。
查看完整描述

1 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

該input()功能用于console輸入,而不是 Discord。要在 Discord 中等待消息,請使用client.wait_for():


message = await client.wait_for("message")

您還可以編寫一個check函數來檢查消息是否符合您的條件:


def checkfunction(message):

? ? return message.author == ctx.author and ctx.channel == message.channel and message.content.isdigit()?


message = await client.wait_for("message", check=checkfunction)

如果 check 函數返回 True,則代碼將繼續,否則它將等待另一條消息。

您現在可以將其實現到您的代碼中:


@client.command()

async def guessnumber(ctx):


? ? await ctx.send(f"Hello {ctx.author.name}! I'm thinking of a number between 1 and 20. You are given 6 tries to find the number. Good luck!")

? ? secretNumber = random.randint(1,20)


? ? def check(message):

? ? ? ? return message.author == ctx.author and message.channel == ctx.channel? and message.content.isdigit()


? ? for guessesTaken in range(6):


? ? ? ? guess = int((await client.wait_for('message', check=check)).content)


? ? ? ? if guess < secretNumber:

? ? ? ? ? ? await ctx.send("Your guess is too low")

? ? ? ? elif guess > secretNumber:

? ? ? ? ? ? await ctx.send("Your guess is too high")

? ? ? ? else:

? ? ? ? ? ? await ctx.send(f"GG! You correctly guessed the number in {guessesTaken + 1} guesses!")


? ? else:

? ? ? ? await ctx.send(f"Nope, sorry, you took too many guesses. The number I was thinking of was {secretNumber}")


查看完整回答
反對 回復 2024-01-04
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號