發布 V1.0.2:排行榜、手動換日、設定改用 md 格式、新預設值
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9286b64518
commit
f5a3b91783
8 files changed
+169
-29
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import db
|
||||
|
||||
SIZE = 5 # 每個排行榜顯示的格數
|
||||
EMPTY = "-----"
|
||||
MEDALS = {1: "🥇 ", 2: "🥈 ", 3: "🥉 "}
|
||||
|
||||
|
||||
def line(rank: int, user_id: int, points: int, currency: str) -> str:
|
||||
return f"{MEDALS.get(rank, '')}第 {rank} 名|<@{user_id}>|{points} {currency}"
|
||||
|
||||
|
||||
async def reply(interaction: discord.Interaction, text: str):
|
||||
# 公開訊息,用 mention 顯示名字但不要 tag 到人
|
||||
await interaction.response.send_message(text, allowed_mentions=discord.AllowedMentions.none())
|
||||
|
||||
|
||||
@app_commands.guild_only()
|
||||
class Leaderboard(commands.GroupCog, group_name="排行榜", group_description="簽到點數排行榜"):
|
||||
@app_commands.command(name="伺服器", description="查看這個伺服器的前五名")
|
||||
async def server(self, interaction: discord.Interaction):
|
||||
currency = db.get_setting(interaction.guild_id, "currency_name")
|
||||
lines = [line(*row, currency) for row in db.get_ranking(interaction.guild_id)[:SIZE]]
|
||||
lines += [EMPTY] * (SIZE - len(lines))
|
||||
await reply(interaction, "**🏆 伺服器排行榜**\n" + "\n".join(lines))
|
||||
|
||||
@app_commands.command(name="個人", description="查看自己的名次和前後各兩位")
|
||||
async def personal(self, interaction: discord.Interaction):
|
||||
user = interaction.user
|
||||
ranking = db.get_ranking(interaction.guild_id)
|
||||
index = next((i for i, row in enumerate(ranking) if row[1] == user.id), None)
|
||||
if index is None:
|
||||
await reply(interaction, f"{user.mention} 在這個伺服器還沒有簽到紀錄")
|
||||
return
|
||||
|
||||
currency = db.get_setting(interaction.guild_id, "currency_name")
|
||||
lines = []
|
||||
for i in range(index - SIZE // 2, index + SIZE // 2 + 1):
|
||||
if not 0 <= i < len(ranking):
|
||||
lines.append(EMPTY)
|
||||
elif i == index:
|
||||
lines.append(f"**▶ {line(*ranking[i], currency)}**")
|
||||
else:
|
||||
lines.append(line(*ranking[i], currency))
|
||||
await reply(interaction, f"**📊 {user.mention} 的排名**\n" + "\n".join(lines))
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Leaderboard())
|
||||
+18
-12
@@ -11,20 +11,26 @@ def summary(guild: discord.Guild) -> str:
|
||||
def get(key):
|
||||
return db.get_setting(guild.id, key)
|
||||
|
||||
# 程式碼區塊裡的 <#id> 不會變成頻道連結,所以直接寫頻道名稱
|
||||
def channel_text(key):
|
||||
channel = guild.get_channel(get(key))
|
||||
return channel.mention if channel else "未設定"
|
||||
return f"#{channel.name}" if channel else "未設定"
|
||||
|
||||
return (
|
||||
"**私人包廂**\n"
|
||||
f"觸發頻道:{channel_text('trigger_channel')}\n"
|
||||
f"包廂名稱:{get('room_name')}\n"
|
||||
f"空房刪除:{get('empty_seconds')} 秒\n"
|
||||
"**簽到**\n"
|
||||
f"簽到文字:{get('signin_text')}\n"
|
||||
f"簽到獎勵:{get('signin_reward')} {get('currency_name')}\n"
|
||||
f"回應表符:{get('signin_reaction')}\n"
|
||||
f"簽到頻道:{channel_text('signin_channel')}"
|
||||
"```md\n"
|
||||
f"# {guild.name} 的設定\n"
|
||||
"\n"
|
||||
"## 私人包廂\n"
|
||||
f"- 觸發頻道:{channel_text('trigger_channel')}\n"
|
||||
f"- 包廂名稱:{get('room_name')}\n"
|
||||
f"- 空房刪除:{get('empty_seconds')} 秒\n"
|
||||
"\n"
|
||||
"## 簽到\n"
|
||||
f"- 簽到文字:{get('signin_text')}\n"
|
||||
f"- 簽到獎勵:{get('signin_reward')} {get('currency_name')}\n"
|
||||
f"- 回應表符:{get('signin_reaction')}\n"
|
||||
f"- 簽到頻道:{channel_text('signin_channel')}\n"
|
||||
"```"
|
||||
)
|
||||
|
||||
|
||||
@@ -68,9 +74,9 @@ class Settings(commands.GroupCog, group_name="設定", group_description="機器
|
||||
|
||||
@app_commands.command(name="簽到", description="設定簽到,只會更新有填的項目")
|
||||
@app_commands.describe(
|
||||
簽到文字="訊息內容完全等於這段文字才算簽到,如 月眠🙏",
|
||||
簽到文字="訊息內容完全等於這段文字才算簽到,如 簽到",
|
||||
簽到獎勵="每次簽到獲得的數量",
|
||||
貨幣名稱="點數的名稱,如 願力",
|
||||
貨幣名稱="點數的名稱,如 元",
|
||||
回應表符="簽到成功時機器人加上的反應",
|
||||
簽到頻道="只偵測這個頻道",
|
||||
)
|
||||
|
||||
+16
-2
@@ -8,6 +8,10 @@ import db
|
||||
from config import TZ
|
||||
|
||||
|
||||
def today() -> str:
|
||||
return datetime.now(TZ).date().isoformat()
|
||||
|
||||
|
||||
class Signin(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot):
|
||||
self.bot = bot
|
||||
@@ -22,10 +26,9 @@ class Signin(commands.Cog):
|
||||
if message.content.strip() != db.get_setting(guild_id, "signin_text"):
|
||||
return
|
||||
|
||||
today = datetime.now(TZ).date().isoformat()
|
||||
with db.conn:
|
||||
signed = db.try_signin(
|
||||
guild_id, message.author.id, db.get_setting(guild_id, "signin_reward"), today
|
||||
guild_id, message.author.id, db.get_setting(guild_id, "signin_reward"), today()
|
||||
)
|
||||
if not signed:
|
||||
return
|
||||
@@ -46,6 +49,17 @@ class Signin(commands.Cog):
|
||||
allowed_mentions=discord.AllowedMentions.none(), # 公開訊息,不要 tag 到自己
|
||||
)
|
||||
|
||||
@app_commands.command(name="手動換日", description="讓今天簽過到的人可以再簽一次(管理員)")
|
||||
@app_commands.guild_only()
|
||||
@app_commands.default_permissions(administrator=True)
|
||||
async def reset_day(self, interaction: discord.Interaction):
|
||||
with db.conn:
|
||||
count = db.reset_signin_day(interaction.guild_id, today())
|
||||
await interaction.response.send_message(
|
||||
f"🔄 已手動換日,今天已簽到的 {count} 位成員可以再簽到一次",
|
||||
allowed_mentions=discord.AllowedMentions.none(),
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot):
|
||||
await bot.add_cog(Signin(bot))
|
||||
Reference in new issue
Block a user