-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
445 lines (351 loc) · 13.1 KB
/
Copy pathmain.py
File metadata and controls
445 lines (351 loc) · 13.1 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import discord
import asyncio
import logging
import os
from datetime import datetime
import time
import aiohttp
from discord import app_commands, ui
from discord.ext import commands
from colorlog import ColoredFormatter
import traceback
import sys
sys.dont_write_bytecode = True
from utils.token import TOKEN
from utils.prefixes import *
from utils.variables import *
from utils.checks import *
from utils.helplund import *
from utils.loads import *
from utils.blacklists import *
from utils.paginator import Ahm
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True
client = commands.Bot(
command_prefix = get_prefix,
intents = intents,
case_insensitive = True,
strip_after_prefix = True,
)
client.remove_command("help")
handler = logging.StreamHandler()
formatter = ColoredFormatter(
"%(log_color)s%(asctime)s - %(levelname)-8s%(reset)s | %(message)s",
datefmt="%H:%M:%S",
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
}
)
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.handlers.clear()
logger.addHandler(handler)
@client.event
async def on_ready():
await client.change_presence(activity=discord.CustomActivity(name=".help | assisterbot.xyz"))
cmds = len(client.commands)
subcmds = sum(len(c.commands) for c in client.commands if hasattr(c, "commands"))
try:
synced = await client.tree.sync()
slash = len(synced)
except Exception as e:
logging.error(f"Slash sync failed: {e}")
slash = 0
logging.info(f"Logged : {client.user}")
logging.info(f"Guilds : {len(client.guilds)}")
logging.info(f"Users : {len(client.users)}")
logging.info(f"Prefix cmds: {cmds} | Subcmds: {subcmds}")
logging.info(f"Slash cmds : {slash}")
logging.info(f"Total cmds : {cmds + subcmds}")
async def _webhook_send(url: str, content: str):
async with aiohttp.ClientSession() as session:
webhook = discord.Webhook.from_url(url, session=session)
if len(content) <= 2000:
await webhook.send(content)
else:
for i in range(0, len(content), 1900):
await webhook.send(content[i:i+1900])
async def log_cooldown(cmd, user, guild, guild_id, user_id, retry_after):
msg = (
f"- ❗ **Cooldown Triggered**\n"
f" - **Guild:** {guild} (`{guild_id}`)\n"
f" - **User:** {user} (`{user_id}`)\n"
f" - **Command:** `{cmd}`\n"
f" - **Cooldown:** `{retry_after:.2f}s`"
)
await _webhook_send(ahmw2, msg)
async def log_command(cmd_usage, user, guild, guild_id, user_id, content, success, error=None):
status = "✅ Executed" if success else "❌ Failed"
msg = (
f"**Command Log**\n"
f"**Guild:** {guild} (`{guild_id}`)\n"
f"**User:** {user} (`{user_id}`)\n"
f"**Command:** `{cmd_usage}`\n"
f"**Raw:** `{content[:1000]}`\n"
f"**Status:** {status}\n"
)
if error:
err = error[:1500]
msg += f"```py\n{err}\n```"
await _webhook_send(ahmw1, msg)
_cooldown_notified: set = set()
async def _clear_cooldown_flag(user_id: int):
await asyncio.sleep(10)
_cooldown_notified.discard(user_id)
async def chuttt(ctx: commands.Context, **kwargs):
try:
if ctx.interaction:
if ctx.interaction.response.is_done():
await ctx.interaction.followup.send(**kwargs, ephemeral=True)
else:
await ctx.interaction.response.send_message(**kwargs, ephemeral=True)
else:
await ctx.reply(**kwargs, mention_author=False)
except (discord.NotFound, discord.HTTPException):
pass
GLOBAL_COOLDOWN_RATE = 1
GLOBAL_COOLDOWN_PER = 3
_last_command_time = {}
async def _check_global_cooldown(uid: int) -> tuple[bool, float]:
now = time.time()
last = _last_command_time.get(uid, 0)
if now - last < GLOBAL_COOLDOWN_PER:
return True, GLOBAL_COOLDOWN_PER - (now - last)
return False, 0.0
@client.check
async def global_cooldown_prefix(ctx: commands.Context):
uid = ctx.author.id
if uid in developer_team_ids:
return True
limited, retry_after = await _check_global_cooldown(uid)
if limited:
bucket = commands.Cooldown(GLOBAL_COOLDOWN_RATE, GLOBAL_COOLDOWN_PER)
raise commands.CommandOnCooldown(bucket, retry_after, commands.BucketType.user)
_last_command_time[uid] = time.time()
return True
async def _global_cooldown_slash(interaction: discord.Interaction):
uid = interaction.user.id
if uid in developer_team_ids:
return True
limited, retry_after = await _check_global_cooldown(uid)
if limited:
embed = discord.Embed(
description=f"❗ Command on cooldown. Try again in `{retry_after:.2f}s`.", color=colour,
)
embed.set_author(name="Assister Slowdown", icon_url=client.user.avatar.url if client.user else discord.Embed.Empty)
try:
if interaction.response.is_done():
await interaction.followup.send(embed=embed, ephemeral=True)
else:
await interaction.response.send_message(embed=embed, ephemeral=True)
except Exception:
pass
return False
_last_command_time[uid] = time.time()
return True
client.tree.interaction_check = _global_cooldown_slash
@client.event
async def on_command_error(ctx: commands.Context, error):
if isinstance(error, commands.CheckFailure):
return
if isinstance(error, commands.CommandOnCooldown):
uid = ctx.author.id
if uid in developer_team_ids:
ctx.command.reset_cooldown(ctx)
try:
await ctx.command.reinvoke(ctx, call_hooks=True)
except Exception:
pass
return
if uid not in _cooldown_notified:
_cooldown_notified.add(uid)
embed = discord.Embed(
description=f"❗ Command on cooldown. Try again in `{error.retry_after:.2f}s`.",
color=colour,
)
embed.set_author(name="Assister Slowdown", icon_url=client.user.avatar.url)
await chuttt(ctx, embed=embed, delete_after=6)
client.loop.create_task(_clear_cooldown_flag(uid))
await log_cooldown(
ctx.command.name, ctx.author.display_name,
ctx.guild.name, ctx.guild.id,
ctx.author.id, error.retry_after,
)
elif isinstance(error, commands.MissingRequiredArgument):
prefix = guild_prefix(str(ctx.guild.id)) if ctx.guild else "."
embed = discord.Embed(
description=(
f"{cross} | You missed the `{error.param.name}` argument\n"
f"{e_dot} Correct usage:```{prefix}{ctx.command.usage}```"
),
color=colour,
)
embed.set_author(
name=ctx.author,
icon_url=ctx.author.avatar.url if ctx.author.avatar else ctx.author.default_avatar.url,
)
await chuttt(ctx, embed=embed, delete_after=10)
elif isinstance(error, commands.MissingPermissions):
missing = ", ".join(p.replace("_", " ").title() for p in error.missing_permissions)
embed = discord.Embed(
description=f"## {animated_cross} **Missing Permissions**\n```py\nYou need: {missing}```",
color=colour,
)
embed.set_author(
name=ctx.author,
icon_url=ctx.author.avatar.url if ctx.author.avatar else ctx.author.default_avatar.url,
)
await chuttt(ctx, embed=embed)
elif isinstance(error, commands.BotMissingPermissions):
missing = ", ".join(p.replace("_", " ").title() for p in error.missing_permissions)
embed = discord.Embed(
description=f"## {animated_cross} **Bot Missing Permissions**\n```py\nI need: {missing}```",
color=colour,
)
embed.set_author(name=client.user, icon_url=client.user.avatar.url)
await chuttt(ctx, embed=embed)
elif isinstance(error, commands.MemberNotFound):
embed = discord.Embed(
description=f"{cross} | Member not found. That user is not in this server.",
color=colour,
)
embed.set_author(
name=ctx.author,
icon_url=ctx.author.avatar.url if ctx.author.avatar else ctx.author.default_avatar.url,
)
await chuttt(ctx, embed=embed, delete_after=10)
elif isinstance(error, commands.CommandNotFound):
return
else:
tb = "".join(traceback.format_exception(type(error), error, error.__traceback__))
logging.error(f"[COMMAND ERROR] {ctx.command} | {ctx.author} | {ctx.guild}\n{tb}")
await log_command(
ctx.command.name if ctx.command else "unknown",
ctx.author.display_name,
ctx.guild.name if ctx.guild else "DM",
ctx.guild.id if ctx.guild else "DM",
ctx.author.id,
ctx.message.content,
False,
tb,
)
await chuttt(
ctx,
embed=discord.Embed(
description=f"{cross} An unexpected error occurred.",
color=colour,
),
delete_after=8,
)
@client.event
async def on_command_completion(ctx: commands.Context):
try:
await log_command(
ctx.command.usage, ctx.author.display_name,
ctx.guild.name, ctx.guild.id,
ctx.author.id, ctx.message.content, True,
)
except Exception as e:
await log_command(
ctx.command.usage, ctx.author.display_name,
ctx.guild.name, ctx.guild.id,
ctx.author.id, ctx.message.content, False, str(e),
)
@client.hybrid_command(
aliases = ["allcmds", "allcommands", "cmds", "commands"],
usage = "commands",
description = "Shows all my commands",
)
@bled()
@commands.cooldown(1, 8, commands.BucketType.user)
async def mycommand(ctx: commands.Context):
cmds = len(client.commands)
subcmds = sum(len(c.commands) for c in client.commands if hasattr(c, "commands"))
total = cmds + subcmds
pages = []
for category, cmds in lul.items():
cmd_list = ", ".join(f"`{c}`" for c in cmds)
embed = discord.Embed(
description=f"## {category.capitalize()} — {len(cmds)} commands\n{cmd_list}\n{blnk}",
color=colour,
)
embed.set_footer(
text = f"{client.user.display_name} • Page {len(pages)+1}/{len(lul)} • Total: {total}",
icon_url = client.user.avatar.url,
)
embed.set_author(name=f"Total: {total} commands", icon_url=client.user.avatar.url)
pages.append(embed)
await Ahm(pages).start(ctx)
@client.command(aliases=['r'])
@is_developer()
async def reload(ctx, cog: str = None):
if cog is None:
return await ctx.send("Please specify a cog or use `all`.")
if cog.lower() == "all":
success = []
failed = []
for extension in list(client.extensions):
try:
await client.reload_extension(extension)
success.append(extension)
except Exception as e:
failed.append((extension, str(e)))
msg = f"{tick} Reloaded {len(success)} cogs."
if failed:
msg += f"\n{animated_cross} Failed: " + ", ".join(f"`{name}`" for name, _ in failed)
await ctx.send(msg)
else:
try:
await client.reload_extension(f"cogs.{cog}")
await ctx.send(f"{tick} Reloaded `{cog}`")
except Exception as e:
await ctx.send(f"{animated_cross} Error: `{e}`")
@client.command()
@is_developer()
async def cmdids(ctx):
cmds = await client.tree.fetch_commands()
lines = [f"`{c.name}` → `{c.id}`" for c in cmds]
chunks = []
current = ""
for line in lines:
if len(current) + len(line) + 1 > 2000:
chunks.append(current)
current = ""
current += line + "\n"
if current:
chunks.append(current)
if ctx.interaction:
await ctx.interaction.response.send_message(chunks[0], ephemeral=True)
for chunk in chunks[1:]:
await ctx.followup.send(chunk, ephemeral=True)
else:
for chunk in chunks:
await ctx.send(chunk)
async def load_extensions():
for root, _, files in os.walk("./cogs"):
for file in files:
if not file.endswith(".py"):
continue
rel = os.path.relpath(os.path.join(root, file), "./cogs").replace(os.sep, ".")
name = f"cogs.{rel[:-3]}"
try:
await client.load_extension(name)
print(f"Loaded {name}")
except Exception as e:
print(f"Failed {name}: {e}")
async def main():
await load_extensions()
try:
await client.load_extension("jishaku")
print("Loaded jishaku")
except Exception:
print("lund jishaku")
await client.start(TOKEN)
asyncio.run(main())