53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
import { Model, Sequelize, STRING } from "sequelize";
|
|
|
|
import "node:fs/promises";
|
|
import { readFile } from "node:fs";
|
|
|
|
export const sql = new Sequelize("sqlite://./blitzcrank.sqlite");
|
|
|
|
export class GuildSetting extends Model {
|
|
declare guildId: string;
|
|
declare key: string;
|
|
declare value?: string;
|
|
}
|
|
|
|
GuildSetting.init(
|
|
{
|
|
guildId: { type: STRING },
|
|
key: { type: STRING },
|
|
value: { type: STRING },
|
|
},
|
|
{ sequelize: sql },
|
|
);
|
|
|
|
export async function initDb() {
|
|
try {
|
|
await sql.authenticate();
|
|
console.log("Connected to database");
|
|
} catch (error) {
|
|
console.error("Unable to connect to the database:", error);
|
|
}
|
|
}
|
|
|
|
export async function readSettingsFromFile(path: string) {
|
|
readFile(path, async (err, data) => {
|
|
try {
|
|
const json = JSON.parse(data.toString());
|
|
const toInsert: any[] = [];
|
|
for (const [guildId, guildSettings] of json) {
|
|
for (const [key, value] of guildSettings) {
|
|
toInsert.push({
|
|
guildId,
|
|
key,
|
|
value,
|
|
});
|
|
}
|
|
}
|
|
GuildSetting.bulkCreate(toInsert, { updateOnDuplicate: ["value"] });
|
|
} catch (error) {
|
|
// TODO
|
|
console.log("Could not read settings:", error);
|
|
}
|
|
});
|
|
}
|