Clarify transmutation failure and log errors to file

This commit is contained in:
Robert Miles 2020-03-14 20:07:40 -04:00
parent fc2db3cb6a
commit c2669b1d33
2 changed files with 23 additions and 4 deletions

View File

@ -3,6 +3,9 @@ from collections import defaultdict
from math import tan, atan, pi, log
import json
class UserDoesntHaveEnoughBadges(Exception):
pass
def quantile(x,m,p):
c=1-m
if x<=0.5: # x <= 1/2
@ -67,7 +70,7 @@ class BadgePopulation:
for badge in self.badges[user]:
if badge.name==badge_name: found=True
if not found:
raise Exception(f"User {user} does not have a {badge_name}!")
raise UserDoesntHaveEnoughBadges(f"User {user} does not have a {badge_name}!")
rarities = calculate_rarities(self.population)
badges = [rarities[name][2] for name in badge_names]
badges.insert(0,1-_random.random())

View File

@ -1,8 +1,15 @@
import plugin, badge, random, json, traceback
import plugin, badge, random, json, traceback, requests, io, importlib
from bot import IRCLine
from collections import Counter
importlib.reload(badge)
BOT = None
def pack_file(txt):
f = io.StringIO()
f.write(txt)
f.seek(0)
return {"file": ("tmp.txt",f,"text/plain")}
class BadgePopData(plugin.Data):
def serialize(self):
return json.dumps(self.value.to_json())
@ -76,10 +83,19 @@ def on_cmd_transmute(event):
return
try:
badge_result = population.value.transmute(account,*event.parts)
except:
traceback.print_exc()
except badge.UserDoesntHaveEnoughBadges:
respond(event,"You must have at least one (1) of each badge you wish to use in the transmutation.")
return
except:
url = "Error saving traceback! Tell khuxkm to check error.log!"
err = traceback.format_exc()
try:
with open("error.log","a") as f:
f.write(err+"\n"+("-"*80)+"\n")
r = requests.post("https://ttm.sh",files=pack_file(err))
url = r.text.strip()
except: pass
respond(event,"Something went wrong! Error: "+url)
respond(event,"You put in the {!s} badges above, and out pops a {}!".format(len(event.parts),badge_result))
population.value.give_badge(account,badge_result,False)
population.save("badges.json")