forked from smallshawn95/Python-Discord-Bot-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
45 lines (37 loc) · 1.31 KB
/
bot.py
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
import os, asyncio, discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "$", intents = intents)
# 當機器人完成啟動時
@bot.event
async def on_ready():
slash = await bot.tree.sync()
print(f"目前登入身份 --> {bot.user}")
print(f"載入 {len(slash)} 個斜線指令")
# 載入指令程式檔案
@bot.command()
async def load(ctx: commands.Context, extension):
await bot.load_extension(f"cogs.{extension}")
await ctx.send(f"Loaded {extension} done.")
# 卸載指令檔案
@bot.command()
async def unload(ctx: commands.Context, extension):
await bot.unload_extension(f"cogs.{extension}")
await ctx.send(f"UnLoaded {extension} done.")
# 重新載入程式檔案
@bot.command()
async def reload(ctx: commands.Context, extension):
await bot.reload_extension(f"cogs.{extension}")
await ctx.send(f"ReLoaded {extension} done.")
# 一開始bot開機需載入全部程式檔案
async def load_extensions():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await bot.load_extension(f"cogs.{filename[:-3]}")
async def main():
async with bot:
await load_extensions()
await bot.start("BOT TOKEN")
# 確定執行此py檔才會執行
if __name__ == "__main__":
asyncio.run(main())