WIP: Add basic code for storing settings in the DB
This commit is contained in:
45
database.ts
45
database.ts
@@ -1,7 +1,28 @@
|
|||||||
import { Sequelize } from "sequelize";
|
import { Model, Sequelize, STRING } from "sequelize";
|
||||||
|
|
||||||
|
import "node:fs/promises";
|
||||||
|
import { readFile } from "node:fs";
|
||||||
|
|
||||||
export const sql = new Sequelize("sqlite://./blitzcrank.sqlite");
|
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,
|
||||||
|
primaryKey: true,
|
||||||
|
},
|
||||||
|
key: { type: STRING, primaryKey: true },
|
||||||
|
value: { type: STRING },
|
||||||
|
},
|
||||||
|
{ sequelize: sql },
|
||||||
|
);
|
||||||
|
|
||||||
export async function initDb() {
|
export async function initDb() {
|
||||||
try {
|
try {
|
||||||
await sql.authenticate();
|
await sql.authenticate();
|
||||||
@@ -10,3 +31,25 @@ export async function initDb() {
|
|||||||
console.error("Unable to connect to the database:", 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
5
index.ts
5
index.ts
@@ -5,7 +5,7 @@ import type {
|
|||||||
SlashCommandOptionsOnlyBuilder,
|
SlashCommandOptionsOnlyBuilder,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
|
|
||||||
import { sql } from "./database";
|
import { sql, GuildSetting, initDb } from "./database";
|
||||||
|
|
||||||
const BLITZCRANK_BANNER = `
|
const BLITZCRANK_BANNER = `
|
||||||
****++++++++++*+++
|
****++++++++++*+++
|
||||||
@@ -145,6 +145,9 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
|
|||||||
|
|
||||||
client.once(Events.ClientReady, async (readyClient) => {
|
client.once(Events.ClientReady, async (readyClient) => {
|
||||||
await syncCommands();
|
await syncCommands();
|
||||||
|
initDb(); // TODO
|
||||||
|
GuildSetting.sync(); // TODO
|
||||||
|
|
||||||
for (const [_name, cmd] of commands) {
|
for (const [_name, cmd] of commands) {
|
||||||
await cmd?.initialize({
|
await cmd?.initialize({
|
||||||
client: client,
|
client: client,
|
||||||
|
|||||||
Reference in New Issue
Block a user