languages/config improvements & bug fixes

This commit is contained in:
DJj123dj
2024-10-02 17:36:08 +02:00
parent 28f0d974e5
commit 6a8a8dd34d
31 changed files with 197 additions and 64 deletions
+6
View File
@@ -370,6 +370,12 @@ export class ODManager<DataType extends ODManagerData> extends ODManagerChangeHe
getIds(): ODId[] {
return this.#data.map((d) => d.id)
}
/**Run an iterator over all data in this manager. This method also supports async-await behaviour!*/
async forEach(cb:(data:DataType,id:ODId) => ODPromiseVoid): Promise<void> {
for (const data of this.getAll()){
await cb(data,data.id)
}
}
/**Use the open ticket debugger in this manager for logs*/
useDebug(debug?:ODDebugger, debugname?:string){
this.#debug = debug
+2 -2
View File
@@ -65,7 +65,7 @@ export class ODCheckerManager extends ODManager<ODChecker> {
if (!res.valid) isValid = false
})
this.functions.getAll().forEach((func) => {
this.functions.forEach((func) => {
const res = func.func(this,this.functions)
final.push(...res.messages)
@@ -1209,7 +1209,7 @@ export class ODCheckerCustomStructure_EmojiString extends ODCheckerStringStructu
}else if (!allowCustomDiscordEmoji && /<a?:[^:]*:[0-9]+>/.test(value)){
checker.createMessage("openticket:emoji-custom","error",`This emoji can't be a custom discord emoji!`,lt,null,[],this.id,(this.options.docs ?? null))
return false
}else if (!/^(?:(?:\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])|(?:<a?:[^:]*:[0-9]+>))*$/.test(value)){
}else if (!/^(?:\p{Emoji}|\p{Emoji_Component}|(?:<a?:[^:]*:[0-9]+>))*$/u.test(value)){
checker.createMessage("openticket:emoji-invalid","error","This is an invalid emoji!",lt,null,[],this.id,(this.options.docs ?? null))
return false
}
+3 -3
View File
@@ -357,7 +357,7 @@ export class ODClientActivityManager {
this.status = status
}
/**When initiating the status, the bot starts updating the status using `discord.js`. Returns if succesfull or not. */
/**When initiating the status, the bot starts updating the status using `discord.js`. Returns `true` when successfull. */
initStatus(): boolean {
if (this.initiated || !this.manager.ready) return false
this.#updateClientActivity(this.type,this.text)
@@ -496,7 +496,7 @@ export class ODSlashCommandManager extends ODManager<ODSlashCommand> {
const existing: {cmd:ODSlashCommand, requiresUpdate:boolean}[] = []
const nonExisting: ODSlashCommand[] = []
this.getAll().forEach((cmd) => {
this.forEach((cmd) => {
if (guildId && cmd.guildId != guildId) return
const result = cmds.find((cmddata) => cmddata.name == cmd.name)
if (result){
@@ -977,7 +977,7 @@ export class ODTextCommandManager extends ODManager<ODTextCommand> {
//filter commands for correct prefix
const validPrefixCommands: {cmd:ODTextCommand,newContent:string}[] = []
this.getAll().forEach((cmd) => {
this.forEach((cmd) => {
if (msg.content.startsWith(cmd.builder.prefix)) validPrefixCommands.push({
cmd:cmd,
newContent:msg.content.substring(cmd.builder.prefix.length)
+35 -4
View File
@@ -86,15 +86,46 @@ export class ODConfig extends ODManagerData {
* const config = new api.ODJsonConfig("plugin-config","test.json","./plugins/testplugin/")
*/
export class ODJsonConfig extends ODConfig {
/**An array of listeners to run when the config gets reloaded. These are not executed on the initial loading. */
#reloadListeners: Function[] = []
constructor(id:ODValidId, file:string, customPath?:string){
super(id)
try {
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./config/",this.file)
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./config/",this.file)
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse config \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
try{
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Config \""+nodepath.join("./",customPath ?? "./config/",file)+"\" doesn't exist!")
throw new ODSystemError("Unable to parse config \""+nodepath.join("./",this.path)+"\"!")
}
}
/**Reload the JSON file. Be aware that this doesn't update classes that used individual parts of the config data! */
reload(){
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to reload config \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
try{
this.data = JSON.parse(fs.readFileSync(this.path).toString())
this.#reloadListeners.forEach((cb) => {
try{
cb()
}catch(err){
process.emit("uncaughtException",err)
}
})
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Unable to reload config \""+nodepath.join("./",this.path)+"\"!")
}
}
/**Listen for a reload of this JSON file! */
onReload(cb:Function){
this.#reloadListeners.push(cb)
}
/**Remove all reload listeners. Not recommended! */
removeAllReloadListeners(){
this.#reloadListeners = []
}
}
+2 -2
View File
@@ -139,7 +139,7 @@ export class ODCounterCooldown extends ODCooldown<{value:number}> {
async init(){
if (this.ready) return
setInterval(() => {
this.data.getAll().forEach((cooldown) => {
this.data.forEach((cooldown) => {
cooldown.data.value = cooldown.data.value - this.decrement
if (cooldown.data.value <= this.cancelLimit){
cooldown.active = false
@@ -217,7 +217,7 @@ export class ODIncrementalCounterCooldown extends ODCooldown<{value:number}> {
async init(){
if (this.ready) return
setInterval(() => {
this.data.getAll().forEach((cooldown) => {
this.data.forEach((cooldown) => {
cooldown.data.value = cooldown.data.value - this.decrement
if (cooldown.data.value <= this.cancelLimit){
cooldown.active = false
+1 -1
View File
@@ -66,7 +66,7 @@ export class ODFlagManager extends ODManager<ODFlag> {
/**Set all flags to their `process.argv` value. */
init(){
this.getAll().forEach((flag) => {
this.forEach((flag) => {
flag.detectProcessParams(false)
})
}
+29 -10
View File
@@ -6,28 +6,33 @@ import nodepath from "path"
import { ODDebugger } from "./console"
import fs from "fs"
export interface ODLanguageMetadata {
otversion:string,
language:string,
translators:string[],
lastedited:string,
automated:boolean
}
export class ODLanguage extends ODManagerData {
/**The name of the file with `.json` extension. */
file: string
/**The path to the file relative to the main directory. */
path: string
data: any
metadata: {
otversion:string,
language:string,
translator:string,
lastedited:string
}|null = null
metadata: ODLanguageMetadata|null = null
constructor(id:ODValidId, file:string, customPath?:string){
super(id)
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
if (!fs.existsSync(this.path)) throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\", the file doesn't exist!")
try{
this.file = (file.endsWith(".json")) ? file : file+".json"
this.path = customPath ? nodepath.join("./",customPath,this.file) : nodepath.join("./languages/",this.file)
this.data = JSON.parse(fs.readFileSync(this.path).toString())
}catch(err){
process.emit("uncaughtException",err)
throw new ODSystemError("Language \""+nodepath.join("./",customPath ?? "./languages/",file)+"\" doesn't exist!")
throw new ODSystemError("Unable to parse language \""+nodepath.join("./",this.path)+"\"!")
}
if (this.data["_TRANSLATION"]) this.metadata = this.data["_TRANSLATION"]
}
@@ -36,27 +41,41 @@ export class ODLanguage extends ODManagerData {
export class ODLanguageManager extends ODManager<ODLanguage> {
current: ODLanguage|null = null
backup: ODLanguage|null = null
#debug: ODDebugger
constructor(debug:ODDebugger, presets:boolean){
super(debug,"language")
if (presets) this.add(new ODLanguage("english","english.json"))
this.current = presets ? new ODLanguage("english","english.json") : null
this.backup = presets ? new ODLanguage("english","english.json") : null
this.#debug = debug
}
setCurrentLanguage(id:ODValidId){
this.current = this.get(id)
const languageId = this.current?.id.value ?? "<unknown-id>"
const languageAutomated = this.current?.metadata?.automated.toString() ?? "<unknown-metadata>"
this.#debug.debug("Selected current language",[
{key:"id",value:languageId},
{key:"automated",value:languageAutomated},
])
}
getCurrentLanguage(){
return (this.current) ? this.current : null
}
setBackupLanguage(id:ODValidId){
this.backup = this.get(id)
const languageId = this.backup?.id.value ?? "<unknown-id>"
const languageAutomated = this.backup?.metadata?.automated.toString() ?? "<unknown-metadata>"
this.#debug.debug("Selected backup language",[
{key:"id",value:languageId},
{key:"automated",value:languageAutomated},
])
}
getBackupLanguage(){
return (this.backup) ? this.backup : null
}
getLanguageMetadata(frombackup?:boolean){
getLanguageMetadata(frombackup?:boolean): ODLanguageMetadata|null {
if (frombackup) return (this.backup) ? this.backup.metadata : null
return (this.current) ? this.current.metadata : null
}
+5 -5
View File
@@ -42,9 +42,9 @@ const preloadMigrationContext = async () => {
const unloadMigrationContext = async () => {
openticket.debug.visible = false
openticket.databases.getAll().forEach((database) => openticket.databases.remove(database.id))
openticket.configs.getAll().forEach((config) => openticket.configs.remove(config.id))
openticket.flags.getAll().forEach((flag) => openticket.flags.remove(flag.id))
openticket.databases.forEach((database,id) => {openticket.databases.remove(id)})
openticket.configs.forEach((config,id) => {openticket.configs.remove(id)})
openticket.flags.forEach((flag,id) => {openticket.flags.remove(id)})
openticket.debug.debug("-- MIGRATION CONTEXT END --")
}
@@ -78,7 +78,7 @@ const loadAllVersionMigrations = async (lastVersion:api.ODVersion) => {
const saveAllVersionsToDatabase = async () => {
const globalDatabase = openticket.databases.get("openticket:global")
openticket.versions.getAll().forEach((version) => {
globalDatabase.set("openticket:last-version",version.id.value,version.toString())
openticket.versions.forEach((version,id) => {
globalDatabase.set("openticket:last-version",id.value,version.toString())
})
}