ajout db
This commit is contained in:
parent
8f8343f2ca
commit
3def4c807e
3 changed files with 89 additions and 4 deletions
|
|
@ -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, ...)
|
||||
- 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
|
||||
84
db.js
Normal file
84
db.js
Normal file
|
|
@ -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();
|
||||
|
|
@ -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"
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue