-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
95 lines (80 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { Client, Collection, GatewayIntentBits, RESTJSONErrorCodes } = require("discord.js");
require("dotenv").config();
const sqlite = require("better-sqlite3");
const fs = require("fs");
// DB Creation check
let db;
(async() => {
// TODO: Convert this to async fs.access
// eslint-disable-next-line n/no-sync
if (!fs.existsSync("data.db")) {
db = new sqlite("./data.db");
await createDB();
}
else db = new sqlite("./data.db");
})();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
module.exports = client; // export client
// Global Variables
client.slashCommands = new Collection();
client.config = require("./config");
client.db = db;
client.pronote = require("./pronote");
client.login(process.env.BOT_TOKEN).catch((err) => {
if (err.code === RESTJSONErrorCodes.InvalidToken || err.code === "TokenInvalid") {
throw new Error("Can't login. The bot token is invalid.");
}
console.error("Can't login. Please check your config or bot token.");
throw err;
});
require("./handler")(client);
async function createDB() {
try {
db.exec(`
CREATE TABLE "changementedt" (
"id" TEXT NOT NULL,
"timestamp" INTEGER,
"prof" TEXT,
"matiere" TEXT,
"raison" TEXT
);
CREATE TABLE "homework" (
"id" TEXT NOT NULL UNIQUE,
"matiere" TEXT,
"description" TEXT,
"date_rendue" TEXT,
"date_donne" TEXT,
"fichiers" TEXT,
"fait" INTEGER,
"message_id" TEXT,
PRIMARY KEY("id")
);
CREATE TABLE "moyenne" (
"matiere" TEXT,
"moyenne" INTEGER,
"moyenne_classe" INTEGER
);
CREATE TABLE "config" (
"name" TEXT NOT NULL UNIQUE,
"value" TEXT
);
CREATE TABLE "holidays" (
"name" TEXT,
"from" INTEGER,
"to" TEXT,
"reminder_before_start" INTEGER DEFAULT 0,
"reminder_start" INTEGER DEFAULT 0,
"reminder_before_end" INTEGER DEFAULT 0,
"reminder_end" INTEGER DEFAULT 0
);`);
db.prepare("INSERT INTO moyenne (matiere) VALUES (?)").run("global");
console.log("DB successfully created !");
} catch (error) {
throw new Error("Cannot create DB... Aborting... " + error);
}
}