Last active
July 8, 2023 07:16
-
-
Save Guf-Hub/264af4dc9410a6b31a378bd1f11cc718 to your computer and use it in GitHub Desktop.
Сохранение лога юзера в телеграм боте в PropertiesService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersProperties { | |
constructor(property) { | |
this.property = property || "users"; | |
this.service = PropertiesService.getScriptProperties(); | |
} | |
get() { | |
return JSON.parse(this.service.getProperty(this.property)); | |
} | |
put(object) { | |
this.service.setProperty(this.property, JSON.stringify(object)); | |
} | |
clear(key, full = true) { | |
const object = this.get(); | |
if (key in object) { | |
if(full) delete object[key]; | |
else object[key] = {}; | |
this.put(object); | |
} | |
} | |
delete() { | |
this.service.deleteProperty(this.property); | |
} | |
} | |
function getUsers() { | |
return new UsersProperties().get(); | |
} | |
function getUser(userId) { | |
return new UsersProperties().get()[userId]; | |
} | |
function addUserAnswer(userId, answer, debug = false) { | |
const cache = new UsersProperties(); | |
const users = getUsers() || {}; | |
if (!users[userId]) { users[userId] = {}; } | |
Object.assign(users[userId], answer); | |
cache.put(users); | |
const result = cache.get(); | |
if (debug) console.log(JSON.stringify(result, null, 4)); | |
return result; | |
} | |
function deleteUser(userId) { | |
new UsersProperties().clear(userId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment