From f677532c35ad5951ef46ceb0a8fed9cd9dfc94f6 Mon Sep 17 00:00:00 2001 From: Lluis Date: Sat, 3 Feb 2024 14:03:27 +0100 Subject: [PATCH] edit stream title when is changed --- handlers/stream.js | 28 ++++++++++++++++++++++------ models/channel.js | 4 ++++ services/twitch.js | 9 ++++++++- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/handlers/stream.js b/handlers/stream.js index 74c4239..1d8337e 100644 --- a/handlers/stream.js +++ b/handlers/stream.js @@ -7,24 +7,40 @@ class Stream { async catchStream (bot) { const result = await TwitchService.getStream() - 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!*` - const text = `${image} ${directo} ${link}` + if (result && result.type === 'live' ) { + const text = this._getText(result) const options = { - parse_mode: 'markdown' + parse_mode: 'Markdown' } await bot.sendMessage(config.telegram.chatId, text, options).then((msg) => { TwitchService.saveLastMessage(msg) + TwitchService.saveTitle(result.title) }) } else if (result && result.type === 'finished' && result.messageId) { await bot.deleteMessage(config.telegram.chatId, result.messageId) await TwitchService.deleteLastMessage() + await TwitchService.saveTitle(result.title) + } else if (result && result.type === 'stillLive' && result.messageId && result.lastTitle !== result.title) { + const options = { + chat_id: config.telegram.chatId, + message_id: result.messageId, + parse_mode: 'Markdown' + } + try { + await bot.editMessageText(this._getText(result), options) + } catch {} + await TwitchService.saveTitle(result.title) } } + + _getText (stream) { + const image = `[\u200c](${stream.thumbnail_url.replace('-{width}x{height}', '')})` + const link = `[${twitchUrl}${stream.user_name}](${twitchUrl}${stream.user_name})` + const title = `🔴 *¡EN DIRECTO!*` + return `${image} ${title} ${link} \n _${stream.title}_` + } } module.exports = Stream diff --git a/models/channel.js b/models/channel.js index 42046f3..582b20d 100644 --- a/models/channel.js +++ b/models/channel.js @@ -14,6 +14,10 @@ const ChannelSchema = new Schema({ lastMessageId: { type: Number, required: false + }, + title: { + type: String, + required: false } }) diff --git a/services/twitch.js b/services/twitch.js index f33942b..83abd6c 100644 --- a/services/twitch.js +++ b/services/twitch.js @@ -25,6 +25,8 @@ async function getStream() { } else if (!liveData && channel.live) { await dbManager.updateChannel(config.twitch.channels, { live: false }) result = { type: 'finished', messageId: channel.lastMessageId} + } else if (liveData && channel.live) { + result = { ...liveData, type: 'stillLive', messageId: channel.lastMessageId, lastTitle: channel.title} } return result @@ -38,9 +40,14 @@ async function deleteLastMessage () { await dbManager.updateChannel(config.twitch.channels, { lastMessageId: null }) } +async function saveTitle (title) { + await dbManager.updateChannel(config.twitch.channels, { title: title }) +} + module.exports = { getStream, saveLastMessage, - deleteLastMessage + deleteLastMessage, + saveTitle }