Skip to content

Commit

Permalink
delete telegram message when stream is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Feb 3, 2024
1 parent ce4ed62 commit c70c88e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 36 deletions.
36 changes: 7 additions & 29 deletions handlers/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!*`
Expand All @@ -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
// }
}
}

Expand Down
5 changes: 2 additions & 3 deletions helpers/dbmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions models/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const ChannelSchema = new Schema({
live: {
type: Boolean,
required: true
},
lastMessageId: {
type: Number,
required: false
}
})

Expand Down
20 changes: 16 additions & 4 deletions services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit c70c88e

Please sign in to comment.