~denisebitca/esmeralde

5bdd551fb1636233d770142deef0b665999cee37 — Rafael Bitca 2 years ago 88b7417
Fully replaced config.json userList by SQLite
2 files changed, 24 insertions(+), 16 deletions(-)

M src/DB.ts
M src/commands/edt.ts
M src/DB.ts => src/DB.ts +19 -10
@@ 1,10 1,10 @@
import Database from 'better-sqlite3';

import path from 'path';

export class DB{
    public static db: Database.Database;
    constructor(){
        DB.db = new Database('database.db');
        DB.db = new Database(path.join(__dirname, 'database.db'));
        if(!DB.checkIfDatabaseInitialised()){
            DB.init();
        }


@@ 14,27 14,27 @@ export class DB{
    public static init(){
        DB.db.prepare(`CREATE TABLE IF NOT EXISTS preference (
            id INTEGER PRIMARY KEY,
            group VARCHAR(150) NOT NULL,
            subgroup VARCHAR(150) NOT NULL
        )`).run();
            filiere VARCHAR(150) NOT NULL,
            groupe VARCHAR(150) NOT NULL
          );`).run();
    }

    public static checkIfDatabaseInitialised(){
        try{
            if(DB.db.prepare(`SELECT * FROM preference`).get() === 0 || DB.db.prepare(`SELECT * FROM preference`)){
            if(DB.db.prepare(`SELECT * FROM preference`).get()){
                return true;
            } else {
                return false;
            }
        } catch(e) {
            console.log(e);
            console.log("Created database!");
            return false;
        }
    }

    public static checkIfUserExists(id: string){
        try {
            return DB.db.prepare(`SELECT * FROM preference WHERE id = ?`).get(id);
            return DB.db.prepare(`SELECT * FROM preference WHERE id = ?`).get(id) !== undefined;
        } catch (e) {
            console.log(e);
            return false;


@@ 46,7 46,7 @@ export class DB{
            if(DB.checkIfUserExists(id)){
                return false;
            } else {
                return DB.db.prepare(`INSERT INTO preference (id, group, subgroup) VALUES (?, ?, ?)`).run(id, group, subgroup);
                return DB.db.prepare(`INSERT INTO preference VALUES (?, ?, ?)`).run(id, group, subgroup).changes;
            }
        } catch(e){
            console.log(e);


@@ 56,7 56,7 @@ export class DB{

    public static removeUser(id: string){
        try{
            DB.db.prepare(`DELETE FROM preference WHERE id = ?`).run(id);
            DB.db.prepare(`DELETE FROM preference WHERE id = ?`).run(id).changes;
        } catch(e){
            console.log(e);
            return false;


@@ 64,6 64,15 @@ export class DB{
        return true;
    }

    public static getUser(id: string){
        try{
            return DB.db.prepare(`SELECT * FROM preference WHERE id = ?`).get(id);
        } catch(e){
            console.log(e);
            return false;
        }
    }


}


M src/commands/edt.ts => src/commands/edt.ts +5 -6
@@ 2,10 2,8 @@ import {Crawler, Group, SubGroup} from "../Crawler";
import { CommandInteraction, MessageActionRow, MessageButton, MessageEmbed, MessageSelectMenu, MessageSelectOptionData } from "discord.js";
import { SlashCommandBuilder } from '@discordjs/builders';
import { Command } from '../index';
import fs from 'fs';
import dayjs from 'dayjs';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import path from "path";
import DB from "../DB";
dayjs.extend(weekOfYear);



@@ 32,10 30,11 @@ let obj : Command = {
		.setDescription('Trouver ton emploi du temps ou l`emploi du temps de ton groupe.'),
	async execute(interaction : CommandInteraction) {

        var config = JSON.parse(fs.readFileSync(path.join(__dirname, "../config.json"), {encoding: "utf-8"}));

        if(DB.getUser(interaction.user.id)){
            //return interaction.reply({content: edtLink(JSON.parse(presenceCheck[0].subgroup).id), ephemeral: true});
        let user = DB.getUser(interaction.user.id);
        if(user !== undefined){
            let group : Group = JSON.parse(user.filiere);
            let subGroup : SubGroup = JSON.parse(user.groupe);
            return interaction.reply({content: "Emploi du temps de la filière " + group.name + ", groupe " + subGroup.name + " : \n" + edtLink(subGroup.id)});
        }

        let crawler = await Crawler.newCrawler("https://edt.iut-orsay.fr/edt_invite.php");