From c70c88e619574bbd6da158ee641aa6ffba37201d Mon Sep 17 00:00:00 2001 From: Lluis Date: Sat, 3 Feb 2024 13:10:46 +0100 Subject: [PATCH] delete telegram message when stream is finished --- handlers/stream.js | 36 +++++++----------------------------- helpers/dbmanager.js | 5 ++--- models/channel.js | 4 ++++ services/twitch.js | 20 ++++++++++++++++---- 4 files changed, 29 insertions(+), 36 deletions(-) diff --git a/handlers/stream.js b/handlers/stream.js index fc65f25..74c4239 100644 --- a/handlers/stream.js +++ b/handlers/stream.js @@ -7,7 +7,7 @@ class Stream { async catchStream (bot) { const result = await TwitchService.getStream() - if (result) { + if (result && result.type === 'live') { const image = `[\u200c](${result.thumbnail_url.replace('-{width}x{height}', '')})` const link = `[${twitchUrl}${result.user_name}](${twitchUrl}${result.user_name})` const directo = `*¡EN DIRECTO!*` @@ -17,35 +17,13 @@ class Stream { parse_mode: 'markdown' } - bot.sendMessage(config.telegram.chatId, text, options) + await bot.sendMessage(config.telegram.chatId, text, options).then((msg) => { + TwitchService.saveLastMessage(msg) + }) + } else if (result && result.type === 'finished' && result.messageId) { + await bot.deleteMessage(config.telegram.chatId, result.messageId) + await TwitchService.deleteLastMessage() } - - // { - // "id": "40400588869", - // "user_id": "779563374", - // "user_login": "manzana_oscura", - // "user_name": "manzana_oscura", - // "game_id": "516575", - // "game_name": "VALORANT", - // "type": "live", - // "title": "aver si veo por donde me matan", - // "viewer_count": 8, - // "started_at": "2024-02-02T12:05:37Z", - // "language": "es", - // "thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_manzana_oscura-{width}x{height}.jpg", - // "tag_ids": [], - // "tags": [ - // "PequeñaGranComunidad", - // "PreguntaYRespondo", - // "lectura", - // "ASMR", - // "Relajacion", - // "Meditacion", - // "Español", - // "English" - // ], - // "is_mature": false - // } } } diff --git a/helpers/dbmanager.js b/helpers/dbmanager.js index 0eddc37..2ded8d0 100644 --- a/helpers/dbmanager.js +++ b/helpers/dbmanager.js @@ -14,9 +14,8 @@ function getChannel (name) { return Channel.findOne({name: name}) } -function updateChannel (name, isLive) { - return Channel.updateOne({name: name}, { live: isLive }) - +function updateChannel (name, update) { + return Channel.updateOne({name: name}, update) } async function getMuncipioStartsWith (name) { diff --git a/models/channel.js b/models/channel.js index 015cb52..42046f3 100644 --- a/models/channel.js +++ b/models/channel.js @@ -10,6 +10,10 @@ const ChannelSchema = new Schema({ live: { type: Boolean, required: true + }, + lastMessageId: { + type: Number, + required: false } }) diff --git a/services/twitch.js b/services/twitch.js index 8e3606a..f33942b 100644 --- a/services/twitch.js +++ b/services/twitch.js @@ -4,7 +4,7 @@ const dbManager = require('../helpers/dbmanager') const endpointPrefix = 'https://api.twitch.tv/helix/streams' async function getStream() { - let result = null + let result = { type: 'notLive'} const token = await dbManager.getToken(parseInt(config.twitch.userId)).lean() const endpoint = endpointPrefix + '?user_login=' + config.twitch.channels @@ -20,15 +20,27 @@ async function getStream() { const channel = await dbManager.getChannel(config.twitch.channels).lean() if (liveData && !channel.live) { - await dbManager.updateChannel(config.twitch.channels, true) + await dbManager.updateChannel(config.twitch.channels, { live: true }) result = liveData } else if (!liveData && channel.live) { - await dbManager.updateChannel(config.twitch.channels, false) + await dbManager.updateChannel(config.twitch.channels, { live: false }) + result = { type: 'finished', messageId: channel.lastMessageId} } return result } +async function saveLastMessage (msg) { + await dbManager.updateChannel(config.twitch.channels, { lastMessageId: msg.message_id }) +} + +async function deleteLastMessage () { + await dbManager.updateChannel(config.twitch.channels, { lastMessageId: null }) +} + + module.exports = { - getStream + getStream, + saveLastMessage, + deleteLastMessage }