81 lines
3.7 KiB
Python
81 lines
3.7 KiB
Python
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())
|