Skip to content

Commit

Permalink
Add privacy policy command
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jul 4, 2024
1 parent 00d6b1b commit fe93e93
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ s - Alias of sign
tsign - Sign a message with your tripcode
t - Alias of tsign
motd - Show the welcome message
privacy - Show privacy policy
version - Get version & source code of this bot
modhelp - Show commands available to moderators
adminhelp - Show commands available to admins
Expand Down
20 changes: 11 additions & 9 deletions secretlounge_ng/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,20 @@ def get_users(user: User):
total=active + inactive + black)

@requireUser
def get_motd(user: User):
motd = db.getSystemConfig().motd
if not motd:
return
return rp.Reply(rp.types.CUSTOM, text=motd)
def get_system_text(user: User, key: str):
if key not in ("motd", "privacy"):
raise ValueError()
v = getattr(db.getSystemConfig(), key)
if v:
return rp.Reply(rp.types.CUSTOM, text=v)

@requireUser
@requireRank(RANKS.admin)
def set_motd(user: User, arg):
def set_system_text(user: User, key: str, arg: str):
if key not in ("motd", "privacy"):
raise ValueError()
with db.modifySystemConfig() as config:
config.motd = arg
logging.info("%s set motd to: %r", user, arg)
setattr(config, key, arg)
logging.info("%s set %s to: %r", user, key, arg)
return rp.Reply(rp.types.SUCCESS)

@requireUser
Expand Down
18 changes: 10 additions & 8 deletions secretlounge_ng/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
class SystemConfig():
def __init__(self):
self.motd = None
self.privacy = None
def defaults(self):
self.motd = ""
self.privacy = ""

USER_PROPS = (
"id", "username", "realname", "rank", "joined", "left", "lastActive",
Expand Down Expand Up @@ -179,20 +181,18 @@ def close(self):
return
@staticmethod
def _systemConfigToDict(config):
return {"motd": config.motd}
return {"motd": config.motd, "privacy": config.privacy}
@staticmethod
def _systemConfigFromDict(d):
if d is None: return None
config = SystemConfig()
config.motd = d["motd"]
config.privacy = d.get("privacy")
return config
@staticmethod
def _userToDict(user):
props = ["id", "username", "realname", "rank", "joined", "left",
"lastActive", "cooldownUntil", "blacklistReason", "warnings",
"warnExpiry", "karma", "hideKarma", "debugEnabled", "tripcode"]
d = {}
for prop in props:
for prop in USER_PROPS:
value = getattr(user, prop)
if isinstance(value, datetime):
value = int(value.replace(tzinfo=timezone.utc).timestamp())
Expand All @@ -203,12 +203,13 @@ def _userFromDict(d):
if d is None: return None
props = ["id", "username", "realname", "rank", "blacklistReason",
"warnings", "karma", "hideKarma", "debugEnabled"]
props_d = [("tripcode", None)]
props_d = {"tripcode": None}
dateprops = ["joined", "left", "lastActive", "cooldownUntil", "warnExpiry"]
assert set(props).union(props_d.keys()).union(dateprops) == set(USER_PROPS)
user = User()
for prop in props:
setattr(user, prop, d[prop])
for prop, default in props_d:
for prop, default in props_d.items():
setattr(user, prop, d.get(prop, default))
for prop in dateprops:
if d[prop] is not None:
Expand Down Expand Up @@ -277,12 +278,13 @@ def close(self):
self.db.close()
@staticmethod
def _systemConfigToDict(config):
return {"motd": config.motd}
return {"motd": config.motd, "privacy": config.privacy}
@staticmethod
def _systemConfigFromDict(d):
if len(d) == 0: return None
config = SystemConfig()
config.motd = d["motd"]
config.privacy = d.get("privacy")
return config
@staticmethod
def _userToDict(user):
Expand Down
21 changes: 15 additions & 6 deletions secretlounge_ng/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def init(config: dict, _db, _ch):
"start", "stop", "users", "info", "motd", "toggledebug", "togglekarma",
"version", "source", "modhelp", "adminhelp", "modsay", "adminsay", "mod",
"admin", "warn", "delete", "remove", "uncooldown", "blacklist", "s", "sign",
"tripcode", "t", "tsign", "cleanup"
"tripcode", "t", "tsign", "cleanup", "privacy"
]
for c in cmds: # maps /<c> to the function cmd_<c>
c = c.lower()
Expand Down Expand Up @@ -563,13 +563,22 @@ def cmd_info(ev):
return send_answer(ev, core.get_info_mod(c_user, reply_msid), True)

@takesArgument(optional=True)
def cmd_motd(ev, arg):
def cmd_motd(ev: TMessage, arg):
c_user = UserContainer(ev.from_user)

if arg == "":
send_answer(ev, core.get_motd(c_user), reply_to=True)
else:
send_answer(ev, core.set_motd(c_user, arg), reply_to=True)
m = core.set_system_text(c_user, "motd", arg) if arg else None
if not m:
m = core.get_system_text(c_user, "motd")
send_answer(ev, m, reply_to=True)

@takesArgument(optional=True)
def cmd_privacy(ev: TMessage, arg):
c_user = UserContainer(ev.from_user)

m = core.set_system_text(c_user, "privacy", arg) if arg else None
if not m:
m = core.get_system_text(c_user, "privacy")
send_answer(ev, m, reply_to=True)

cmd_toggledebug = wrap_core(core.toggle_debug)
cmd_togglekarma = wrap_core(core.toggle_karma)
Expand Down

0 comments on commit fe93e93

Please sign in to comment.