From 029fee16ea029a62e20531ec2d14e965e87d534c Mon Sep 17 00:00:00 2001 From: tangsong Date: Tue, 3 Mar 2026 23:52:33 +0800 Subject: [PATCH] =?UTF-8?q?edit=20=E4=BD=BF=E7=94=A8=20.env=20=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E6=94=B9=E9=A0=BB=E9=81=93ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 6 +++++- cogs/lottery.py | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/.env.example b/.env.example index 40467f1..28b11a9 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,8 @@ DISCORD_BOT_TOKEN=your_bot_token_here # Discord Application ID (可選) # 用於 Slash Commands -DISCORD_APPLICATION_ID=your_application_id_here \ No newline at end of file +DISCORD_APPLICATION_ID=your_application_id_here + +# Discord Guild ID (可選) +# 用於限制 Slash Commands 只在特定伺服器可用 +GUILD_ID=your_guild_id_here \ No newline at end of file diff --git a/cogs/lottery.py b/cogs/lottery.py index 2d868a3..4066916 100644 --- a/cogs/lottery.py +++ b/cogs/lottery.py @@ -1,12 +1,19 @@ +import os import discord import random import json import asyncio import datetime +from dotenv import load_dotenv from discord.ext import commands from discord import app_commands from discord import FFmpegPCMAudio +# 載入 .env 檔案 +load_dotenv() +# 從環境變數讀取 Guild ID 並轉型為 int +MY_GUILD_ID = int(os.getenv('GUILD_ID')) + class Lottery(commands.Cog): def __init__(self, bot): self.bot = bot @@ -22,7 +29,7 @@ class Lottery(commands.Cog): self.stop = False # --- 建立抽選清單 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="建立抽選清單", description="建立寶可夢或動畫角色的抽選清單") @app_commands.choices(list_name=[ app_commands.Choice(name="寶可夢", value="寶可夢"), @@ -48,7 +55,7 @@ class Lottery(commands.Cog): await interaction.response.send_message(msg, ephemeral=True) # --- 顯示清單狀態 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="抽選清單", description="檢視所有抽選清單及目前抽選狀態") async def show_list(self, interaction: discord.Interaction): pokeCount = self.getPokeCount + 1 @@ -59,7 +66,7 @@ class Lottery(commands.Cog): ) # --- 抽寶可夢 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="抽寶可夢", description="顯示題目,並於指定秒數後公布答案圖片") @app_commands.describe(t="倒數秒數(需大於5)") async def get_poke(self, interaction: discord.Interaction, t: int): @@ -97,7 +104,7 @@ class Lottery(commands.Cog): await interaction.response.send_message("請先加入語音頻道", ephemeral=True) # --- 抽動畫角色 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="抽動畫角色", description="顯示題目,並於指定秒數後公布答案圖片") @app_commands.describe(t="倒數秒數(需大於5)") async def get_ani(self, interaction: discord.Interaction, t: int): @@ -135,7 +142,7 @@ class Lottery(commands.Cog): await interaction.response.send_message("請先加入語音頻道", ephemeral=True) # --- 新增:動畫角色單一複習 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="動畫角色複習", description="顯示指定編號的角色") @app_commands.describe(num="想要查看的角色編號 (從 1 開始)") async def check_ani(self, interaction: discord.Interaction, num: int = 1): @@ -151,7 +158,7 @@ class Lottery(commands.Cog): await interaction.response.send_message(embed=embed) # --- 新增:動畫角色總複習 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="動畫角色總複習", description="每 2 秒顯示一位動畫角色,直到結束") async def check_all_ani(self, interaction: discord.Interaction): self.stop = False @@ -177,7 +184,7 @@ class Lottery(commands.Cog): await interaction.followup.send("複習完畢!") # --- 停止複習 --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name="停止", description="停止顯示總複習清單") async def stop_check_all_ani(self, interaction: discord.Interaction): self.stop = True