-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
80 lines (63 loc) · 2.43 KB
/
bot.py
File metadata and controls
80 lines (63 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import discord
from discord.ext import commands
import asyncio
import os
from dotenv import load_dotenv
from utils.music import log_message, scan_and_update_musicsheet, init_musicsheet_system
from utils.db import init_db
import utils.shared_state as shared_state
# 加載環境變數
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ENV_PATH = os.path.join(BASE_DIR, "data", ".env")
load_dotenv(ENV_PATH)
TOKEN = os.getenv("DISCORD_BOT_TOKEN")
if not TOKEN:
print("❌ 錯誤: 未找到 DISCORD_BOT_TOKEN 環境變數")
TOKEN = ""
else:
TOKEN = TOKEN.strip()
LOG_DIR = "logs"
SONG_DIR = "song/"
# 初始化 Discord 設定
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
class GooseBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix="!", intents=intents, help_command=None)
async def setup_hook(self):
# 載入 Cogs
await self.load_extension("cogs.general")
await self.load_extension("cogs.music")
await self.load_extension("cogs.dice")
await self.load_extension("cogs.initiative")
# 同步 Slash Commands
await self.tree.sync()
print("✅ Slash commands synced!")
async def on_ready(self):
os.makedirs(LOG_DIR, exist_ok=True)
os.makedirs(SONG_DIR, exist_ok=True)
# 清空 log
with open(os.path.join(LOG_DIR, "log.txt"), "w", encoding="utf-8") as f:
f.write("")
shared_state.playback_mode = "循環播放清單"
log_message(f"✅ 機器人已上線:{self.user}")
print(f"✅ 機器人已上線:{self.user}")
init_musicsheet_system()
scan_and_update_musicsheet()
await init_db()
async def on_error(self, event, *args, **kwargs):
import traceback
error_info = traceback.format_exc()
log_message(f"❌ 未捕捉錯誤發生於事件 `{event}`\n{error_info}")
print(f"❌ 未捕捉錯誤發生於事件 `{event}`")
bot = GooseBot()
@bot.tree.command(name="sync", description="手動同步應用指令")
async def sync(interaction: discord.Interaction):
await bot.tree.sync()
await interaction.response.send_message("✅ 指令已同步!", ephemeral=True)
if __name__ == "__main__":
if os.name == 'nt':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
if TOKEN:
bot.run(TOKEN)