Compare commits
2 Commits
4378a8d572
...
054a7e62dd
| Author | SHA1 | Date |
|---|---|---|
|
|
054a7e62dd | |
|
|
50d60bf97c |
|
|
@ -0,0 +1,17 @@
|
||||||
|
# 使用輕量級的 Python 映像檔
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
# 設定容器內的工作目錄
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 先複製需求文件,利用 Docker 快取機制加速打包
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
# 安裝必要的套件
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# 複製其餘的程式碼到容器內
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 執行你的 Discord Bot
|
||||||
|
CMD ["python", "discord-bot.py"]
|
||||||
11
cogs/base.py
11
cogs/base.py
|
|
@ -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):
|
||||||
"""回覆你的訊息"""
|
"""回覆你的訊息"""
|
||||||
|
|
|
||||||
|
|
@ -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:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
services:
|
||||||
|
discord-bot:
|
||||||
|
build: .
|
||||||
|
container_name: my-discord-bot
|
||||||
|
restart: always # 機器人當掉或伺服器重啟時會自動重啟
|
||||||
|
env_file:
|
||||||
|
- .env # 讀取你的 .env 檔案(如果有的話)
|
||||||
|
volumes:
|
||||||
|
- .:/app # (選配) 開發階段可以掛載目錄,修改程式碼不用重新 build
|
||||||
|
logging: # 限制日誌大小,避免硬碟被塞爆
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-size: "10mb"
|
||||||
|
max-file: "3"
|
||||||
Loading…
Reference in New Issue