Skip to content

Commit

Permalink
unban requests
Browse files Browse the repository at this point in the history
lluisd committed May 9, 2024
1 parent f8f189f commit 229e663
Showing 4 changed files with 95 additions and 12 deletions.
30 changes: 30 additions & 0 deletions handlers/stream.js
Original file line number Diff line number Diff line change
@@ -13,6 +13,36 @@ class Stream {
async refreshPage() {
await BrowserService.refreshPage().catch(() => { console.error('refreshPage on refreshPage')})
}

async getUnbanRequests(target, bot) {
const result = await TwitchService.getUnbanRequests()
if (result !== null) {
const text = result.length > 0 ? `Hay ${result.length} solicitudes de desbaneo pendientes de revisar (${this._getUserNames(result)})` : 'No hay solicitudes de desbaneo pendientes de revisar.'
await bot.say(target, text)
}
}

_getUserNames (unbanRequests) {
let text
if (unbanRequests.length === 1){
text = this._maskUserName(unbanRequests[0].user_name)
} else if (unbanRequests.length > 1) {
text = unbanRequests.map(ur => this._maskUserName(ur[0].user_name)).join(', ').replace(/, ([^,]*)$/, ' y $1')
}
return text
}

_maskUserName (userName) {
const unmaskedLength = 3
let maskedUserName = userName.substring(0,unmaskedLength)

if (userName.length > unmaskedLength) {
maskedUserName = maskedUserName + '*'.repeat(userName.length - unmaskedLength)
}

return maskedUserName
}

async captureScreenshot(target, bot, notifierBot, displayName, roomId) {
const image = await BrowserService.getScreenshot().catch(() => { console.error('getScreenshot on captureScreenshot')})
if (image) {
4 changes: 4 additions & 0 deletions lib/inputParser.js
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@ class InputParser {
isAskingForServerStatus (text) {
return text.toLowerCase().startsWith('!estado')
}

isAskingForPendingUnbanRequests (text) {
return text.toLowerCase().startsWith('!bans')
}
}

module.exports = InputParser
29 changes: 27 additions & 2 deletions lib/messenger.js
Original file line number Diff line number Diff line change
@@ -22,7 +22,8 @@ class Messenger {
this.bot = new tmi.client(opts)
this.cooldown = {
screenshot: false,
of: false
of: false,
bans: false
}
this.notifier = new Notifier(this.bot)
await this.notifier.notify()
@@ -103,7 +104,8 @@ class Messenger {
}
}

if (textSplit.length > 0 && inputParser.isAskingForF5(textSplit[0]) && (context['mod'] || context['vip']))
if (textSplit.length > 0 && inputParser.isAskingForF5(textSplit[0]) &&
(this._isVip(context) || this._isMod(context) || this._isBroadcaster(context)))
return handlers.stream.refreshPage(target, this.bot, this.notifier.bot)

if (textSplit.length === 2 && inputParser.isAskingForAddBirthday(textSplit[0]) && textSplit[1].includes('-')) {
@@ -117,6 +119,17 @@ class Messenger {
if (textSplit.length > 0 && inputParser.isAskingForServerStatus(textSplit[0])) {
return handlers.generic.status(target, this.bot)
}

if (textSplit.length > 0 && inputParser.isAskingForPendingUnbanRequests(textSplit[0]) &&
(this._isVip(context) || this._isMod(context) || this._isBroadcaster(context))) {
if (!this.cooldown.bans) {
this.cooldown.bans = true
setTimeout(() => {
this.cooldown.bans = false
}, 30000)
return handlers.stream.getUnbanRequests(target, this.bot)
}
}
}

handleHosting (channel, target, viewers) {
@@ -130,6 +143,18 @@ class Messenger {
handleConnect (addr, port) {
console.log(`* Connected to ${addr}:${port}`)
}

_isVip(context) {
return context['vip'] || context.badges.vip
}

_isMod(context) {
return context['mod']
}

_isBroadcaster(context) {
return context['user-id'] === context['room-id']
}
}

module.exports = Messenger
44 changes: 34 additions & 10 deletions services/twitch.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
const config = require('../config')
const dbManager = require('../helpers/dbmanager')

const endpointPrefix = 'https://api.twitch.tv/helix/streams'
const endpointPrefix = 'https://api.twitch.tv/helix'

async function getStream() {
let result = { type: 'notLive'}
let liveData = null

const token = await dbManager.getToken(parseInt(config.twitch.userId)).lean()
const endpoint = endpointPrefix + '?user_login=' + config.twitch.channels
const options = {
headers: {
'Client-Id': config.twitch.clientId,
'Authorization': 'Bearer ' + token.accessToken
}
}
const options = await _getHeaders()
const endpoint = endpointPrefix + '/streams?user_login=' + config.twitch.channels

try {
const response = await fetch(endpoint, options)
@@ -40,6 +34,24 @@ async function getStream() {
return { ...result, lastUpdate: channel.lastUpdate}
}

async function getUnbanRequests () {
let result
const options = await _getHeaders()
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`

try {
const response = await fetch(endpoint, options)
const data = await response.json()
result = data?.data ?? null

} catch {
result = null
}

return result
}

async function getChannel () {
return dbManager.getChannel(config.twitch.channels).lean()
}
@@ -52,10 +64,22 @@ async function saveLastUpdate () {
await dbManager.updateChannel(config.twitch.channels, { lastUpdate: new Date() })
}

async function _getHeaders () {
const token = await dbManager.getToken(parseInt(config.twitch.userId)).lean()
return {
headers: {
'Client-Id': config.twitch.clientId,
'Authorization': 'Bearer ' + token.accessToken
}
}
}

module.exports = {
getStream,
saveLastMessage,
getChannel,
saveLastUpdate
saveLastUpdate,
getUnbanRequests
}


0 comments on commit 229e663

Please sign in to comment.