diff --git a/src/decorators.py b/src/decorators.py index b8d9a02..0fa0823 100644 --- a/src/decorators.py +++ b/src/decorators.py @@ -1,6 +1,6 @@ -import traceback import fnmatch import socket +import traceback import types from collections import defaultdict @@ -54,15 +54,7 @@ class handle_error: if not botconfig.PASTEBIN_ERRORS or botconfig.CHANNEL != botconfig.DEV_CHANNEL: cli.msg(botconfig.CHANNEL, msg) if botconfig.PASTEBIN_ERRORS and botconfig.DEV_CHANNEL: - try: - with socket.socket() as sock: - sock.connect(("termbin.com", 9999)) - sock.send(traceback.format_exc().encode("utf-8", "replace") + b"\n") - url = sock.recv(1024).decode("utf-8") - except socket.error: - pass - else: - cli.msg(botconfig.DEV_CHANNEL, " ".join((msg, url))) + pastebin_tb(cli, msg, traceback.format_exc()) class cmd: def __init__(self, *cmds, raw_nick=False, flag=None, owner_only=False, diff --git a/src/utilities.py b/src/utilities.py index c006cfe..ea8ea58 100644 --- a/src/utilities.py +++ b/src/utilities.py @@ -1,6 +1,11 @@ -import re import fnmatch import itertools +import json +import random +import re +import string +import traceback +import urllib import botconfig import src.settings as var @@ -428,6 +433,37 @@ def get_nick(cli, nick): return None return ul[ull.index(lnick)] +def pastebin_tb(cli, msg, exc): + try: + bot_id = re.sub(r"[^A-Za-z0-9-]", "-", botconfig.NICK) + bot_id = re.sub(r"--+", "-", bot_id) + bot_id = re.sub(r"^-+|-+$", "", bot_id) + + rand_id = "".join(random.sample(string.ascii_letters + string.digits, 8)) + + api_url = "https://ptpb.pw/~{0}-error-{1}".format(bot_id, rand_id) + + req = urllib.request.Request(api_url, urllib.parse.urlencode({ + "c": traceback.format_exc(), # contents + "s": 86400 # expiry (seconds) + }).encode("utf-8", "replace")) + + req.add_header("Accept", "application/json") + resp = urllib.request.urlopen(req) + data = json.loads(resp.read().decode("utf-8")) + url = data["url"] + except urllib.error.HTTPError as e: + if e.code == 409: # paste ID conflict + pastebin_tb(exc) # retry + else: + # Make sure we print the exception anyway + traceback.print_exc() + cli.msg(botconfig.DEV_CHANNEL, msg + " (Unable to pastebin traceback; please check the console.)") + except Exception: + traceback.print_exc() + cli.msg(botconfig.DEV_CHANNEL, msg + " (Unable to pastebin traceback; please check the console.)") + else: + cli.msg(botconfig.DEV_CHANNEL, " ".join((msg, url))) class InvalidModeException(Exception): pass