diff --git a/connection-db.js b/connection-db.js index a40536101cf0494d1e25abaeaa3dc5184bdabdfb..1d42911f0aeb72601c6b099f9c225a8d20b7dd6e 100644 --- a/connection-db.js +++ b/connection-db.js @@ -95,6 +95,11 @@ function addGroup(chatId, groupId) { `); } +function removeGroup(chatId, groupId) { + return query(` + DELETE FROM groups WHERE chatId = ${chatId} AND grp = ${groupId}`) +} + function getGroups(chatId) { return query(` SELECT grp @@ -114,4 +119,4 @@ function getSchedules() { } -module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, getGroups, getSchedules }; \ No newline at end of file +module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, removeGroup, getGroups, getSchedules }; \ No newline at end of file diff --git a/telegram.js b/telegram.js index 602723b71e8c7f8ea1b4967195dcded92544a4dc..6b090db5e6e43e8d0765bc75d7a7b50d58f90ac1 100644 --- a/telegram.js +++ b/telegram.js @@ -4,7 +4,7 @@ process.env["NTBA_FIX_319"] = 1; var TelegramBot = require('node-telegram-bot-api'); // Modules propres -var { getChanByChatId, createChan, deleteChanByChatId, modifyChan, addGroup, getGroups } = require('./connection-db'); +var { getChanByChatId, createChan, deleteChanByChatId, modifyChan, addGroup, removeGroup, getGroups } = require('./connection-db'); var { getBirthdays, searchGroups, getGroupById } = require('./requests'); var { schedules, addSchedule, deleteSchedule } = require('./schedule'); @@ -167,6 +167,26 @@ bot.onText(/\/add (.+)/, (msg, match) => { }) }) +bot.onText(/\/del (.+)/, (msg, match) => { + const chatId = msg.chat.id; + getChanByChatId(chatId).then(chan => { + const id = parseInt(match[1].split(' ')[0]); + return Promise.all([ + getGroupById(chan.token, id), + getGroups(chatId), + id + ]); + }).then(([group, groups, id]) => { + // si pas de groupe trouvé + if (!group) return bot.sendMessage(chatId, 'Pas de groupe trouvé ayant cette ID'); + // si le groupe y est déjà + if (groups.indexOf(id) === -1) return bot.sendMessage(chatId, 'Ce groupe n\'est pas dans votre liste d\'anniversaire.'); + // sinon on le rajoute + return removeGroup(chatId, id).then(_ => { + bot.sendMessage(chatId, `Retrait du groupe \`${group}\` de la liste des anniversaires.`, { parse_mode: 'Markdown' }); + }) + }) +}) // Ajout d'un rappel bot.onText(/\/schedule (.+)/, (msg, match) => {