-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMain.py
307 lines (265 loc) · 10.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Imports
import Config
import discord
from discord.ext import commands, tasks
import logging
import random
import datetime
import importlib
import Utils
from EnchantedLogging import EnchantedLogging
if Config.testing:
intents = discord.Intents.default()
intents.members = True
intents.presences = True
logging.basicConfig(level=logging.INFO, format="Enchanted [%(levelname)s] | %(message)s")
running_commands = 0
class UserInBlacklist(Exception):
"""Raised when a user is in the blacklist"""
pass
async def get_prefix(bot, message):
return commands.when_mentioned_or(Utils.fetch_prefix(message))(bot, message)
bot = commands.AutoShardedBot(command_prefix=get_prefix, case_insensitive=True)
bot.remove_command("help")
# Create Logging Method
Config.LOGGING = EnchantedLogging(bot, logging.INFO, "Enchanted `[{level}]` | {message}")
# Cogs
cogs = ["Eval", "Settings", "Profile", "Spells", "Matchmaking", "Chests", "Bosses", "Essentials", "Cosmetics",
"Leaderboard", "Shop", "Inventory", "Clans", "Blacklist", "TopGG", "Code", "Dungeons", "Tournament"]
if Config.testing:
cogs.remove("TopGG")
# Starts all cogs
for cog in cogs:
bot.load_extension("Cogs." + cog)
# Check to see if the user invoking the command is in the OWNERIDS Config
def owner(ctx):
return int(ctx.author.id) in Config.OWNERIDS
# Restarts and reloads all cogs
@bot.command(aliases=["retard"])
@commands.check(owner)
async def restart(ctx):
"""
Restart the bot.
"""
restarting = discord.Embed(
title="Restarting...",
color=Config.MAINCOLOR
)
msg = await ctx.send(embed=restarting)
the_cogs = cogs
if "TopGG" in the_cogs:
the_cogs.remove("TopGG")
for cog in the_cogs:
bot.reload_extension("Cogs." + cog)
restarting.add_field(name=f"{cog}", value="✅ Restarted!")
# await msg.edit(embed = restarting)
importlib.reload(Utils)
restarting.add_field(name="Utils module", value="Reloaded")
importlib.reload(Config)
restarting.add_field(name="Config", value="Reloaded")
restarting.title = "Bot Restarted"
await msg.edit(embed=restarting)
Config.LOGGING.info(
f"Bot has been restarted succesfully in {len(bot.guilds)} server(s) with {len(bot.users)} users by {ctx.author.name}#{ctx.author.discriminator} (ID - {ctx.author.id})!")
await msg.delete(delay=3)
if ctx.guild != None:
await ctx.message.delete(delay=3)
@bot.command()
@commands.check(owner)
async def clean_q(ctx):
"""
Restart queues.
"""
restarting = discord.Embed(
title="Cleaning queue",
color=Config.MAINCOLOR
)
msg = await ctx.send(embed=restarting)
for cog in ["Bosses", "Clans", "Matchmaking"]:
bot.reload_extension("Cogs." + cog)
restarting.add_field(name=f"{cog}", value="✅ Restarted!")
# await msg.edit(embed = restarting)
restarting.title = "Queue Cleaned!"
await msg.edit(embed=restarting)
await msg.delete(delay=5)
if ctx.guild != None:
await ctx.message.delete(delay=5)
@bot.command()
@commands.check(owner)
async def reload(ctx, *, command: str = None):
"""
Restart queues.
"""
if command is None:
return
restarting = discord.Embed(
title="Reloading",
color=Config.MAINCOLOR
)
msg = await ctx.send(embed=restarting)
if command == "Utils":
importlib.reload(Utils)
elif command == "Config":
importlib.reload(Config)
else:
bot.reload_extension("Cogs." + command)
restarting.add_field(name=f"{command}", value="✅ Restarted!")
await msg.edit(embed=restarting)
restarting.title = "Reloading done!"
await msg.edit(embed=restarting)
await msg.delete(delay=5)
if ctx.guild != None:
await ctx.message.delete(delay=5)
@bot.before_invoke
async def before_commands(ctx):
if Config.testing:
if ctx.guild.id == 1:
return
global running_commands
running_commands += 1
Config.LOGGING.info("Command " + ctx.command.name + " Started by " + ctx.author.name + " | "
+ str(running_commands) + " commands running")
@bot.after_invoke
async def after_commands(ctx):
global running_commands
running_commands -= 1
Config.LOGGING.info(
"Command " + ctx.command.name + " Started by " + ctx.author.name + " Was completed alright." + " | "
+ str(running_commands) + " commands running")
@bot.check
async def check(ctx):
if Config.testing:
if ctx.guild.id == 1:
return False
if ctx.author.id in Config.OWNERIDS:
return True
if Config.MAINTENANCE is True and ctx.author.id not in Config.OWNERIDS:
return False
elif ctx.command.name in Config.DISABLED and ctx.author.id not in Config.OWNERIDS:
return False
else:
if ctx.command.name not in ["blacklist", "add", "remove"]:
server = Config.SERVERS.find_one({'guild_id': ctx.guild.id})
if server is not None:
return not ctx.channel.id in server['channel_blacklist']
else:
return True
else:
return True
# Command error
@bot.event
async def on_command_error(ctx, error):
if Config.testing:
if ctx.guild.id == 1:
pass
if isinstance(error, commands.CommandNotFound):
pass
elif isinstance(error, commands.UserInputError):
embed = discord.Embed(
title="Oh no.",
description=f"Looks like you used that command wrong:\n```{error}```",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
elif isinstance(error, UserInBlacklist):
embed = discord.Embed(
title="Oh no.",
description=f"It looks like you have been blacklisted from using Enchanted. You can join the [Support Server](https://discord.com/) and appeal by opening a ticket.",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
elif isinstance(error, commands.CheckFailure):
if Config.MAINTENANCE is True:
embed = discord.Embed(
title="Uh oh..",
description=f"Enchanted is currently in maintenance...",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
elif ctx.command.name in Config.DISABLED:
embed = discord.Embed(
title="Uh oh..",
description=f"This command has been disabled (temporarily)...",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title="Uh oh..",
description=f"You can't use this command here...",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
elif isinstance(error, discord.errors.Forbidden):
try:
await Config.LOGGING.error("Missing permissions to post.")
except discord.errors.Forbidden:
print("Missing permission to post missing permission error in logging channel")
else:
try:
embed = discord.Embed(
title="Error",
description=f"An error has occurred while executing this command:\n```{error}```",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
except discord.errors.Forbidden:
try:
await Config.LOGGING.error("\n```" + str(error) + "```")
except discord.errors.Forbidden:
print("Missing permission to post missing permission error in logging channel")
raise error
@tasks.loop(seconds=30)
async def status_set():
# Main #
statuses = ["heavenly beings smile", "flutters of the night", "the sky twinkle", "trees bend", "the fairies dance",
"leaves fall", "every star", "grass wave", "bees buzz", "stardust sprinkle", "tadpoles play",
"waterfalls"]
spooktober_statuses = ["the ghosts", "the graveyard", "the devil laugh", "the vampires screeching",
"the wolfs growl", "the demonic creatures", "the dead", "the dead souls", "the nightmares",
"the creatures growl", "the orks", "the people panic", "burning houses", "souls rise"]
snow_statuses = ["the snow fall", "the Ice Golem", "the reindeer", "the snowball fights", "santa fly by",
"the bells jingle", "the snowflakes tumble", "the children laughing"]
easter_statuses = ["chicks hatching", "the Easter Bunny", "grass sway", "daffodils bloom", "Easter Island",
"bunnies hop", "eggs being eaten", "eggs being hidden", "lambs jump hedges", "chicks cheep"]
if Config.MAINTENANCE is True:
status = discord.Status.dnd
else:
status = discord.Status.online
await bot.change_presence(status=status, activity=discord.Activity(type=discord.ActivityType.watching,
name=random.choice(statuses) + " | ]help"))
# Starts info log sending
@tasks.loop(seconds=300)
async def send_info_logs():
await Config.LOGGING.send_logs()
@bot.check
async def check_blacklist(ctx):
if Config.BLACKLIST.find_one({'user_id': ctx.author.id}) is not None:
embed = discord.Embed(
title="Oh no.",
description=f"It looks like you have been blacklisted from using Enchanted. You can join the [Support Server](https://discord.com/) and appeal by opening a ticket.",
color=Config.ERRORCOLOR
)
await ctx.send(embed=embed)
raise UserInBlacklist
else:
return True
# On ready
@bot.event
async def on_ready():
Config.LOGGING.info(f"__Bot has started successfully in {len(bot.guilds)} server(s)__") # with {len(bot.users)} users")
status_set.start()
send_info_logs.start()
if not hasattr(bot, "uptime"):
bot.uptime = datetime.datetime.utcnow()
# shard on_ready
@bot.event
async def on_shard_ready(shard_id):
Config.LOGGING.info(f"Shard ID {shard_id} is online and connected to discord gateway")
# cache all classes
classes = []
for _class in Utils.get_all_classes():
classes.append(_class["name"])
Config.ALL_CLASSES = classes
# Starts bot
bot.run(Config.TOKEN)