Commit 1102f224 authored by Guillaume Vagner's avatar Guillaume Vagner
Browse files

rebootproof

parent 65b29782
......@@ -105,5 +105,13 @@ function getGroups(chatId) {
})
}
function getSchedules() {
return query(`
SELECT *
FROM channel
WHERE schedule <> ""
`)
}
module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups };
\ No newline at end of file
module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups, getSchedules };
\ No newline at end of file
......@@ -2,23 +2,22 @@
var schedule = require('node-schedule');
// Modules propres
var { modifyChan, getGroups } = require('./connection-db');
var { modifyChan, getGroups, getSchedules } = require('./connection-db');
var { getBirthdays, getNewToken } = require('./requests');
var bot = require('./telegram');
// Création de variables
var schedules = {};
getSchedules().then(chans => {
chans.forEach(chan => {
addSchedule(chan, chan.schedule)
})
console.log(`[schdles] reload schedules`)
})
// En cas de redémarrage de l'appli
// query(`SELECT chatId, schedule FROM channel WHERE schedule <> ""`).then(rep => {
// rep.forEach(chan => {
//
// })
// })
function addSchedule(chan, time, bot) {
function addSchedule(chan, time) {
const hour = parseInt(time.split(':')[0]);
const minute = parseInt(time.split(':')[1]);
chan.schedule = time;
......
......@@ -168,7 +168,7 @@ bot.onText(/\/add (.+)/, (msg, match) => {
})
// Ajout du temps de schedule
// Ajout d'un rappel
bot.onText(/\/schedule (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const time = match[1];
......@@ -180,19 +180,20 @@ bot.onText(/\/schedule (.+)/, (msg, match) => {
bot.sendMessage(chatId, `Votre rappel est configuré pour tous les jours à ${time}`);
})
// Suppression du rappel
bot.onText(/\/unschedule/, msg => {
const chatId = msg.chat.id;
getChanByChatId(chatId).then(chan => {
if (!chan) return bot.sendMessage(chatId, 'Pas de compte enregistré, faites /start pour commencer');
if (chan.time == '') return bot.sendMessage(chatId, 'Pas de rappel défini...');
if (chan.schedule == '') return bot.sendMessage(chatId, 'Pas de rappel défini...');
deleteSchedule(chatId)
chan.time = ''
chan.schedule = ''
return modifyChan(chan)
}).then(_ => {
bot.sendMessage(chatId, 'Votre rappel a été supprimé.')
})
})
// J'étais bien obligé (en vrai c'est pour tester)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment