2018-12-27 10:56:24 +00:00
|
|
|
import json
|
2023-04-05 10:10:18 +00:00
|
|
|
import os
|
2018-12-27 10:56:24 +00:00
|
|
|
import time
|
|
|
|
|
2024-03-02 08:56:41 +00:00
|
|
|
from robocop_ng.helpers.data_loader import read_json
|
2023-10-09 20:56:13 +00:00
|
|
|
|
2020-04-20 22:05:32 +00:00
|
|
|
userlog_event_types = {
|
|
|
|
"warns": "Warn",
|
|
|
|
"bans": "Ban",
|
|
|
|
"kicks": "Kick",
|
|
|
|
"mutes": "Mute",
|
|
|
|
"notes": "Note",
|
|
|
|
}
|
2018-12-27 10:56:24 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def get_userlog_path(bot):
|
|
|
|
return os.path.join(bot.state_dir, "data/userlog.json")
|
|
|
|
|
|
|
|
|
|
|
|
def get_userlog(bot):
|
2024-03-02 08:56:41 +00:00
|
|
|
return read_json(bot, get_userlog_path(bot))
|
2018-12-27 10:56:24 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def set_userlog(bot, contents):
|
|
|
|
with open(get_userlog_path(bot), "w") as f:
|
2018-12-27 10:56:24 +00:00
|
|
|
f.write(contents)
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def fill_userlog(bot, userid, uname):
|
|
|
|
userlogs = get_userlog(bot)
|
2020-01-08 10:08:38 +00:00
|
|
|
uid = str(userid)
|
2019-03-02 18:00:55 +00:00
|
|
|
if uid not in userlogs:
|
2020-04-20 22:05:32 +00:00
|
|
|
userlogs[uid] = {
|
|
|
|
"warns": [],
|
|
|
|
"mutes": [],
|
|
|
|
"kicks": [],
|
|
|
|
"bans": [],
|
|
|
|
"notes": [],
|
|
|
|
"watch": False,
|
|
|
|
"name": "n/a",
|
|
|
|
}
|
2019-03-02 18:00:55 +00:00
|
|
|
if uname:
|
|
|
|
userlogs[uid]["name"] = uname
|
2020-01-08 10:08:38 +00:00
|
|
|
|
|
|
|
return userlogs, uid
|
2020-04-20 22:05:32 +00:00
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def userlog(bot, uid, issuer, reason, event_type, uname: str = ""):
|
|
|
|
userlogs, uid = fill_userlog(bot, uid, uname)
|
2020-01-08 10:08:38 +00:00
|
|
|
|
2019-03-02 18:00:55 +00:00
|
|
|
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
|
2020-04-20 22:05:32 +00:00
|
|
|
log_data = {
|
|
|
|
"issuer_id": issuer.id,
|
|
|
|
"issuer_name": f"{issuer}",
|
|
|
|
"reason": reason,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
}
|
2019-03-02 18:00:55 +00:00
|
|
|
if event_type not in userlogs[uid]:
|
|
|
|
userlogs[uid][event_type] = []
|
|
|
|
userlogs[uid][event_type].append(log_data)
|
2023-04-05 10:10:18 +00:00
|
|
|
set_userlog(bot, json.dumps(userlogs))
|
2019-03-02 18:00:55 +00:00
|
|
|
return len(userlogs[uid][event_type])
|
|
|
|
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
def setwatch(bot, uid, issuer, watch_state, uname: str = ""):
|
|
|
|
userlogs, uid = fill_userlog(bot, uid, uname)
|
2019-03-02 18:00:55 +00:00
|
|
|
|
|
|
|
userlogs[uid]["watch"] = watch_state
|
2023-04-05 10:10:18 +00:00
|
|
|
set_userlog(bot, json.dumps(userlogs))
|
2019-03-02 18:00:55 +00:00
|
|
|
return
|