fixed 修改漏掉的變數 MY_GUILD_ID

This commit is contained in:
tangsong 2026-03-04 01:27:56 +08:00
parent 50d60bf97c
commit 054a7e62dd
2 changed files with 11 additions and 4 deletions

View File

@ -5,7 +5,12 @@ Base Cog - 基礎功能
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import app_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): class Base(commands.Cog):
"""基礎功能 Cog""" """基礎功能 Cog"""
@ -14,7 +19,7 @@ class Base(commands.Cog):
self.bot = bot self.bot = bot
# --- Slash Commands --- # --- Slash Commands ---
@app_commands.guilds(discord.Object(id=605678541530726421)) @app_commands.guilds(discord.Object(id=MY_GUILD_ID))
@app_commands.command(name='info', description='顯示機器人資訊') @app_commands.command(name='info', description='顯示機器人資訊')
async def slash_info(self, interaction: discord.Interaction): 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__} 驅動') embed.set_footer(text=f'由 Discord.py {discord.__version__} 驅動')
await interaction.response.send_message(embed=embed) 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='測試機器人連線') @app_commands.command(name='ping', description='測試機器人連線')
async def slash_ping(self, interaction: discord.Interaction): async def slash_ping(self, interaction: discord.Interaction):
"""測試機器人連線""" """測試機器人連線"""
latency = round(self.bot.latency * 1000) latency = round(self.bot.latency * 1000)
await interaction.response.send_message(f'🏓 Pong! 延遲: {latency}ms') 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='回覆你的訊息') @app_commands.command(name='echo', description='回覆你的訊息')
async def slash_echo(self, interaction: discord.Interaction, message: str): async def slash_echo(self, interaction: discord.Interaction, message: str):
"""回覆你的訊息""" """回覆你的訊息"""

View File

@ -12,6 +12,8 @@ load_dotenv()
# 設定 bot token # 設定 bot token
TOKEN = os.getenv('DISCORD_BOT_TOKEN') TOKEN = os.getenv('DISCORD_BOT_TOKEN')
# 從環境變數讀取 Guild ID 並轉型為 int
MY_GUILD_ID = int(os.getenv('GUILD_ID'))
# 設定 bot # 設定 bot
intents = discord.Intents.default() intents = discord.Intents.default()
@ -42,7 +44,7 @@ async def on_ready():
# 同步 Slash Commands 到指定伺服器 # 同步 Slash Commands 到指定伺服器
try: try:
guild = discord.Object(id=605678541530726421) guild = discord.Object(id=MY_GUILD_ID)
synced = await bot.tree.sync(guild=guild) synced = await bot.tree.sync(guild=guild)
print(f'✅ 已同步 {len(synced)} 個 Slash Commands 到伺服器 {guild.id}') print(f'✅ 已同步 {len(synced)} 個 Slash Commands 到伺服器 {guild.id}')
for cmd in synced: for cmd in synced: