Skip to content

Commit

Permalink
add announcements MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Jul 9, 2024
1 parent cb6d82c commit 3dd9d6d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
12 changes: 12 additions & 0 deletions handlers/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
config = require("../config")
const TwitchService = require('../services/twitch')

class Events {
async sendEvents() {
if (config.twitch.channels.startsWith('m')) {
await TwitchService.sendAnnouncement(`28 y 29 de septiembre quedada en la Tarraco Manga`, 'orange')
}
}
}

module.exports = Events
4 changes: 3 additions & 1 deletion handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const Train = require('./Train')
const Stream = require('./Stream')
const Birthday = require('./Birthday')
const TempsDeFlors = require('./TempsDeFlors')
const Events = require('./Events')

module.exports = {
generic: new Generic(),
weather: new Weather(),
train: new Train(),
stream: new Stream(),
birthday: new Birthday(),
tempsDeFlors: new TempsDeFlors()
tempsDeFlors: new TempsDeFlors(),
events: new Events()
}
4 changes: 4 additions & 0 deletions lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Notifier {
await handlers.stream.refreshPage()
})

cron.schedule('0/15 * * * *', async () => {
await handlers.events.sendEvents(this.twitchBot, this.target)
})

return Promise.resolve()
}

Expand Down
24 changes: 14 additions & 10 deletions services/twitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async function getStream() {
async function getUnbanRequests () {
let result
const options = await _getHeaders()
options.
const channel = await dbManager.getChannel(config.twitch.channels).lean()
const endpoint = `${endpointPrefix}/moderation/unban_requests?broadcaster_id=${channel.roomId}&moderator_id=${config.twitch.userId}&status=pending`

Expand All @@ -53,23 +52,27 @@ async function getUnbanRequests () {
return result
}

async function annunce() {
async function sendAnnouncement(text, color) {
let result
const options = await _getHeaders()
options.method = 'POST'
const channel = await dbManager.getChannel(config.twitch.channels).lean()
const endpoint = `${endpointPrefix}/chat/announcements?broadcaster_id=${channel.roomId}&moderator_id=${config.twitch.userId}`
try {
const response = await fetch(endpoint, options)
const data = await response.json()
result = data?.data ?? null

} catch {
result = null
const body = {
data : {
message: text,
color: color
}
}

return result
options.body = JSON.stringify(body)

try {
await fetch(endpoint, options)
} catch {
result = null
}
}

async function getChannel () {
Expand Down Expand Up @@ -104,7 +107,8 @@ module.exports = {
getChannel,
saveLastUpdate,
getUnbanRequests,
setActiveSpot
setActiveSpot,
sendAnnouncement
}


0 comments on commit 3dd9d6d

Please sign in to comment.