Custom path for data is now possible

This commit is contained in:
2022-09-15 12:45:29 +02:00
parent 7f3b5174ff
commit 02d945aef5
4 changed files with 88 additions and 22 deletions

19
bot.py
View File

@@ -25,10 +25,11 @@ async def start(app, msg):
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
user_locale = msg.from_user.language_code
data = configGet("data")
if f"{msg.from_user.id}.json" not in listdir(f"data{sep}users{sep}"):
if f"{msg.from_user.id}.json" not in listdir(f"{data}{sep}users{sep}"):
logWrite(f'Creating blank data file for {msg.from_user.id}')
jsonSave( f"data{sep}users{sep}{msg.from_user.id}.json", {"api_key": None, "linked": False, "context": {"action": None, "data": None}} )
jsonSave( f"{data}{sep}users{sep}{msg.from_user.id}.json", {"api_key": None, "linked": False, "context": {"action": None, "data": None}} )
if not userGet(msg.from_user.id, "linked"):
await msg.reply_text(locale("link_input", "msg", locale=user_locale), reply_markup=ForceReply(placeholder=locale("link", "fry", locale=user_locale)))
@@ -44,14 +45,15 @@ async def unlink(app, msg):
await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING)
user_locale = msg.from_user.language_code
data = configGet("data")
if not userGet(msg.from_user.id, "linked"):
await msg.reply_text(locale("not_linked", "msg", locale=user_locale))
else:
try:
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
del keys_storage[userGet(msg.from_user.id, "api_key")]
jsonSave(f"data{sep}keys_storage.json", keys_storage)
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
except:
pass
userClear(msg.from_user.id, "api_key")
@@ -87,14 +89,15 @@ async def any_message_handler(app, msg):
if userGet(msg.from_user.id, "context") == "link_key":
user_locale = msg.from_user.language_code
data = configGet("data")
if msg.text in jsonLoad(configGet("api_keys"))["autozoom"]:
await msg.reply_text(locale("key_correct", "msg", locale=user_locale))
userSet(msg.from_user.id, "api_key", msg.text)
userSet(msg.from_user.id, "linked", True)
keys_storage = jsonLoad(f"data{sep}keys_storage.json")
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
keys_storage[msg.text] = msg.from_user.id
jsonSave(f"data{sep}keys_storage.json", keys_storage)
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
logWrite(f"Added apikey {msg.text} for user {msg.from_user.id}")
else:
logWrite(f"User {msg.from_user.id} tried to pair with invalid apikey {msg.text}")
@@ -110,8 +113,10 @@ if __name__ == "__main__":
app.start()
app.send_message(configGet("admin"), f"Starting bot with pid `{pid}`")
locales = configGet("locales")
for entry in listdir("locale"):
for entry in listdir(locales):
if entry.endswith(".json"):