add 初版程式碼

This commit is contained in:
tangsong
2026-02-27 16:05:14 +08:00
parent 6d1d99ee73
commit 46898c8165
10 changed files with 1902 additions and 2 deletions
+76
View File
@@ -0,0 +1,76 @@
"""
Base Cog - 基礎功能
開發者:唐宋
"""
import discord
from discord.ext import commands
from discord import app_commands
class Base(commands.Cog):
"""基礎功能 Cog"""
def __init__(self, bot):
self.bot = bot
# --- Slash Commands ---
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name='info', description='顯示機器人資訊')
async def slash_info(self, interaction: discord.Interaction):
"""顯示機器人資訊"""
embed = discord.Embed(
title='📖 機器人資訊',
color=discord.Color.blue()
)
embed.add_field(name='名稱', value=self.bot.user.name, inline=True)
embed.add_field(name='ID', value=self.bot.user.id, inline=True)
embed.add_field(name='伺服器數量', value=len(self.bot.guilds), inline=True)
embed.add_field(name='成員數量', value=sum(g.member_count for g in self.bot.guilds), inline=True)
embed.set_footer(text=f'由 Discord.py {discord.__version__} 驅動')
await interaction.response.send_message(embed=embed)
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name='ping', description='測試機器人連線')
async def slash_ping(self, interaction: discord.Interaction):
"""測試機器人連線"""
latency = round(self.bot.latency * 1000)
await interaction.response.send_message(f'🏓 Pong! 延遲: {latency}ms')
@app_commands.guilds(discord.Object(id=605678541530726421))
@app_commands.command(name='echo', description='回覆你的訊息')
async def slash_echo(self, interaction: discord.Interaction, message: str):
"""回覆你的訊息"""
await interaction.response.send_message(f'🔊 {message}')
# --- Prefix Commands (向後相容) ---
@commands.command(name='info')
async def info(self, ctx):
"""顯示機器人資訊"""
embed = discord.Embed(
title='📖 機器人資訊',
color=discord.Color.blue()
)
embed.add_field(name='名稱', value=self.bot.user.name, inline=True)
embed.add_field(name='ID', value=self.bot.user.id, inline=True)
embed.add_field(name='伺服器數量', value=len(self.bot.guilds), inline=True)
embed.add_field(name='成員數量', value=sum(g.member_count for g in self.bot.guilds), inline=True)
embed.set_footer(text=f'由 Discord.py {discord.__version__} 驅動')
await ctx.send(embed=embed)
@commands.command(name='ping')
async def ping(self, ctx):
"""測試機器人連線"""
latency = round(self.bot.latency * 1000)
await ctx.send(f'🏓 Pong! 延遲: {latency}ms')
@commands.command(name='echo')
async def echo(self, ctx, *, message):
"""回覆你的訊息"""
await ctx.send(f'🔊 {message}')
# 需要注入 bot 變數
async def setup(bot):
"""註冊 Base cog"""
await bot.add_cog(Base(bot))
+153
View File
@@ -0,0 +1,153 @@
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))