Skip to content

Commit

Permalink
edit stream title when is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 3, 2024
1 parent c70c88e commit f677532
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
28 changes: 22 additions & 6 deletions handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions models/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const ChannelSchema = new Schema({
lastMessageId: {
type: Number,
required: false
},
title: {
type: String,
required: false
}
})

Expand Down
9 changes: 8 additions & 1 deletion services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit f677532

Please sign in to comment.