98 lines
3.5 KiB
Python
98 lines
3.5 KiB
Python
from typing import Optional
|
|
|
|
import discord
|
|
from discord import app_commands
|
|
from discord.ext import commands
|
|
|
|
import db
|
|
|
|
|
|
def summary(guild: discord.Guild) -> str:
|
|
def get(key):
|
|
return db.get_setting(guild.id, key)
|
|
|
|
def channel_text(key):
|
|
channel = guild.get_channel(get(key))
|
|
return channel.mention 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')}"
|
|
)
|
|
|
|
|
|
async def apply(interaction: discord.Interaction, **values):
|
|
"""寫入有填的項目(空白視為沒填),並回覆目前的設定"""
|
|
with db.conn:
|
|
for key, value in values.items():
|
|
if isinstance(value, str):
|
|
value = value.strip()
|
|
if value not in (None, ""):
|
|
db.set_setting(interaction.guild.id, key, value)
|
|
await interaction.response.send_message(summary(interaction.guild), ephemeral=True)
|
|
|
|
|
|
@app_commands.guild_only()
|
|
@app_commands.default_permissions(administrator=True)
|
|
class Settings(commands.GroupCog, group_name="設定", group_description="機器人設定(管理員)"):
|
|
@app_commands.command(name="查看", description="查看目前的設定")
|
|
async def show(self, interaction: discord.Interaction):
|
|
await apply(interaction)
|
|
|
|
@app_commands.command(name="包廂", description="設定私人包廂,只會更新有填的項目")
|
|
@app_commands.describe(
|
|
觸發頻道="進入這個語音頻道就會建立包廂",
|
|
包廂名稱="新包廂的名稱",
|
|
空房秒數="包廂沒人超過幾秒就刪除",
|
|
)
|
|
async def rooms(
|
|
self,
|
|
interaction: discord.Interaction,
|
|
觸發頻道: Optional[discord.VoiceChannel] = None,
|
|
包廂名稱: Optional[app_commands.Range[str, 1, 100]] = None,
|
|
空房秒數: Optional[app_commands.Range[int, 5, 86400]] = None,
|
|
):
|
|
await apply(
|
|
interaction,
|
|
trigger_channel=觸發頻道 and 觸發頻道.id,
|
|
room_name=包廂名稱,
|
|
empty_seconds=空房秒數,
|
|
)
|
|
|
|
@app_commands.command(name="簽到", description="設定簽到,只會更新有填的項目")
|
|
@app_commands.describe(
|
|
簽到文字="訊息內容完全等於這段文字才算簽到,如 月眠🙏",
|
|
簽到獎勵="每次簽到獲得的數量",
|
|
貨幣名稱="點數的名稱,如 願力",
|
|
回應表符="簽到成功時機器人加上的反應",
|
|
簽到頻道="只偵測這個頻道",
|
|
)
|
|
async def signin(
|
|
self,
|
|
interaction: discord.Interaction,
|
|
簽到文字: Optional[str] = None,
|
|
簽到獎勵: Optional[app_commands.Range[int, 1]] = None,
|
|
貨幣名稱: Optional[str] = None,
|
|
回應表符: Optional[str] = None,
|
|
簽到頻道: Optional[discord.TextChannel] = None,
|
|
):
|
|
await apply(
|
|
interaction,
|
|
signin_text=簽到文字,
|
|
signin_reward=簽到獎勵,
|
|
currency_name=貨幣名稱,
|
|
signin_reaction=回應表符,
|
|
signin_channel=簽到頻道 and 簽到頻道.id,
|
|
)
|
|
|
|
|
|
async def setup(bot: commands.Bot):
|
|
await bot.add_cog(Settings())
|