Compare commits
4
Commits
4378a8d572
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71e542fb1f | ||
|
|
de0c7cb90e | ||
|
|
054a7e62dd | ||
|
|
50d60bf97c |
+20
@@ -0,0 +1,20 @@
|
|||||||
|
# 使用 Python 映像檔
|
||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
# 更新系統並安裝 FFmpeg
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y ffmpeg && \
|
||||||
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# 設定工作目錄
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# 複製並安裝 Python 套件
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
# 複製程式碼
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# 執行 Bot,加上 -u 確保輸出不被快取
|
||||||
|
CMD ["python", "-u", "discord-bot.py"]
|
||||||
+9
-3
@@ -3,9 +3,15 @@ Base Cog - 基礎功能
|
|||||||
開發者:唐宋
|
開發者:唐宋
|
||||||
"""
|
"""
|
||||||
import discord
|
import discord
|
||||||
|
import os
|
||||||
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 +20,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 +35,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):
|
||||||
"""回覆你的訊息"""
|
"""回覆你的訊息"""
|
||||||
|
|||||||
+3
-1
@@ -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,18 @@
|
|||||||
|
services:
|
||||||
|
discord-bot:
|
||||||
|
build: .
|
||||||
|
container_name: my-discord-bot
|
||||||
|
restart: always
|
||||||
|
tty: true # 模擬終端機
|
||||||
|
stdin_open: true # 開啟標準輸入
|
||||||
|
environment:
|
||||||
|
- PYTHONUNBUFFERED=1 # 強制 Python 立即輸出,不進行快取
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
volumes:
|
||||||
|
- .:/app # (選配) 開發階段可以掛載目錄,修改程式碼不用重新 build
|
||||||
|
logging: # 限制日誌大小,避免硬碟被塞爆
|
||||||
|
driver: "json-file"
|
||||||
|
options:
|
||||||
|
max-size: "10mb"
|
||||||
|
max-file: "3"
|
||||||
Reference in New Issue
Block a user