這篇會補全功能上的限制或處理,
像是輸到誰就只能誰來回答,
與錯誤處理,
或是回答過的不可以再使用。
if interaction.user.id != self.author.id:
await interaction.response.send_message("你不是發起者喔w", ephemeral=True)
for child in self.children:
if type(child) == discord.ui.Button:
child.disabled = True
await interaction.response.edit_message(view=self)
if interaction.user.id != setting.playerList[setting.round]:
return await interaction.response.send_message(f"還沒輪到你喔w", ephemeral=True)
try: playerInput = int(playerInput)
except: return await interaction.response.send_message(f"請輸入 {setting.minVal + 1}~{setting.maxVal} 的整數", ephemeral=True)
if playerInput < setting.minVal or playerInput > setting.maxVal:
return await interaction.response.send_message(f"請輸入 {setting.minVal + 1}~{setting.maxVal} 的整數", ephemeral=True)
import random
import discord
from discord.commands import Option
def main(Bot):
name = "終極密碼"
print(f"{name} 註冊成功")
class GameNumberView(discord.ui.View):
def __init__(self, author, maxValue, client):
super().__init__()
self.client = client
self.answer = random.randint(1, maxValue)
self.author = author
self.playerList = []
self.round = 0
self.minVal = 1
self.maxVal = maxValue
self.playerList.append(author.id)
class PlayerStart(discord.ui.View):
def __init__(playerSelf, setting):
super().__init__()
playerSelf.setting = setting
@discord.ui.button(label="輸入猜測數字", style=discord.ButtonStyle.success)
async def player_start_callback(playerSelf, button, interaction):
setting = playerSelf.setting
await interaction.response.send_modal(setting.NumberGuess(title=f"從 {setting.minVal}~{setting.maxVal} 選一個整數", setting=setting))
class NumberGuess(discord.ui.Modal):
def __init__(modalSelf, title, setting):
super().__init__(title=title)
modalSelf.setting = setting
modalSelf.add_item(discord.ui.InputText(label="整數數字", required=True))
async def callback(modalSelf, interaction):
setting = modalSelf.setting
playerInput = modalSelf.children[0].value
if interaction.user.id != setting.playerList[setting.round]:
return await interaction.response.send_message(f"還沒輪到你喔w", ephemeral=True)
try: playerInput = int(playerInput)
except: return await interaction.response.send_message(f"請輸入 {setting.minVal + 1}~{setting.maxVal} 的整數", ephemeral=True)
if playerInput < setting.minVal or playerInput > setting.maxVal:
return await interaction.response.send_message(f"請輸入 {setting.minVal + 1}~{setting.maxVal} 的整數", ephemeral=True)
if playerInput != setting.answer:
if playerInput < setting.answer:
setting.minVal = playerInput + 1
elif playerInput > setting.answer:
setting.maxVal = playerInput - 1
if (setting.round + 1) >= len(setting.playerList): setting.round = 0
else: setting.round += 1
await interaction.response.edit_message(view=None)
channel = setting.client.get_channel(interaction.channel_id)
await channel.send(f"<@{setting.playerList[setting.round]}> 開始猜,從 {setting.minVal}~{setting.maxVal} 選一個整數", view=setting.PlayerStart(setting))
else:
await interaction.response.send_message(f"BOOM!!! <@{interaction.user.id}> 中獎囉WWW")
@discord.ui.button(label="點擊參加", style=discord.ButtonStyle.primary)
async def button_callback(self, button, interaction):
try: self.playerList.index(interaction.user.id)
except: self.playerList.append(interaction.user.id)
message = f"<@{self.author.id}>已開啟了一場「終極密碼」\n 已參加 : "
for data in self.playerList: message += f"<@{data}> "
message += f"\n確認好人數後,發起者按下開始w"
await interaction.response.edit_message(content=message)
@discord.ui.button(label="開始", style=discord.ButtonStyle.success)
async def button_start_callback(self, button, interaction):
if interaction.user.id == self.author.id:
for child in self.children:
if type(child) == discord.ui.Button:
child.disabled = True
await interaction.response.edit_message(view=self)
channel = Bot.get_channel(interaction.channel_id)
await channel.send(f"<@{self.playerList[self.round]}> 開始猜,從 {self.minVal}~{self.maxVal} 選一個整數", view=self.PlayerStart(self))
else:
await interaction.response.send_message("你不是發起者喔w", ephemeral=True)
@Bot.slash_command(name=name, description="建立一個猜數字遊戲")
async def create_game(ctx, number: Option(int, "猜數字的最大範圍1~N", name="最大值", min_value=2, required=True)):
msg = f"<@{ctx.author.id}>已開啟了一場「終極密碼」\n 已參加 : <@{ctx.author.id}> \n確認好人數後,發起者按下開始w"
await ctx.send_response(msg, view=GameNumberView(ctx.author, number, Bot))