fix bugs to 2.7.1
This commit is contained in:
1 parent
6747dbacb8
commit
7716f8c297
22 files changed
+639
-1018
No files matched your search
@@ -0,0 +1,80 @@
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import db
|
||||
from game import lottery
|
||||
from game.achievements import announce
|
||||
|
||||
NOT_ENOUGH = "代幣不足(或尚未簽到),無法抽獎。"
|
||||
|
||||
|
||||
class Lottery(commands.GroupCog, group_name="抽獎", group_description="進行抽獎"):
|
||||
async def cog_app_command_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError):
|
||||
if isinstance(error, app_commands.CommandInvokeError) and isinstance(error.original, db.NotEnoughCoins):
|
||||
await interaction.response.send_message(NOT_ENOUGH, ephemeral=True)
|
||||
else:
|
||||
raise error
|
||||
|
||||
@app_commands.command(name="御神籤", description=f"抽一次御神籤,消耗{lottery.OMIKUJI_COST}代幣")
|
||||
async def omikuji(self, interaction: discord.Interaction):
|
||||
name, exp = lottery.omikuji(interaction.user.id)
|
||||
await interaction.response.send_message(
|
||||
f"{interaction.user.mention} 抽中了 **{name}**,獲得了 **{exp}** exp!"
|
||||
)
|
||||
|
||||
@app_commands.command(name="幸運礦工", description="從指定礦池挖礦,每次消耗1代幣")
|
||||
@app_commands.describe(礦池="選擇礦池", 次數=f"挖幾次(1-{lottery.MINER_MAX_TIMES})")
|
||||
@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],
|
||||
次數: app_commands.Range[int, 1, lottery.MINER_MAX_TIMES] = 1,
|
||||
):
|
||||
total, counter, achievements = lottery.miner(interaction.user.id, 礦池.value, 次數)
|
||||
summary = "、".join(f"{reward} exp ×{n}" for reward, n in sorted(counter.items()))
|
||||
await interaction.response.send_message(
|
||||
f"{interaction.user.mention} 在 **{礦池.name}** 挖了 {次數} 次\n"
|
||||
f"總共獲得:**{total} exp**\n"
|
||||
f"### {summary}\n"
|
||||
f"{announce(achievements)}"
|
||||
)
|
||||
|
||||
@app_commands.command(name="扭蛋", description=f"從作者池抽扭蛋,每抽消耗{lottery.GACHA_COST}代幣")
|
||||
@app_commands.describe(抽數="一次抽幾個")
|
||||
@app_commands.choices(抽數=[
|
||||
app_commands.Choice(name="一抽", value=1),
|
||||
app_commands.Choice(name="三抽", value=3),
|
||||
app_commands.Choice(name="十抽", value=10),
|
||||
])
|
||||
async def gacha(self, interaction: discord.Interaction, 抽數: app_commands.Choice[int]):
|
||||
lines, achievements = lottery.gacha(interaction.user.id, 抽數.value)
|
||||
result = "\n".join(f"・{line}" for line in lines)
|
||||
await interaction.response.send_message(
|
||||
f"{interaction.user.mention} 抽了 {抽數.name} 作者池,結果如下:\n{result}\n{announce(achievements)}"
|
||||
)
|
||||
|
||||
@app_commands.command(name="刮刮樂", description=f"玩一次刮刮樂,消耗{lottery.SCRATCH_COST}代幣")
|
||||
async def scratch(self, interaction: discord.Interaction):
|
||||
opponent, player, hits, exp = lottery.scratch(interaction.user.id)
|
||||
|
||||
def spoiler(nums):
|
||||
return "# ||" + " ".join(f"{n:02d}" for n in nums) + "||"
|
||||
|
||||
board = "\n".join(
|
||||
["對手的號碼:", spoiler(opponent), "您的號碼:"]
|
||||
+ [spoiler(player[i:i + 3]) for i in range(0, 9, 3)]
|
||||
)
|
||||
await interaction.response.send_message(
|
||||
f"{interaction.user.mention} 刮刮樂開始!祝你好運~\n{board}\n"
|
||||
f"結果:||對中 {hits} 個,獲得 {exp} exp||"
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Lottery())
|
||||
@@ -0,0 +1,44 @@
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import db
|
||||
from game.achievements import ACHIEVEMENTS
|
||||
|
||||
|
||||
class Profile(commands.Cog):
|
||||
@app_commands.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"),
|
||||
app_commands.Choice(name="成就", value="achievements"),
|
||||
])
|
||||
async def view(self, interaction: discord.Interaction, 類型: app_commands.Choice[str]):
|
||||
user = interaction.user
|
||||
row = db.get_user(user.id)
|
||||
if row is None or row["total_signin"] == 0:
|
||||
await interaction.response.send_message(f"# {user.mention} 沒有簽到過!!\n請先簽到")
|
||||
return
|
||||
|
||||
match 類型.value:
|
||||
case "signin_count":
|
||||
msg = f"{user.mention} 簽到了 {row['total_signin']} 次"
|
||||
case "coin_count":
|
||||
msg = f"{user.mention} 擁有 {row['coin']} 枚代幣"
|
||||
case "exp_count":
|
||||
msg = f"{user.mention} 有 {row['exp']} 經驗值"
|
||||
case "last_signin":
|
||||
msg = f"{user.mention} 上次簽到日期為 {row['last_signin']}"
|
||||
case "achievements":
|
||||
got = db.get_achievements(user.id)
|
||||
lines = [f"🏆 **{name}** — {cond}" for k, (name, cond, _) in ACHIEVEMENTS.items() if k in got]
|
||||
msg = f"{user.mention} 已解鎖成就 {len(got)}/{len(ACHIEVEMENTS)}\n" + "\n".join(lines)
|
||||
|
||||
await interaction.response.send_message(msg)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Profile())
|
||||
@@ -0,0 +1,84 @@
|
||||
from datetime import datetime, time
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands, tasks
|
||||
|
||||
import db
|
||||
from config import TZ
|
||||
|
||||
SERVER_TOP = 10
|
||||
GLOBAL_TOP = 20
|
||||
COLUMNS = {"signin": "total_signin", "exp": "exp"}
|
||||
|
||||
|
||||
class Rank(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
self.global_cache: dict[str, list] = {}
|
||||
self.global_updated: datetime | None = None
|
||||
|
||||
async def cog_load(self):
|
||||
self.refresh_global()
|
||||
self.daily_refresh.start()
|
||||
|
||||
async def cog_unload(self):
|
||||
self.daily_refresh.cancel()
|
||||
|
||||
# 全國排行榜每日 00:00 更新
|
||||
@tasks.loop(time=time(0, 0, tzinfo=TZ))
|
||||
async def daily_refresh(self):
|
||||
self.refresh_global()
|
||||
|
||||
def refresh_global(self):
|
||||
for key, col in COLUMNS.items():
|
||||
self.global_cache[key] = db.conn.execute(
|
||||
f"SELECT id, {col} AS value FROM users WHERE {col} > 0 ORDER BY {col} DESC LIMIT ?",
|
||||
(GLOBAL_TOP,),
|
||||
).fetchall()
|
||||
self.global_updated = datetime.now(TZ)
|
||||
|
||||
def server_rank(self, guild: discord.Guild, col: str) -> list[tuple[str, int]]:
|
||||
rows = db.conn.execute(f"SELECT id, {col} AS value FROM users WHERE {col} > 0 ORDER BY {col} DESC")
|
||||
result = []
|
||||
for row in rows:
|
||||
member = guild.get_member(row["id"])
|
||||
if member:
|
||||
result.append((member.display_name, row["value"]))
|
||||
if len(result) >= SERVER_TOP:
|
||||
break
|
||||
return result
|
||||
|
||||
def global_rank(self, key: str) -> list[tuple[str, int]]:
|
||||
result = []
|
||||
for row in self.global_cache[key]:
|
||||
user = self.bot.get_user(row["id"])
|
||||
result.append((user.name if user else f"(ID:{row['id']})", row["value"]))
|
||||
return result
|
||||
|
||||
@app_commands.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"),
|
||||
])
|
||||
@app_commands.guild_only()
|
||||
async def rank(self, interaction: discord.Interaction, 類型: app_commands.Choice[str]):
|
||||
key, scope = 類型.value.split("_")
|
||||
if scope == "server":
|
||||
data = self.server_rank(interaction.guild, COLUMNS[key])
|
||||
footer = ""
|
||||
else:
|
||||
data = self.global_rank(key)
|
||||
footer = f"\n更新時間:{self.global_updated:%Y-%m-%d %H:%M}(每日 00:00 更新)"
|
||||
|
||||
body = "\n".join(f"{i}. {name} - {value}" for i, (name, value) in enumerate(data, start=1))
|
||||
await interaction.response.send_message(
|
||||
f"**{類型.name} 排行榜**\n```{body or '(目前沒有資料)'}```{footer}"
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Rank(bot))
|
||||
@@ -0,0 +1,36 @@
|
||||
from typing import Optional
|
||||
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import db
|
||||
|
||||
|
||||
class Settings(commands.Cog):
|
||||
@app_commands.command(name="設定", description="設定簽到頻道與表符(管理員)")
|
||||
@app_commands.describe(
|
||||
簽到表符="簽到用的表符,如 🪙 或自訂表符",
|
||||
簽到頻道="只偵測這個頻道,不填則偵測所有頻道",
|
||||
)
|
||||
@app_commands.guild_only()
|
||||
@app_commands.default_permissions(administrator=True)
|
||||
async def settings(
|
||||
self,
|
||||
interaction: discord.Interaction,
|
||||
簽到表符: str,
|
||||
簽到頻道: Optional[discord.TextChannel] = None,
|
||||
):
|
||||
emoji = 簽到表符.strip()
|
||||
with db.conn:
|
||||
db.set_server(interaction.guild.id, 簽到頻道.id if 簽到頻道 else None, emoji)
|
||||
|
||||
channel_text = 簽到頻道.mention if 簽到頻道 else "所有頻道"
|
||||
await interaction.response.send_message(
|
||||
f"已更新簽到設定:\n頻道:{channel_text}\n表符:{emoji}",
|
||||
ephemeral=True,
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Settings())
|
||||
@@ -0,0 +1,46 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
import db
|
||||
from game import signin
|
||||
|
||||
DEFAULT_EMOJI = "🪙"
|
||||
|
||||
|
||||
class Signin(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_message(self, message: discord.Message):
|
||||
if message.author.bot or not message.guild:
|
||||
return
|
||||
|
||||
setting = db.get_server(message.guild.id)
|
||||
emoji = setting["signin_emoji"] if setting else DEFAULT_EMOJI
|
||||
channel_id = setting["signin_channel"] if setting else None
|
||||
|
||||
if message.content.strip() != emoji:
|
||||
return
|
||||
if channel_id is not None and message.channel.id != channel_id:
|
||||
return
|
||||
|
||||
signed, jackpot = signin.try_signin(message.author.id)
|
||||
if not signed:
|
||||
return
|
||||
|
||||
try:
|
||||
await message.add_reaction(emoji)
|
||||
except discord.HTTPException:
|
||||
pass # 表符失效(例如被刪除)時不影響簽到
|
||||
|
||||
if jackpot:
|
||||
await message.channel.send(
|
||||
f"# 感謝 {message.author.mention} 的陪伴!\n"
|
||||
f"將為您奉上三顆糾纏之緣 (共 {signin.JACKPOT_COIN} 代幣)!\n"
|
||||
"# 🏆 達成成就《三顆糾纏之緣》"
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Signin(bot))
|
||||
Reference in new issue
Block a user