62 lines
1.3 KiB
TypeScript
62 lines
1.3 KiB
TypeScript
import {Model, Sequelize, STRING} from 'sequelize';
|
|
|
|
import 'node:fs/promises';
|
|
import {readFile} from 'node:fs';
|
|
|
|
export const sequelize = new Sequelize('sqlite://./blitzcrank.sqlite');
|
|
|
|
export class GuildSetting extends Model {
|
|
// Discord ID for the Guild (server)
|
|
declare guildId: string;
|
|
// Name of the option
|
|
declare key: string;
|
|
// Value being set
|
|
declare value?: string;
|
|
}
|
|
|
|
export async function initializeDatabase() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('Connected to database');
|
|
} catch (error) {
|
|
console.error('Unable to connect to the database:', error);
|
|
}
|
|
}
|
|
|
|
export async function initializeModels() {
|
|
GuildSetting.init(
|
|
{
|
|
guildId: {type: STRING},
|
|
key: {type: STRING},
|
|
value: {type: STRING},
|
|
},
|
|
{sequelize},
|
|
);
|
|
}
|
|
|
|
export async function syncModels() {
|
|
await GuildSetting.sync();
|
|
}
|
|
|
|
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);
|
|
}
|
|
});
|
|
}
|