From 054a7e62dd7a235bbbb4800dce53965a8df80b5a Mon Sep 17 00:00:00 2001 From: tangsong Date: Wed, 4 Mar 2026 01:27:56 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=E4=BF=AE=E6=94=B9=E6=BC=8F=E6=8E=89?= =?UTF-8?q?=E7=9A=84=E8=AE=8A=E6=95=B8=20MY=5FGUILD=5FID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cogs/base.py | 11 ++++++++--- discord-bot.py | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cogs/base.py b/cogs/base.py index 309da92..fb52c6e 100644 --- a/cogs/base.py +++ b/cogs/base.py @@ -5,7 +5,12 @@ Base Cog - 基礎功能 import discord from discord.ext import commands from discord import app_commands +from dotenv import load_dotenv +# 載入 .env 檔案 +load_dotenv() +# 從環境變數讀取 Guild ID 並轉型為 int +MY_GUILD_ID = int(os.getenv('GUILD_ID')) class Base(commands.Cog): """基礎功能 Cog""" @@ -14,7 +19,7 @@ class Base(commands.Cog): self.bot = bot # --- Slash Commands --- - @app_commands.guilds(discord.Object(id=605678541530726421)) + @app_commands.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name='info', description='顯示機器人資訊') async def slash_info(self, interaction: discord.Interaction): """顯示機器人資訊""" @@ -29,14 +34,14 @@ class Base(commands.Cog): 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.guilds(discord.Object(id=MY_GUILD_ID)) @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.guilds(discord.Object(id=MY_GUILD_ID)) @app_commands.command(name='echo', description='回覆你的訊息') async def slash_echo(self, interaction: discord.Interaction, message: str): """回覆你的訊息""" diff --git a/discord-bot.py b/discord-bot.py index 5c1770a..e77f2fe 100644 --- a/discord-bot.py +++ b/discord-bot.py @@ -12,6 +12,8 @@ load_dotenv() # 設定 bot token TOKEN = os.getenv('DISCORD_BOT_TOKEN') +# 從環境變數讀取 Guild ID 並轉型為 int +MY_GUILD_ID = int(os.getenv('GUILD_ID')) # 設定 bot intents = discord.Intents.default() @@ -42,7 +44,7 @@ async def on_ready(): # 同步 Slash Commands 到指定伺服器 try: - guild = discord.Object(id=605678541530726421) + guild = discord.Object(id=MY_GUILD_ID) synced = await bot.tree.sync(guild=guild) print(f'✅ 已同步 {len(synced)} 個 Slash Commands 到伺服器 {guild.id}') for cmd in synced: