Compare commits
2 commits
1b11a3728f
...
30968bcd5b
Author | SHA1 | Date | |
---|---|---|---|
30968bcd5b | |||
a5bddd8c6f |
12 changed files with 126 additions and 80 deletions
|
@ -9,6 +9,7 @@ import {
|
||||||
setTimeoutReminder,
|
setTimeoutReminder,
|
||||||
updateReminder,
|
updateReminder,
|
||||||
} from "../../utils/commands/reminder";
|
} from "../../utils/commands/reminder";
|
||||||
|
import { readSQL } from "../../utils/db";
|
||||||
|
|
||||||
export const once = true;
|
export const once = true;
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ export default async (client: Client) => {
|
||||||
// Restart all the timeout about reminders here
|
// Restart all the timeout about reminders here
|
||||||
new Promise((ok, ko) => {
|
new Promise((ok, ko) => {
|
||||||
// Fetch all reminders
|
// Fetch all reminders
|
||||||
client.db.all("SELECT * FROM reminder", [], (err, row) => {
|
client.db.all(readSQL("reminder/select"), [], (err, row) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
ko(err);
|
ko(err);
|
||||||
}
|
}
|
||||||
|
|
13
src/sql/init.sql
Normal file
13
src/sql/init.sql
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS
|
||||||
|
reminder (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
data TEXT,
|
||||||
|
expiration_date TEXT,
|
||||||
|
option_id INTEGER,
|
||||||
|
channel_id TEXT,
|
||||||
|
creation_date TEXT,
|
||||||
|
user_id TEXT,
|
||||||
|
guild_id TEXT,
|
||||||
|
locale TEXT,
|
||||||
|
timeout_id TEXT
|
||||||
|
);
|
14
src/sql/reminder/add.sql
Normal file
14
src/sql/reminder/add.sql
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
INSERT INTO
|
||||||
|
reminder (
|
||||||
|
data,
|
||||||
|
expiration_date,
|
||||||
|
option_id,
|
||||||
|
channel_id,
|
||||||
|
creation_date,
|
||||||
|
user_id,
|
||||||
|
guild_id,
|
||||||
|
locale,
|
||||||
|
timeout_id
|
||||||
|
)
|
||||||
|
VALUES
|
||||||
|
(?, ?, ?, ?, ?, ?, ?, ?, ?);
|
13
src/sql/reminder/find.sql
Normal file
13
src/sql/reminder/find.sql
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
SELECT
|
||||||
|
data,
|
||||||
|
creation_date,
|
||||||
|
expiration_date,
|
||||||
|
id
|
||||||
|
FROM
|
||||||
|
reminder
|
||||||
|
WHERE
|
||||||
|
user_id = ?
|
||||||
|
AND (
|
||||||
|
guild_id = ?
|
||||||
|
OR guild_id = 0
|
||||||
|
)
|
6
src/sql/reminder/findById.sql
Normal file
6
src/sql/reminder/findById.sql
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
reminder
|
||||||
|
WHERE
|
||||||
|
id = ?
|
14
src/sql/reminder/ownership_check.sql
Normal file
14
src/sql/reminder/ownership_check.sql
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
SELECT
|
||||||
|
EXISTS (
|
||||||
|
SELECT
|
||||||
|
1
|
||||||
|
FROM
|
||||||
|
reminder
|
||||||
|
WHERE
|
||||||
|
id = ?
|
||||||
|
AND user_id = ?
|
||||||
|
AND (
|
||||||
|
guild_id = ?
|
||||||
|
OR guild_id = 0
|
||||||
|
)
|
||||||
|
)
|
4
src/sql/reminder/remove.sql
Normal file
4
src/sql/reminder/remove.sql
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
DELETE FROM reminder
|
||||||
|
WHERE
|
||||||
|
creation_date = ?
|
||||||
|
AND user_id = ?
|
4
src/sql/reminder/select.sql
Normal file
4
src/sql/reminder/select.sql
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
reminder
|
13
src/sql/reminder/update.sql
Normal file
13
src/sql/reminder/update.sql
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
UPDATE reminder
|
||||||
|
SET
|
||||||
|
data = ?,
|
||||||
|
expiration_date = ?,
|
||||||
|
option_id = ?,
|
||||||
|
channel_id = ?,
|
||||||
|
creation_date = ?,
|
||||||
|
user_id = ?,
|
||||||
|
guild_id = ?,
|
||||||
|
locale = ?,
|
||||||
|
timeout_id = ?
|
||||||
|
WHERE
|
||||||
|
ID = ?
|
|
@ -5,6 +5,7 @@ import { Database } from "sqlite3";
|
||||||
import "../modules/client";
|
import "../modules/client";
|
||||||
import { loadLocales } from "./locales";
|
import { loadLocales } from "./locales";
|
||||||
import { YoutubeiExtractor } from "discord-player-youtubei";
|
import { YoutubeiExtractor } from "discord-player-youtubei";
|
||||||
|
import { readSQL } from "./db";
|
||||||
|
|
||||||
/** Creation of the client and definition of its properties */
|
/** Creation of the client and definition of its properties */
|
||||||
export default async (isDev: boolean) => {
|
export default async (isDev: boolean) => {
|
||||||
|
@ -59,7 +60,7 @@ export default async (isDev: boolean) => {
|
||||||
|
|
||||||
client.db = new Database(`${process.env.DOCKERIZED === "1" ? "/config" : "./config"}/db.sqlite3`);
|
client.db = new Database(`${process.env.DOCKERIZED === "1" ? "/config" : "./config"}/db.sqlite3`);
|
||||||
|
|
||||||
initDatabase(client.db);
|
client.db.run(readSQL("init"));
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
@ -75,25 +76,3 @@ export const quit = (client: Client) => {
|
||||||
// Close client
|
// Close client
|
||||||
client.destroy();
|
client.destroy();
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Initalize the database
|
|
||||||
* @param db Database
|
|
||||||
*/
|
|
||||||
const initDatabase = (db: Database) => {
|
|
||||||
// Table for reminders
|
|
||||||
db.run(
|
|
||||||
"CREATE TABLE IF NOT EXISTS reminder ( \
|
|
||||||
id INTEGER PRIMARY KEY, \
|
|
||||||
data TEXT, \
|
|
||||||
expiration_date TEXT, \
|
|
||||||
option_id INTEGER, \
|
|
||||||
channel_id TEXT, \
|
|
||||||
creation_date TEXT, \
|
|
||||||
user_id TEXT, \
|
|
||||||
guild_id TEXT, \
|
|
||||||
locale TEXT, \
|
|
||||||
timeout_id TEXT \
|
|
||||||
);",
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { getLocale } from "../locales";
|
||||||
import { blank, cleanCodeBlock } from "../misc";
|
import { blank, cleanCodeBlock } from "../misc";
|
||||||
import { showDate, strToSeconds, timeDeltaToString } from "../time";
|
import { showDate, strToSeconds, timeDeltaToString } from "../time";
|
||||||
import { RegexC, RegExpFlags } from "../regex";
|
import { RegexC, RegExpFlags } from "../regex";
|
||||||
|
import { readSQL } from "../db";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Option possible for reminders
|
* Option possible for reminders
|
||||||
|
@ -92,9 +93,7 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
||||||
|
|
||||||
// Add the remind to the db
|
// Add the remind to the db
|
||||||
client.db.run(
|
client.db.run(
|
||||||
"INSERT INTO reminder ( \
|
readSQL("reminder/add"),
|
||||||
data, expiration_date, option_id, channel_id, creation_date, user_id, guild_id, locale, timeout_id \
|
|
||||||
) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? );",
|
|
||||||
[
|
[
|
||||||
info.message,
|
info.message,
|
||||||
`${expiration_date}`,
|
`${expiration_date}`,
|
||||||
|
@ -127,19 +126,15 @@ export const newReminder = async (client: Client, time: string, info: infoRemind
|
||||||
export const deleteReminder = (client: Client, createdAt: string, userId: string) => {
|
export const deleteReminder = (client: Client, createdAt: string, userId: string) => {
|
||||||
// Delete the reminder for the database
|
// Delete the reminder for the database
|
||||||
return new Promise((ok, ko) => {
|
return new Promise((ok, ko) => {
|
||||||
// Add the remind to the db
|
// Remove the remind to the db
|
||||||
client.db.run(
|
client.db.run(readSQL("reminder/remove"), [createdAt, userId], (err) => {
|
||||||
"DELETE FROM reminder WHERE creation_date = ? AND user_id = ?",
|
|
||||||
[createdAt, userId],
|
|
||||||
(err) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ko(err);
|
ko(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send confirmation to user
|
// Send confirmation to user
|
||||||
ok(true);
|
ok(true);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -280,12 +275,7 @@ export const checkOwnershipReminder = async (
|
||||||
const data = (await new Promise((ok, ko) => {
|
const data = (await new Promise((ok, ko) => {
|
||||||
// Check the ownership
|
// Check the ownership
|
||||||
client.db.all<returnData>(
|
client.db.all<returnData>(
|
||||||
"SELECT EXISTS ( \
|
readSQL("reminder/ownership_check"),
|
||||||
SELECT 1 FROM reminder \
|
|
||||||
WHERE id = ? \
|
|
||||||
AND user_id = ? \
|
|
||||||
AND (guild_id = ? OR guild_id = 0) \
|
|
||||||
)",
|
|
||||||
[id, userId, guildId],
|
[id, userId, guildId],
|
||||||
(err, row) => {
|
(err, row) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -308,19 +298,14 @@ export const checkOwnershipReminder = async (
|
||||||
export const getReminderInfo = async (client: Client, id: number) => {
|
export const getReminderInfo = async (client: Client, id: number) => {
|
||||||
return (await new Promise((ok, ko) => {
|
return (await new Promise((ok, ko) => {
|
||||||
// Check the ownership
|
// Check the ownership
|
||||||
client.db.all<dbReminder>(
|
client.db.all<dbReminder>(readSQL("reminder/findById"), [id], (err, row) => {
|
||||||
"SELECT * FROM reminder \
|
|
||||||
WHERE id = ?",
|
|
||||||
[id],
|
|
||||||
(err, row) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ko(err);
|
ko(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send all the current reminders
|
// Send all the current reminders
|
||||||
ok(row[0]);
|
ok(row[0]);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
})) as dbReminder;
|
})) as dbReminder;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -334,17 +319,7 @@ export const updateReminder = (client: Client, data: dbReminder) => {
|
||||||
return new Promise((ok, ko) => {
|
return new Promise((ok, ko) => {
|
||||||
// Update the db
|
// Update the db
|
||||||
client.db.run(
|
client.db.run(
|
||||||
"UPDATE reminder \
|
readSQL("reminder/update"),
|
||||||
SET data = ?, \
|
|
||||||
expiration_date = ?, \
|
|
||||||
option_id = ?, \
|
|
||||||
channel_id = ?, \
|
|
||||||
creation_date = ?, \
|
|
||||||
user_id = ?, \
|
|
||||||
guild_id = ?, \
|
|
||||||
locale = ?, \
|
|
||||||
timeout_id = ? \
|
|
||||||
WHERE ID = ?",
|
|
||||||
[
|
[
|
||||||
data.data,
|
data.data,
|
||||||
data.expiration_date,
|
data.expiration_date,
|
||||||
|
@ -378,19 +353,14 @@ export const updateReminder = (client: Client, data: dbReminder) => {
|
||||||
const listReminders = async (client: Client, userId: string, guildId: string | null) => {
|
const listReminders = async (client: Client, userId: string, guildId: string | null) => {
|
||||||
return (await new Promise((ok, ko) => {
|
return (await new Promise((ok, ko) => {
|
||||||
// Check the ownership
|
// Check the ownership
|
||||||
client.db.all<dbReminder>(
|
client.db.all<dbReminder>(readSQL("reminder/find"), [userId, guildId ?? 0], (err, row) => {
|
||||||
"SELECT data, creation_date, expiration_date, id FROM reminder \
|
|
||||||
WHERE user_id = ? AND (guild_id = ? OR guild_id = 0)",
|
|
||||||
[userId, guildId ?? 0],
|
|
||||||
(err, row) => {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ko(err);
|
ko(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send all the current reminders
|
// Send all the current reminders
|
||||||
ok(row);
|
ok(row);
|
||||||
},
|
});
|
||||||
);
|
|
||||||
})) as dbReminder[];
|
})) as dbReminder[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
15
src/utils/db.ts
Normal file
15
src/utils/db.ts
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
import fs from "node:fs";
|
||||||
|
|
||||||
|
export const readSQL = (path: string) => {
|
||||||
|
const dir = "./src/sql/";
|
||||||
|
if (!path.startsWith(dir)) {
|
||||||
|
path = dir + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ext = ".sql";
|
||||||
|
if (!path.endsWith(ext)) {
|
||||||
|
path += ext;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs.readFileSync(path, "utf8");
|
||||||
|
};
|
Loading…
Reference in a new issue