Cool, working, bedtimenow

This commit is contained in:
MSWS
2023-04-07 00:30:41 -07:00
parent a86ce4482e
commit 714bf9a321
6 changed files with 66 additions and 37 deletions

View File

@@ -1,3 +1,3 @@
{
"token": "$APP_DISCORD_TOKEN"
"TOKEN": "$APP_DISCORD_TOKEN"
}

View File

@@ -1,11 +1,11 @@
export interface BotConfig {
registerEntry(entry: ConfigEntry): void;
registerEntry(entry: ConfigEntry): ConfigEntry;
removeEntry(entry: ConfigEntry): void;
getConfigEntry(name: string): ConfigEntry | undefined;
getEntries(): Promise<ConfigEntry[]>;
getValue(name: string): Promise<unknown>;
getValue(id: string | ConfigEntry): Promise<unknown>;
save(): Promise<void>;
load(): Promise<void>;

View File

@@ -18,18 +18,21 @@ class Bot implements DSBot {
this.modules = new ModuleManager();
this.config = new FileConfig("config.json");
this.config.registerEntry(new TokenEntry());
this.config.load();
const tokenEntry = this.config.registerEntry(new TokenEntry());
this.client = new Client({ intents: ["Guilds"] });
this.config.getValue("token").then(token => {
this.client.login(token as string);
});
this.config.load().then(() => {
this.config.getValue(tokenEntry).then(token => {
console.log("Token: " + token);
this.client.login(token as string);
});
this.config.getValue("maul").then((result) => {
const conf = result as MaulConfig;
this.maul = new MAUL(conf.TOKEN, conf.URL, conf.IP);
this.config.getValue("MAUL").then((result) => {
const conf = result as MaulConfig;
this.maul = new MAUL(conf.TOKEN, conf.URL, conf.IP);
});
});
}
}
}
new Bot();

View File

@@ -1,7 +1,7 @@
import { ConfigEntry } from "../../api/data/config";
export class TokenEntry implements ConfigEntry {
name = "token";
name = "TOKEN";
value: unknown;
onUpdate(oldValue: unknown, newValue: unknown): void {

View File

@@ -13,36 +13,49 @@ export class FileConfig implements BotConfig {
return this.entries;
}
async getValue(name: string): Promise<unknown> {
return this.getConfigEntry(name)?.value;
async getValue(id: string | ConfigEntry): Promise<unknown> {
if (typeof id === "string")
return this.getConfigEntry(id)?.value;
for (let entry of this.entries)
if (entry.name === id.name)
return entry.value;
return undefined;
}
async load(): Promise<void> {
// Open the file and load the entries
fs.open(this.file, 'r', (err, fd) => {
if (err) {
console.error(err);
return;
}
// Parse the file as JSON
let data = JSON.parse(fs.readFileSync(fd, 'utf8'));
// For each key in the JSON object, create a new ConfigEntry
for (let key in data) {
const entry = this.getConfigEntry(key);
if(!entry) {
console.error("Config entry not found: " + key);
continue;
console.log("Loading config file: " + this.file);
const promise = new Promise<void>((resolve, reject) => {
fs.open(this.file, 'r', (err, fd) => {
if (err) {
console.error(err);
reject(err);
return;
}
entry.onUpdate(entry.value, data[key]);
}
// Parse the file as JSON
let data = JSON.parse(fs.readFileSync(fd, 'utf8'));
console.log("Parsed data: " + data);
console.log(JSON.stringify(data));
// For each key in the JSON object, create a new ConfigEntry
for (let key in data) {
const entry = this.getConfigEntry(key);
if (!entry) {
console.error("Config entry not found: " + key);
continue;
}
entry.onUpdate(entry.value, data[key]);
}
resolve();
});
});
return promise;
}
registerEntry(entry: ConfigEntry): void {
registerEntry(entry: ConfigEntry): ConfigEntry {
this.entries.push(entry);
return entry;
}
removeEntry(entry: ConfigEntry): void {
this.entries.splice(this.entries.indexOf(entry), 1);

View File

@@ -6,7 +6,19 @@ export class SQLConfig implements BotConfig {
constructor() {
this.entries = [];
}
getConfigEntry(name: string): ConfigEntry | undefined {
throw new Error("Method not implemented.");
}
getEntries(): Promise<ConfigEntry[]> {
throw new Error("Method not implemented.");
}
getValue(name: string): Promise<unknown> {
throw new Error("Method not implemented.");
}
load(): Promise<void> {
throw new Error("Method not implemented.");
}
removeEntry(entry: ConfigEntry): void {
throw new Error("Method not implemented.");
}
@@ -14,7 +26,8 @@ export class SQLConfig implements BotConfig {
throw new Error("Method not implemented.");
}
registerEntry(entry: ConfigEntry): void {
registerEntry(entry: ConfigEntry): ConfigEntry {
this.entries.push(entry);
return entry;
}
}