-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (161 loc) · 5.52 KB
/
main.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#各種使用的模組
"""======================================================================================="""
import discord
import random
import json
from pathlib import Path
from functools import wraps
"""======================================================================================="""
intents = discord.Intents.all()
json_dir = Path(__file__).resolve().parent / "json"
with open(json_dir / "Setting.json", "r", encoding="utf8") as jfile:
Setting = json.load(jfile)
with open(json_dir / "Token.json", "r", encoding="utf8") as jfile:
Token = json.load(jfile)
bot = discord.Bot()
def Guild_Admin_Examine(func):
@wraps(func)
async def wrapper(ctx, *args, **kwargs):
try:
if ctx.author.guild_permissions.administrator:
return await func(ctx, *args, **kwargs)
else:
await ctx.respond("你沒有管理者權限用來執行這個指令")
except AttributeError:
await ctx.respond("你不在伺服器內")
return wrapper
def Cogs_NotLoaded(ctx: discord.AutocompleteContext):
query = ctx.value.lower()
cogs_path = Path('./cogs')
CogsList = [file.stem for file in cogs_path.glob('*.py')]
Cogs_Loaded = []
try:
for cog in [cog for cog in bot.cogs]:
Cogs_Loaded.append(cog)
except AttributeError:
pass
Only_Exists_NotLoaded = [cog for cog in CogsList if cog not in Cogs_Loaded]
return [
discord.OptionChoice(name=pic, value=pic)
for pic in Only_Exists_NotLoaded
if pic.lower().startswith(query)
]
def Cogs_Loaded(ctx: discord.AutocompleteContext):
query = ctx.value.lower()
Cogslist = []
try:
for cog in [cog for cog in bot.cogs]:
Cogslist.append(cog)
except AttributeError:
pass
return [
discord.OptionChoice(name=pic, value=pic)
for pic in Cogslist
if pic.lower().startswith(query)
]
#載入所有位於cogs的cog
def load_cogs():
cogs_path = Path('./cogs')
CogsList = [file.stem for file in cogs_path.glob('*.py')]
for filename in CogsList:
bot.load_extension(f'cogs.{filename}')
print(f"載入 {filename} 完成")
load_cogs()
@bot.event
async def on_ready():
#啟動時會在終端機印出的訊息
bot_name = f"Bot Logged in as {bot.user}"
border = "=" * 40
print(border)
print(f"= {bot_name.center(len(border) - 4)} =")
print(f"= {'>> Bot start <<'.center(len(border) - 4)} =")
print(border)
#bot的狀態顯示
await bot.change_presence(activity=discord.Game(name="/help general 來獲取指令列表"))
#用於加載、卸載、重讀不同cosg檔
"""======================================================================================="""
cogs = discord.SlashCommandGroup("cogs", "cogs management instructions")
@cogs.command(
description="加載指定的cog"
)
@discord.option(
"extension",
type=discord.SlashCommandOptionType.string,
description="cogs名稱",
autocomplete = Cogs_NotLoaded
)
@Guild_Admin_Examine
async def load(ctx, extension: str):
try:
bot.load_extension(f"cogs.{extension}")
await ctx.respond(f"{extension}模塊加載完成")
except Exception as e:
await ctx.respond(f"加載模塊時發生錯誤: {e}")
@cogs.command(
description="卸載指定的cog"
)
@discord.option(
"extension",
type=discord.SlashCommandOptionType.string,
description="cogs名稱",
autocomplete = Cogs_Loaded
)
@Guild_Admin_Examine
async def unload(ctx, extension: str):
try:
bot.unload_extension(f"cogs.{extension}")
await ctx.respond(f"{extension}模塊卸載完成")
except Exception as e:
await ctx.respond(f"卸載模塊時發生錯誤: {e}")
@cogs.command(
description="重載指定的cog"
)
@discord.option(
"extension",
type=discord.SlashCommandOptionType.string,
description="cogs名稱",
autocomplete = Cogs_Loaded
)
@Guild_Admin_Examine
async def reload(ctx, extension: str):
try:
bot.reload_extension(f"cogs.{extension}")
await ctx.respond(f"{extension}模塊重載完成")
except Exception as e:
await ctx.respond(f"重載模塊時發生錯誤: {e}")
@cogs.command(
description="列出已載入的cog"
)
@Guild_Admin_Examine
async def list(ctx):
loaded_cogs = Cogs_Loaded()
message = "已載入的 cog 如下:\n"
for cog in loaded_cogs:
message += f"* {cog}\n"
await ctx.respond(message)
bot.add_application_command(cogs)
"""======================================================================================="""
#測試bot的ping值
@bot.command(
description="測試bot的延遲",
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install
}
)
async def ping(ctx):
await ctx.respond(f"{round(bot.latency*1000)}(ms)")
#用來取得bot的邀請連結
@bot.command(
description="取得邀請連結",
integration_types={
discord.IntegrationType.guild_install,
discord.IntegrationType.user_install
}
)
async def invitation(ctx):
color = random.randint(0, 16777215)
embed=discord.Embed(title="------連結------", url=Setting["Invitation"], description="狠狠的點下去吧", color=color)
await ctx.respond(embed=embed)
if __name__ == "__main__":
bot.run(Token["Bot_Token"])