229 lines
9.2 KiB
Python
229 lines
9.2 KiB
Python
import os
|
||
import discord
|
||
from discord.ext import commands
|
||
from discord import app_commands
|
||
from typing import Literal, Optional
|
||
|
||
import asyncio
|
||
|
||
from func import signin
|
||
from func import setup
|
||
from func import rank
|
||
from func import lottery
|
||
from func.setup import get_signin_setup_view
|
||
from data import load, save, create
|
||
|
||
intents = discord.Intents.default()
|
||
intents.messages = True
|
||
intents.message_content = True
|
||
intents.guilds = True
|
||
intents.members = True
|
||
|
||
bot = commands.Bot(command_prefix="/", intents=intents)
|
||
|
||
DEV_ID = 1365846048144228392
|
||
|
||
@bot.event
|
||
async def on_ready():
|
||
bot.tree.add_command(LotteryCommandGroup())
|
||
await bot.tree.sync()
|
||
print(f"Logged in as {bot.user}")
|
||
channel = bot.get_channel(DEV_ID)
|
||
|
||
if channel:
|
||
await channel.send("遊戲機已啟動,版本V2.7")
|
||
|
||
@bot.event
|
||
async def on_message(message: discord.Message):
|
||
if message.author.bot:
|
||
return
|
||
await signin.handle_signin(bot, message)
|
||
await bot.process_commands(message)
|
||
|
||
@bot.tree.command(name="查看", description="查看個人資訊")
|
||
@app_commands.describe(類型="選擇查看資訊")
|
||
@app_commands.choices(類型=[
|
||
app_commands.Choice(name="簽到次數", value="signin_count"),
|
||
app_commands.Choice(name="代幣個數", value="coin_count"),
|
||
app_commands.Choice(name="上次簽到", value="last_signin"),
|
||
app_commands.Choice(name="經驗值", value="exp_count")
|
||
])
|
||
async def 查看(interaction: discord.Interaction, 類型: app_commands.Choice[str]):
|
||
user_id = str(interaction.user.id)
|
||
user = interaction.user
|
||
if 類型.value == "signin_count":
|
||
count = load.get_signin_count(user_id)
|
||
if count == 0:
|
||
await interaction.response.send_message(f" # {user.mention} 沒有簽到過!!\n請先使用簽到")
|
||
else:
|
||
await interaction.response.send_message(f" {user.mention} 簽到了 {count} 次")
|
||
elif 類型.value == "coin_count":
|
||
coins = load.get_coin_count(user_id)
|
||
if coins == 0:
|
||
await interaction.response.send_message(f"# {user.mention} 沒有代幣!!\n請先使用簽到")
|
||
else:
|
||
await interaction.response.send_message(f"{user.mention} 擁有 {coins} 枚代幣")
|
||
elif 類型.value == "exp_count":
|
||
exp = load.get_exp_count(user_id)
|
||
if exp == 0:
|
||
await interaction.response.send_message(f"# {user.mention} 沒有經驗值!!\n請先使用簽到")
|
||
else:
|
||
await interaction.response.send_message(f"{user.mention} 有 {exp} 經驗值")
|
||
elif 類型.value == "last_signin":
|
||
signin_time = load.get_signin_time(user_id)
|
||
if signin_time == "no signin data":
|
||
await interaction.response.send_message(f" # {user.mention} 沒有簽到過!!\n請先使用簽到")
|
||
else:
|
||
await interaction.response.send_message(f"{user.mention} 上次簽到日期為 {signin_time}")
|
||
|
||
@bot.tree.command(name="設定", description="設定")
|
||
@app_commands.describe(
|
||
簽到頻道="輸入簽到頻道的ID,要偵測所有頻道請打\"default\"",
|
||
簽到表符="輸入簽到表符,如 🪙 或自訂表符"
|
||
)
|
||
async def 設定(
|
||
interaction: discord.Interaction,
|
||
簽到頻道: str,
|
||
簽到表符: str
|
||
):
|
||
if not interaction.user.guild_permissions.administrator:
|
||
await interaction.response.send_message("只有管理員能使用此指令", ephemeral=True)
|
||
return
|
||
|
||
if not 簽到頻道.isdigit():
|
||
await interaction.response.send_message("頻道ID格式錯誤,請輸入純數字頻道ID。", ephemeral=True)
|
||
return
|
||
|
||
guild_id = interaction.guild.id
|
||
|
||
current = load.load_server_setting(guild_id)
|
||
if not current:
|
||
create.create_server_setting(guild_id)
|
||
|
||
new_data = {
|
||
"Server_ID": guild_id,
|
||
"signin_channel": 簽到頻道,
|
||
"signin_emoji": 簽到表符
|
||
}
|
||
save.save_server_setting(new_data)
|
||
|
||
channel = interaction.guild.get_channel(int(簽到頻道))
|
||
channel_mention = channel.mention if channel else f"<簽到頻道 {簽到頻道}>"
|
||
|
||
await interaction.response.send_message(
|
||
f"已更新簽到設定:\n頻道 {channel_mention}\n表符:{簽到表符}",
|
||
ephemeral=True
|
||
)
|
||
|
||
@bot.tree.command(name="排行榜", description="查看排行榜")
|
||
@app_commands.describe(類型="選擇排行榜類型")
|
||
@app_commands.choices(類型=[
|
||
app_commands.Choice(name="簽到-本伺服器", value="signin_server"),
|
||
app_commands.Choice(name="簽到-全國", value="signin_all"),
|
||
app_commands.Choice(name="經驗-本伺服", value="exp_server"),
|
||
app_commands.Choice(name="經驗-全國", value="exp_all"),
|
||
])
|
||
async def rank_command(interaction: discord.Interaction, 類型: app_commands.Choice[str]):
|
||
await interaction.response.defer()
|
||
|
||
match 類型.value:
|
||
case "signin_server":
|
||
result = await rank.sort_signin_server(interaction.guild)
|
||
case "exp_server":
|
||
result = await rank.sort_exp_server(interaction.guild)
|
||
case "signin_all":
|
||
result = await rank.sort_signin_all(bot)
|
||
case "exp_all":
|
||
result = await rank.sort_exp_all(bot)
|
||
|
||
await interaction.followup.send(f"**{類型.name} 排行榜**\n```{result}```")
|
||
|
||
# lottery
|
||
class LotteryCommandGroup(app_commands.Group):
|
||
def __init__(self):
|
||
super().__init__(name="抽獎", description="進行抽獎")
|
||
|
||
@app_commands.command(name="御神籤", description="抽一次御神籤,消耗10代幣")
|
||
async def omikuji(self, interaction: discord.Interaction):
|
||
user_id = str(interaction.user.id)
|
||
|
||
result = lottery.omikuji(user_id)
|
||
if result is None:
|
||
await interaction.response.send_message("代幣不足,無法抽取御神籤。", ephemeral=True)
|
||
else:
|
||
await interaction.response.send_message(f"{interaction.user.mention} 抽中了 **{result[0]}**,獲得了 **{result[1]}** exp!")
|
||
|
||
|
||
@app_commands.command(name="幸運礦工", description="從指定礦池進行抽獎,消耗1代幣")
|
||
@app_commands.describe(礦池="選擇礦池", 次數="抽幾次")
|
||
@app_commands.choices(礦池=[
|
||
app_commands.Choice(name="小型礦池", value="small"),
|
||
app_commands.Choice(name="大型礦池", value="large"),
|
||
app_commands.Choice(name="賭狗模式", value="risk")
|
||
])
|
||
async def miner(self, interaction: discord.Interaction, 礦池: app_commands.Choice[str], 次數: int = 1):
|
||
user_id = str(interaction.user.id)
|
||
|
||
if 次數 > 500 or 次數 <= 0:
|
||
await interaction.response.send_message("次數必須介於 1 到 500 之間。", ephemeral=True)
|
||
return
|
||
|
||
result = lottery.miner(user_id, 礦池.value, 次數)
|
||
if result is None:
|
||
await interaction.response.send_message("代幣不足,無法幸運礦工。", ephemeral=True)
|
||
else:
|
||
total_exp, log = result
|
||
log_str = ", ".join(str(x) for x in log)
|
||
|
||
await interaction.response.send_message(
|
||
f"{interaction.user.mention} 進行了 {次數} 次 **{礦池.name}** 抽獎\n"
|
||
f"總共獲得:**{total_exp} exp**\n"
|
||
f"抽獎結果如下:\n### {log_str}"
|
||
)
|
||
|
||
@app_commands.command(name="扭蛋", description="從指定扭蛋池進行抽獎,消耗3代幣")
|
||
@app_commands.describe(卡池="選擇卡池")
|
||
@app_commands.choices(卡池=[
|
||
app_commands.Choice(name="作者池", value="作者池")
|
||
])
|
||
async def gacha(self, interaction: discord.Interaction, 卡池: app_commands.Choice[str]):
|
||
user_id = str(interaction.user.id)
|
||
|
||
result = lottery.gacha(user_id, 卡池.value)
|
||
if result is None:
|
||
await interaction.response.send_message("代幣不足或參數錯誤。", ephemeral=True)
|
||
else:
|
||
await interaction.response.send_message(
|
||
f"{interaction.user.mention} 抽取了一次 {卡池.name} 扭蛋,結果如下:\n{result}"
|
||
)
|
||
@app_commands.command(name="刮刮樂", description="玩一次刮刮樂,消耗5代幣")
|
||
@app_commands.describe(卡池="選擇模式")
|
||
@app_commands.choices(卡池=[
|
||
app_commands.Choice(name="模式一", value="模式一"),
|
||
app_commands.Choice(name="模式二", value="模式二")
|
||
])
|
||
async def scratch(self, interaction: discord.Interaction, 卡池: app_commands.Choice[str]):
|
||
if 卡池.value == "模式二":
|
||
await interaction.response.send_message("模式二尚未實裝,敬請期待!", ephemeral=True)
|
||
return
|
||
|
||
# 模式一邏輯
|
||
result = lottery.scratch(interaction.user.id)
|
||
if result is None:
|
||
await interaction.response.send_message("你的代幣不足,無法玩刮刮樂!", ephemeral=True)
|
||
return
|
||
|
||
answer_line = "# ||" + " ".join(str(n) for n in result["opponent"]) + "||"
|
||
rows = [
|
||
"# ||" + " ".join(str(n) for n in result["player"][i:i+3]) + "||"
|
||
for i in range(0, 9, 3)
|
||
]
|
||
scratch_board = "\n".join(["對手的號碼:",answer_line,"您的號碼:"] + rows)
|
||
|
||
|
||
await interaction.response.send_message(f"刮刮樂開始!祝你好運~\n{scratch_board}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
TOKEN = os.getenv("DISCORD_BOT_TOKEN")
|
||
bot.run(TOKEN) |