66import yaml
77import asyncio
88import platform
9+ import json
910
1011# Logging
1112logging .basicConfig (
2829# Bot
2930bot = commands .Bot (command_prefix = 'sh!' , intents = discord .Intents .all ())
3031
32+ async def write_slash_synced (slash : discord .app_commands .AppCommand ):
33+ with open ('slash.json' , 'w' ) as f :
34+ # 取得所有指令與其說明,並存入陣列中
35+ logging .info ('寫入指令同步資料' )
36+ slash = []
37+ for command in bot .tree .walk_commands ():
38+ logging .debug (f'指令名稱:{ command .name } ,指令說明:{ command .description } ' )
39+ slash .append ({
40+ 'name' : command .name ,
41+ 'description' : command .description
42+ })
43+
44+ logging .debug (f'指令同步資料:{ slash } ' )
45+ json .dump (slash , f )
46+ logging .info ('指令同步資料寫入成功' )
47+
3148# Cogs Slash Command
3249@bot .event
3350async def on_ready ():
@@ -43,6 +60,7 @@ async def on_ready():
4360 OS: { platform .system ()} { platform .release ()}
4461 Python Version: { platform .python_version ()}
4562 Discord.py Version: { discord .__version__ }
63+ YunyuBot Version: { cfg ["version" ]}
4664 Development by 510208, Thanks for using!
4765
4866"""
@@ -57,6 +75,7 @@ async def on_ready():
5775 slash = await bot .tree .sync ()
5876 if slash :
5977 logging .info (f'指令同步完成:{ slash } ' )
78+ await write_slash_synced (slash )
6079 else :
6180 logging .error ('指令同步失敗' )
6281
@@ -297,13 +316,15 @@ async def main():
297316# 確定執行此py檔才會執行
298317async def close_bot ():
299318 # 卸載全部Cog
319+ logging .info ('卸載全部Cogs' )
300320 for cog in bot .cogs :
301321 logging .info (f'卸載{ cog } 中...' )
302322 try :
303323 bot .unload_extension (f'Cogs.{ cog } ' )
304324 logging .info (f'卸載{ cog } 成功' )
305325 except Exception as e :
306326 logging .error (f'卸載{ cog } 失敗:{ e } ' )
327+ logging .info ('Bot關閉中...' )
307328 await bot .close ()
308329 logging .info ('Bot已關閉,謝謝使用!' )
309330
0 commit comments