Merge pull request #62 from Mz49/patch-1

Condense repeated code to a function
This commit is contained in:
Ave 2020-01-08 15:51:54 +00:00 committed by GitHub
commit 2055a63eec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -18,9 +18,9 @@ def set_userlog(contents):
f.write(contents) f.write(contents)
def userlog(uid, issuer, reason, event_type, uname: str = ""): def fill_userlog(userid, uname):
userlogs = get_userlog() userlogs = get_userlog()
uid = str(uid) uid = str(userid)
if uid not in userlogs: if uid not in userlogs:
userlogs[uid] = {"warns": [], userlogs[uid] = {"warns": [],
"mutes": [], "mutes": [],
@ -31,6 +31,13 @@ def userlog(uid, issuer, reason, event_type, uname: str = ""):
"name": "n/a"} "name": "n/a"}
if uname: if uname:
userlogs[uid]["name"] = uname userlogs[uid]["name"] = uname
return userlogs, uid
def userlog(uid, issuer, reason, event_type, uname: str = ""):
userlogs, uid = fill_userlog(uid, uname)
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log_data = {"issuer_id": issuer.id, log_data = {"issuer_id": issuer.id,
"issuer_name": f"{issuer}", "issuer_name": f"{issuer}",
@ -44,19 +51,7 @@ def userlog(uid, issuer, reason, event_type, uname: str = ""):
def setwatch(uid, issuer, watch_state, uname: str = ""): def setwatch(uid, issuer, watch_state, uname: str = ""):
userlogs = get_userlog() userlogs, uid = fill_userlog(uid, uname)
uid = str(uid)
# Can we reduce code repetition here?
if uid not in userlogs:
userlogs[uid] = {"warns": [],
"mutes": [],
"kicks": [],
"bans": [],
"notes": [],
"watch": False,
"name": "n/a"}
if uname:
userlogs[uid]["name"] = uname
userlogs[uid]["watch"] = watch_state userlogs[uid]["watch"] = watch_state
set_userlog(json.dumps(userlogs)) set_userlog(json.dumps(userlogs))