Skip to content

Commit

Permalink
oauth: Fix channelId fetch
Browse files Browse the repository at this point in the history
Closes #1391
  • Loading branch information
sogehige committed Sep 29, 2018
1 parent bf4a35d commit cde16fa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/bot/commons.js
Expand Up @@ -157,20 +157,21 @@ Commons.prototype.sendMessage = async function (message, sender, attr) {
} else {
global.log.chatOut(message, { username: sender.username })
if ((await global.configuration.getValue('sendWithMe')) && !message.startsWith('/')) {
global.commons.message('me', global.commons.getBroadcaster(), message)
global.commons.message('me', null, message)
} else {
global.commons.message('say', global.commons.getBroadcaster(), message)
global.commons.message('say', null, message)
}
}
}
return true
}

Commons.prototype.message = function (type, username, message, retry) {
Commons.prototype.message = async function (type, username, message, retry) {
if (config.debug.console) return
if (cluster.isWorker && process.send) process.send({ type: type, sender: username, message: message })
else if (cluster.isMaster) {
try {
if (username === null) username = await global.oauth.settings.general.channel
if (username === '') {
global.log.error('TMI: channel is not defined, message cannot be sent')
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/bot/main.js
Expand Up @@ -118,9 +118,9 @@ function forkOn (worker) {
for (let time of global.avgResponse) avgTime += parseInt(time, 10)
global.status['RES'] = (avgTime / global.avgResponse.length).toFixed(0)
} else if (msg.type === 'say') {
global.commons.message('say', await global.oauth.settings.general.channel, msg.message)
global.commons.message('say', null, msg.message)
} else if (msg.type === 'me') {
global.commons.message('me', await global.oauth.settings.general.channel, msg.message)
global.commons.message('me', null, msg.message)
} else if (msg.type === 'whisper') {
global.commons.message('whisper', msg.sender, msg.message)
} else if (msg.type === 'parse') {
Expand Down
23 changes: 18 additions & 5 deletions src/bot/oauth.js
Expand Up @@ -10,6 +10,7 @@ const constants = require('./constants')

class OAuth extends Core {
timeouts: Object = {}
currentChannel: string = ''

constructor () {
const settings = {
Expand All @@ -18,6 +19,7 @@ class OAuth extends Core {
bot: '',
clientId: '',
botId: '',
broadcasterId: '',
channelId: ''
},
general: {
Expand Down Expand Up @@ -111,9 +113,24 @@ class OAuth extends Core {
this.addMenu({ category: 'settings', name: 'core', id: 'core' })
this.validateOAuth('bot')
this.validateOAuth('broadcaster')
this.getChannelId()
this.sendDataToClusters()
}

async getChannelId () {
clearTimeout(this.timeouts['getChannelId'])

const channel = await this.settings.general.channel
if (this.currentChannel !== channel && channel !== '') {
this.currentChannel = channel
const cid = await global.users.getIdFromTwitch(channel)
this.settings._.channelId = cid
global.log.info('Channel ID set to ' + cid)
}

this.timeouts['getChannelId'] = setTimeout(() => this.getChannelId(), 10000)
}

async sendDataToClusters () {
clearTimeout(this.timeouts['sendDataToClusters'])
global.commons.cached.owners = await this.settings.general.owners
Expand Down Expand Up @@ -162,11 +179,7 @@ class OAuth extends Core {
this.settings._.clientId = request.data.client_id

if (type === 'bot') this.settings._.botId = request.data.user_id
else {
const currentChannelId = await this.settings._.channelId
if (currentChannelId !== request.data.user_id) global.log.info('Broadcaster channel ID set to ' + request.data.user_id)
this.settings._.channelId = request.data.user_id
}
else this.settings._.broadcasterId = request.data.user_id

this.settings[type]._authenticatedScopes = request.data.scopes
this.settings[type].username = request.data.login
Expand Down
2 changes: 1 addition & 1 deletion src/bot/overlays/credits.js
Expand Up @@ -153,7 +153,7 @@ class Credits {
async getTopClips () {
const period = _.includes(['day', 'week', 'month', 'all'], await await global.configuration.getValue('creditsTopClipsPeriod')) ? await global.configuration.getValue('creditsTopClipsPeriod') : 'day'
const count = await await global.configuration.getValue('creditsTopClipsCount')
const channel = await global.oauth.settings.broadcaster.username
const channel = await global.oauth.settings.general.channel

const token = await global.oauth.settings.bot.accessToken
if (token === '') return
Expand Down

0 comments on commit cde16fa

Please sign in to comment.