-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
96 lines (72 loc) · 3.01 KB
/
bot.py
File metadata and controls
96 lines (72 loc) · 3.01 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
import json
import os
import traceback
from datetime import datetime
import discord
from discord.ext import commands
async def generate_user_error_embed(ctx, message):
embed = discord.Embed(title="**Error**", description=message, colour=0xA50B06)
await ctx.send(embed=embed)
async def send_traceback(ctx, exception):
tb_lines = traceback.format_exception(type(exception), exception, exception.__traceback__, 4)
await ctx.send("An unexpected error occurred and will be logged ~\n```Python\n{}```".format(''.join(tb_lines)))
if not os.path.exists("logs"):
os.mkdir("logs")
today = datetime.utcnow()
path_pattern = "logs/{}-{}-{} %s.json".format(today.day, today.month, today.year)
i = 1
# First do an exponential search
while os.path.exists(path_pattern % i):
i = i * 2
# Result lies somewhere in the interval (i/2..i]
# We call this interval (a..b] and narrow it down until a + 1 = b
a, b = (i // 2, i)
while a + 1 < b:
c = (a + b) // 2 # interval midpoint
a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)
path = path_pattern % b
# Saving log to JSON file - easier to manage later on if needed
with open(path, "w") as logfile:
command = ctx.invoked_with
args = [str(arg) for arg in ctx.args]
kwargs = [str(kwargs) for kwargs in ctx.kwargs]
data = {"User ID": ctx.author.id,
"Command": command,
"Args": args,
"Kwargs": kwargs,
"Traceback": tb_lines}
json.dump(data, logfile, indent=4)
class Bot(commands.Bot):
def __init__(self):
prefix = "*"
intents = discord.Intents.default()
intents.members = True
super().__init__(command_prefix=prefix,
description="Pole",
intents=intents,
activity=discord.Activity(type=discord.ActivityType.listening, name=prefix + "help"))
self.cog_list = []
async def on_ready(self):
print(f"Name:\t{super().user.name}\nID:\t{super().user.id}")
async def on_command_error(self, ctx, exception):
# This prevents any commands with local handlers being handled here
if hasattr(ctx.command, "on_error"):
return
# This prevents any cogs with an overwritten cog_command_error being handled here
if ctx.cog and ctx.cog._get_overridden_method(ctx.cog.cog_command_error) is not None:
return
# This prevents errors being thrown if the command doesn't exist
if isinstance(exception, commands.errors.CommandNotFound):
return
# All stations good, send the error log!
await send_traceback(ctx, exception)
def run(self):
super().run(open("token.secret", "r").read())
def load_cog(self, cog):
super().load_extension(cog)
self.cog_list.append(cog)
if __name__ == "__main__":
bot = Bot()
bot.remove_command("help")
bot.load_cog("cogs.commands")
bot.run()