Files
discord-guess-img-bot/cogs/lottery.py
T

153 lines
6.9 KiB
Python
Raw Normal View History

2026-02-27 16:05:14 +08:00
import discord
import random
import json
import asyncio
import datetime
from discord.ext import commands
from discord import app_commands
from discord import FFmpegPCMAudio
class Lottery(commands.Cog):
def __init__(self, bot):
self.bot = bot
with open('./pokemon.json', mode='r', encoding='utf8') as jFile:
self.pokeData = json.load(jFile)
with open('./animate.json', mode='r', encoding='utf8') as jFile:
self.aniData = json.load(jFile)
self.getPokeCount = 0
self.getAniCount = 0
self.pokeList = random.sample(range(0, len(self.pokeData)), len(self.pokeData))
self.aniList = random.sample(range(0, len(self.aniData)), len(self.aniData))
self.stop = False
# --- Slash Command with Choice ---
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name="建立抽選清單", description="建立寶可夢或動畫角色的抽選清單")
@app_commands.choices(list_name=[
app_commands.Choice(name="寶可夢", value="寶可夢"),
app_commands.Choice(name="動畫角色", value="動畫角色"),
app_commands.Choice(name="全部", value="全部")
])
async def create_list(self, interaction: discord.Interaction, list_name: app_commands.Choice[str]):
if list_name.value == "寶可夢":
self.pokeList = random.sample(range(0, len(self.pokeData)), len(self.pokeData))
self.getPokeCount = 0
msg = "已建立寶可夢抽選清單!"
elif list_name.value == "動畫角色":
self.aniList = random.sample(range(0, len(self.aniData)), len(self.aniData))
self.getAniCount = 0
msg = "已建立動畫角色抽選清單!"
else:
self.pokeList = random.sample(range(0, len(self.pokeData)), len(self.pokeData))
self.aniList = random.sample(range(0, len(self.aniData)), len(self.aniData))
self.getPokeCount = 0
self.getAniCount = 0
msg = "已建立全部抽選清單!"
await interaction.response.send_message(msg, ephemeral=True)
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name="抽選清單", description="檢視所有抽選清單及目前抽選狀態")
async def show_list(self, interaction: discord.Interaction):
pokeCount = self.getPokeCount + 1
aniCount = self.getAniCount + 1
await interaction.response.send_message(
f'寶可夢清單:共{len(self.pokeData)}個,正準備抽選第 {pokeCount}\n'
f'動畫角色清單:共{len(self.aniData)}個,正準備抽選第 {aniCount} 個'
)
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name="抽寶可夢", description="顯示題目,並於指定秒數後公布答案圖片")
@app_commands.describe(t="倒數秒數(需大於5")
async def get_poke(self, interaction: discord.Interaction, t: int):
if t < 5:
await interaction.response.send_message("秒數需大於5秒", ephemeral=True)
return
if self.getPokeCount >= len(self.pokeData):
await interaction.response.send_message("清單已抽取完畢,請重新建立抽選清單", ephemeral=True)
return
if interaction.user.voice and interaction.user.voice.channel:
if interaction.guild.voice_client is None:
vc = await interaction.user.voice.channel.connect()
else:
vc = interaction.guild.voice_client
idx = self.pokeList[self.getPokeCount]
poke = self.pokeData[idx]
self.getPokeCount += 1
await interaction.response.send_message(
f'第 {self.getPokeCount}\n{poke["name"]} \n倒數 {t} 秒,開始!'
)
await asyncio.sleep(t - 5)
if not vc.is_playing():
vc.play(FFmpegPCMAudio('countdown.wav'))
await asyncio.sleep(5)
await interaction.followup.send("時間到!")
embed = discord.Embed(title="抽選寶可夢", timestamp=datetime.datetime.now(), color=0x34f4b4)
embed.add_field(name="編號:", value=poke["number"], inline=True)
embed.add_field(name="名稱:", value=poke["name"], inline=True)
embed.set_image(url=poke["url"])
await interaction.followup.send(embed=embed)
await vc.disconnect()
else:
await interaction.response.send_message("請先加入語音頻道", ephemeral=True)
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name="抽動畫角色", description="顯示題目,並於指定秒數後公布答案圖片")
@app_commands.describe(t="倒數秒數(需大於5")
async def get_ani(self, interaction: discord.Interaction, t: int):
if t < 5:
await interaction.response.send_message("秒數需大於5秒", ephemeral=True)
return
if self.getAniCount >= len(self.aniData):
await interaction.response.send_message("清單已抽取完畢,請重新建立抽選清單", ephemeral=True)
return
if interaction.user.voice and interaction.user.voice.channel:
if interaction.guild.voice_client is None:
vc = await interaction.user.voice.channel.connect()
else:
vc = interaction.guild.voice_client
idx = self.aniList[self.getAniCount]
ani = self.aniData[idx]
self.getAniCount += 1
await interaction.response.send_message(
f'第 {self.getAniCount}\n{ani["animate"]}{ani["character"]}\n倒數 {t} 秒,開始!'
)
await asyncio.sleep(t - 5)
if not vc.is_playing():
vc.play(FFmpegPCMAudio('countdown.wav'))
await asyncio.sleep(5)
await interaction.followup.send("時間到!")
embed = discord.Embed(title=f'第 {self.getAniCount} 抽角色', timestamp=datetime.datetime.now(), color=0x34f4b4)
embed.add_field(name="動畫:", value=ani["animate"], inline=True)
embed.add_field(name="角色:", value=ani["character"], inline=True)
embed.set_image(url=ani["url"])
await interaction.followup.send(embed=embed)
await vc.disconnect()
else:
await interaction.response.send_message("請先加入語音頻道", ephemeral=True)
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name="停止", description="停止顯示總複習清單")
async def stop_check_all_ani(self, interaction: discord.Interaction):
self.stop = True
await interaction.response.send_message("已接收停止指令", ephemeral=True)
# Discord.py 2.0+ 的 setup 寫法
async def setup(bot):
await bot.add_cog(Lottery(bot))