-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGist_ErrorHandler.py
133 lines (101 loc) · 4.18 KB
/
Gist_ErrorHandler.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
from discord.ext import commands
import discord
from typing import List
import traceback
from pathlib import Path
import core.common
import asyncio
import requests
import yarl
import os
import json
from core.common import query
import logging
import random
logger = logging.getLogger(__name__)
'''
SETUP:
In order to use this error handler, you require an .env file with your GIST token! (Variable must be GIST)
Ex:
GIST = to_k_en
You can create a GIST Token here:
https://github.com/settings/tokens/new
Guide:
https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
Scopes Required:
GIST
'''
def random_rgb(seed=None):
if seed is not None:
random.seed(seed)
return discord.Colour.from_rgb(random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
def stackoverflow(q):
q = str(q)
baseUrl = "https://stackoverflow.com/search?q="
error = q.replace(" ","+")
error = error.replace(".","")
stackURL = baseUrl + error
return stackURL
class GithubError(commands.CommandError):
pass
class CustomError(Exception):
def __init__(self, times: int, msg: str):
self.times = times
self.msg = msg
self.pre = "This is a custom error:"
self.message = f"{self.pre} {self.msg*self.times}"
super().__init__(self.message)
class CommandErrorHandler(commands.Cog):
def __init__(self, bot):
self.bot = bot
logger.info("ErrorHandler: Cog Loaded!")
@commands.command() #method of testing the error handler!
async def error(self, ctx, times: int = 20, msg="error"):
raise CustomError(int(times), msg)
# Checks if the command has a local error handler.
@commands.Cog.listener()
async def on_command_error(self, ctx, error: Exception):
tb = error.__traceback__
etype = type(error)
exception = traceback.format_exception(etype, error, tb, chain=True) #Format the Exception so it includes every detail (line number, full stack traceback.
exception_msg = ""
sturl = stackoverflow(error)
for line in exception: #Writes to a file so you can see the latest traceback.
exception_msg += line
if isinstance(error, commands.CheckFailure) or isinstance(error, commands.CheckAnyFailure):
return
if hasattr(ctx.command, 'on_error'):
return
elif isinstance(error, commands.CommandNotFound):
config, _ = core.common.load_config()
print("Ignored error: " + str(ctx.command))
return
else:
error_file = Path("error.txt")
error_file.touch()
with error_file.open("w") as f:
f.write(exception_msg)
with error_file.open("r") as f:
config, _ = core.common.load_config()
data = "\n".join([l.strip() for l in f])
GITHUB_API="https://api.github.com"
API_TOKEN=os.getenv("GIST")
url=GITHUB_API+"/gists"
print(f"Request URL: {url}")
headers={'Authorization':'token %s'%API_TOKEN}
params={'scope':'gist'}
payload={"description":"PortalBot encountered a Traceback!","public":True,"files":{"error":{"content": f"{data}"}}}
res=requests.post(url,headers=headers,params=params,data=json.dumps(payload))
j=json.loads(res.text)
ID = j['id']
#ID = j.ID
gisturl = f"https://gist.github.com/{ID}"
print(gisturl)
embed = discord.Embed(title = "Beep Boop", description = "🚨 *I've ran into an issue!* 🚨\nThe Developers should get back to fixing that!", color = random_rgb())
embed.add_field(name = "Gist URL", value = f"**https://gist.github.com/{ID}**")
embed.add_field(name = "Stack Overflow", value = f"**{sturl}**", inline = False)
embed.set_footer(text = f"Error: {str(error)}")
await ctx.send(embed = embed)
error_file.unlink()
def setup(bot):
bot.add_cog(CommandErrorHandler(bot))