Files
LunarHelper/cogs/signin.py
T

66 lines
2.3 KiB
Python

from datetime import datetime
import discord
from discord import app_commands
from discord.ext import commands
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
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
if message.author.bot or not message.guild:
return
guild_id = message.guild.id
if message.channel.id != db.get_setting(guild_id, "signin_channel"):
return
if message.content.strip() != db.get_setting(guild_id, "signin_text"):
return
with db.conn:
signed = db.try_signin(
guild_id, message.author.id, db.get_setting(guild_id, "signin_reward"), today()
)
if not signed:
return
try:
await message.add_reaction(db.get_setting(guild_id, "signin_reaction"))
except discord.HTTPException:
pass # 表符失效(例如被刪除)時不影響簽到
@app_commands.command(name="餘額", description="查看自己的點數")
@app_commands.guild_only()
async def balance(self, interaction: discord.Interaction):
user = interaction.user
guild_id = interaction.guild_id
await interaction.response.send_message(
f"{user.mention} 目前有 {db.get_points(guild_id, user.id)} "
f"{db.get_setting(guild_id, 'currency_name')}",
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))