test 4345

This commit is contained in:
Lou 2025-11-20 20:48:18 +01:00
parent 6f7616823f
commit 2b0a93c39f
4 changed files with 104 additions and 84 deletions

116
db.js
View file

@ -1,8 +1,8 @@
import Database from 'better-sqlite3';
import fs from 'fs';
// Initialisation (crée le fichier s'il n'existe pas)
export const db = new Database('./db.sqlite');
const DB_PATH = './db.json';
// Valeurs par défaut
const settingsDefaultValues = {
quoiAnswerPercentage: 100,
quoicoubehAnswerPercentage: 100,
@ -12,73 +12,81 @@ const settingsDefaultValues = {
ignoredRoleId: null,
};
function initDb() {
// .exec pour les scripts sans paramètres
db.exec(`
CREATE TABLE IF NOT EXISTS IgnoredChannels (
channelId VARCHAR PRIMARY KEY,
guildId VARCHAR
);
`);
// Structure de base de la DB en mémoire
let dbData = {
ignoredChannels: [], // Liste d'objets { channelId, guildId }
configs: {} // Objet : guildId -> { settings }
};
db.exec(`
CREATE TABLE IF NOT EXISTS Config (
guildId VARCHAR PRIMARY KEY,
quoiAnswerPercentage INTEGER NOT NULL DEFAULT ${settingsDefaultValues.quoiAnswerPercentage},
quoicoubehAnswerPercentage INTEGER NOT NULL DEFAULT ${settingsDefaultValues.quoicoubehAnswerPercentage},
feurAnswerPercentage INTEGER NOT NULL DEFAULT ${settingsDefaultValues.feurAnswerPercentage},
mentionAnswerPercentage INTEGER NOT NULL DEFAULT ${settingsDefaultValues.mentionAnswerPercentage},
forcedAnswerRoleId VARCHAR,
ignoredRoleId VARCHAR
);
`);
// --- Fonctions internes (Sauvegarde/Chargement) ---
function loadDb() {
if (!fs.existsSync(DB_PATH)) {
saveDb(); // Crée le fichier s'il n'existe pas
return;
}
try {
const raw = fs.readFileSync(DB_PATH, 'utf-8');
dbData = JSON.parse(raw);
// Sécurité : s'assurer que les structures existent
if (!dbData.ignoredChannels) dbData.ignoredChannels = [];
if (!dbData.configs) dbData.configs = {};
} catch (e) {
console.error("Erreur lecture DB, réinitialisation :", e);
saveDb();
}
}
function saveDb() {
fs.writeFileSync(DB_PATH, JSON.stringify(dbData, null, 2), 'utf-8');
}
// Initialisation au démarrage
loadDb();
// --- Fonctions exportées (Même signature que l'ancien db.js) ---
export function addChannelToIgnoreList(channelId, guildId) {
return db.prepare(`
INSERT INTO IgnoredChannels (channelId, guildId)
VALUES (?, ?)
ON CONFLICT(channelId) DO NOTHING
`).run(channelId, guildId);
// Vérifier si déjà présent
const exists = dbData.ignoredChannels.some(c => c.channelId === channelId);
if (!exists) {
dbData.ignoredChannels.push({ channelId, guildId });
saveDb();
}
}
export function getIgnoredChannels(guildId) {
return db.prepare(`
SELECT channelId
FROM IgnoredChannels
WHERE guildId = ?
`).all(guildId);
return dbData.ignoredChannels.filter(c => c.guildId === guildId);
}
export function removeChannelFromIgnoreList(channelId) {
return db.prepare(`
DELETE FROM IgnoredChannels
WHERE channelId = ?
`).run(channelId);
const initialLength = dbData.ignoredChannels.length;
dbData.ignoredChannels = dbData.ignoredChannels.filter(c => c.channelId !== channelId);
if (dbData.ignoredChannels.length !== initialLength) {
saveDb();
}
}
export function setSetting(guildId, setting, value) {
// ${setting} est injecté directement (nom de colonne)
return db.prepare(`
INSERT INTO Config (guildId, ${setting})
VALUES (?, ?)
ON CONFLICT(guildId) DO UPDATE SET ${setting} = ?
`).run(guildId, value, value);
// Initialiser la config de la guilde si inexistante
if (!dbData.configs[guildId]) {
dbData.configs[guildId] = { ...settingsDefaultValues };
}
// Mettre à jour la valeur
dbData.configs[guildId][setting] = value;
saveDb();
}
export function getSetting(guildId, setting) {
// .pluck() permet de récupérer directement la valeur au lieu d'un objet { colonne: valeur }
const result = db.prepare(`
SELECT ${setting}
FROM Config
WHERE guildId = ?
`).pluck().get(guildId);
if (result === undefined) {
const config = dbData.configs[guildId];
if (!config) {
return settingsDefaultValues[setting];
}
return result;
}
initDb();
const val = config[setting];
return val === undefined ? settingsDefaultValues[setting] : val;
}