diff --git a/README.md b/README.md index 119a3f8..48d9e33 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,5 @@ bun add fr-compromise conjugation-fr discord.js # version - 0.1.1 : initialisation et création du code - 0.2.1 : ajout de modules de détection -- 0.2.2 : rajout de nouveaux comportement (détection d'un feur par quelqu'un d'autre, ...) \ No newline at end of file +- 0.2.2 : rajout de nouveaux comportement (détection d'un feur par quelqu'un d'autre, ...) +- 0.3.1 : ajout de db (retention de setting) et maj des dependences \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..effde44 --- /dev/null +++ b/db.js @@ -0,0 +1,84 @@ +import Database from 'better-sqlite3'; + +// Initialisation (crée le fichier s'il n'existe pas) +export const db = new Database('./db.sqlite'); + +const settingsDefaultValues = { + quoiAnswerPercentage: 100, + quoicoubehAnswerPercentage: 100, + feurAnswerPercentage: 100, + mentionAnswerPercentage: 100, + forcedAnswerRoleId: null, + 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 + ); + `); + + 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 + ); + `); +} + +export function addChannelToIgnoreList(channelId, guildId) { + return db.prepare(` + INSERT INTO IgnoredChannels (channelId, guildId) + VALUES (?, ?) + ON CONFLICT(channelId) DO NOTHING + `).run(channelId, guildId); +} + +export function getIgnoredChannels(guildId) { + return db.prepare(` + SELECT channelId + FROM IgnoredChannels + WHERE guildId = ? + `).all(guildId); +} + +export function removeChannelFromIgnoreList(channelId) { + return db.prepare(` + DELETE FROM IgnoredChannels + WHERE channelId = ? + `).run(channelId); +} + +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); +} + +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) { + return settingsDefaultValues[setting]; + } + + return result; +} + +initDb(); \ No newline at end of file diff --git a/package.json b/package.json index 8a66215..521a4e2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,5 @@ { "name": "genius-troll", - "version": "1.0", - "description": "", "repository": { "type": "git", "url": "https://dev.ldelforge.com/TRUkae/genius-troll.git" @@ -17,6 +15,8 @@ "conjugation-fr": "^0.3.4", "discord.js": "^14.25.0", "dotenv": "^17.2.3", - "fr-compromise": "^0.2.8" + "fr-compromise": "^0.2.8", + "better-sqlite3": "^11.0.0" + } }