Skip to content

Commit

Permalink
users: fetch username if its missing in db
Browse files Browse the repository at this point in the history
Fixes: #1473
PR: #1474
  • Loading branch information
sogehige committed Oct 16, 2018
1 parent b1791e9 commit b1ed132
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
56 changes: 56 additions & 0 deletions src/bot/api.js
Expand Up @@ -142,6 +142,62 @@ class API {
global.db.engine.update('api.current', { key: 'game' }, { value: await global.cache.gameCache() })
}

async getUsernameFromTwitch (id: string) {
const url = `https://api.twitch.tv/helix/users?id=${id}`
var request
/*
{
"data": [{
"id": "44322889",
"login": "dallas",
"display_name": "dallas",
"type": "staff",
"broadcaster_type": "",
"description": "Just a gamer playing games and chatting. :)",
"profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-profile_image-1a2c906ee2c35f12-300x300.png",
"offline_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/dallas-channel_offline_image-1a2c906ee2c35f12-1920x1080.png",
"view_count": 191836881,
"email": "login@provider.com"
}]
}
*/

const token = await global.oauth.settings.bot.accessToken
const needToWait = token === ''
const notEnoughAPICalls = global.api.calls.bot.remaining <= 30 && global.api.calls.bot.refresh > _.now() / 1000
if ((needToWait || notEnoughAPICalls)) {
return null
}

try {
request = await axios.get(url, {
headers: {
'Authorization': 'Bearer ' + token
}
})

// save remaining api calls
// $FlowFixMe error with flow on request.headers
this.calls.bot.limit = request.headers['ratelimit-limit']
// $FlowFixMe error with flow on request.headers
this.calls.bot.remaining = request.headers['ratelimit-remaining']
// $FlowFixMe error with flow on request.headers
this.calls.bot.refresh = request.headers['ratelimit-reset']
global.commons.processAll({ ns: 'api', fnc: 'setRateLimit', args: [ 'bot', request.headers['ratelimit-limit'], request.headers['ratelimit-remaining'], request.headers['ratelimit-reset'] ] })

if (global.panel && global.panel.io) global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'getUsernameFromTwitch', api: 'helix', endpoint: url, code: request.status, remaining: this.calls.bot.remaining })
return request.data.data[0].login
} catch (e) {
if (typeof e.response !== 'undefined' && e.response.status === 429) {
global.commons.processAll({ ns: 'api', fnc: 'setRateLimit', args: [ 'bot', 120, 0, e.response.headers['ratelimit-reset'] ] })
this.calls.bot.remaining = 0
this.calls.bot.refresh = e.response.headers['ratelimit-reset']
}
if (global.panel && global.panel.io) global.panel.io.emit('api.stats', { data: {}, timestamp: _.now(), call: 'getUsernameFromTwitch', api: 'helix', endpoint: url, code: e.stack, remaining: this.calls.bot.remaining })
}
return null
}

async getIdFromTwitch (username: string, isChannelId: bool = false) {
const url = `https://api.twitch.tv/helix/users?login=${username}`
var request
Expand Down
3 changes: 2 additions & 1 deletion src/bot/twitch.js
Expand Up @@ -340,7 +340,7 @@ class Twitch {

moment.locale(global.lib.translate.lang)
for (let user of sorted) {
message += (i + 1) + '. ' + (await global.configuration.getValue('atUsername') ? '@' : '') + user.username + ' - '
message += (i + 1) + '. ' + (await global.configuration.getValue('atUsername') ? '@' : '') + (user.username || 'unknown') + ' - '
if (type === 'time') message += (user.watched / 1000 / 60 / 60).toFixed(1) + 'h'
else if (type === 'tips') message += user.amount.toFixed(2) + global.currency.symbol(await global.configuration.getValue('currency'))
else if (type === 'points') {
Expand All @@ -356,6 +356,7 @@ class Twitch {
} else {
message += 'no data available'
}
console.log(message)
global.commons.sendMessage(message, opts.sender)
}

Expand Down
10 changes: 9 additions & 1 deletion src/bot/users.js
Expand Up @@ -278,7 +278,15 @@ class Users extends Core {
}

async getNameById (id: string) {
return (await global.db.engine.findOne('users', { id })).username
let username = (await global.db.engine.findOne('users', { id })).username

if (typeof username === 'undefined' || username === null) {
username = await global.api.getUsernameFromTwitch(id)
if (username) {
global.db.engine.update('users', { id }, { username })
}
}
return username || null
}

async getIdByName (username: string) {
Expand Down

0 comments on commit b1ed132

Please sign in to comment.