Skip to content

Commit

Permalink
Optimize loading of external resources in tag replacement function
Browse files Browse the repository at this point in the history
  • Loading branch information
BOLL7708 committed Jun 29, 2022
1 parent c3e9f1f commit a0926f7
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,39 +258,51 @@ class Utils {
* @returns
*/
static async replaceTagsInText(text: string, userData?: IActionUser, extraTags: { [key:string]: string } = {}) {
const modules = ModulesSingleton.getInstance()
const states = StatesSingleton.getInstance()

// Default tags from incoming user data
const tags = await this.getDefaultTags(userData)
const states = StatesSingleton.getInstance()
if(states.lastSteamAppId) {
const steamGameMeta = await SteamStore.getGameMeta(states.lastSteamAppId)
tags.gameId = states.lastSteamAppId ?? 'N/A'
tags.gamePrice = SteamStore.getPrice(steamGameMeta)
tags.gameLink = SteamStore.getStoreURL(states.lastSteamAppId)
tags.gameName = steamGameMeta?.name ?? 'N/A'
tags.gameInfo = steamGameMeta?.short_description ?? 'N/A'
tags.gameDeveloper = steamGameMeta?.developers?.join(', ') ?? 'N/A'
tags.gamePublisher = steamGameMeta?.publishers?.join(', ') ?? 'N/A'
tags.gameBanner = steamGameMeta?.header_image ?? ''
tags.gameRelease = steamGameMeta?.release_date?.date ?? 'N/A'

// Game tags
if(text.includes('%game')) {
if(states.lastSteamAppId) {
const steamGameMeta = await SteamStore.getGameMeta(states.lastSteamAppId)
tags.gameId = states.lastSteamAppId ?? 'N/A'
tags.gameLink = SteamStore.getStoreURL(states.lastSteamAppId)
if(steamGameMeta) {
tags.gamePrice = SteamStore.getPrice(steamGameMeta)
tags.gameName = steamGameMeta.name ?? 'N/A'
tags.gameInfo = steamGameMeta.short_description ?? 'N/A'
tags.gameDeveloper = steamGameMeta.developers?.join(', ') ?? 'N/A'
tags.gamePublisher = steamGameMeta.publishers?.join(', ') ?? 'N/A'
tags.gameBanner = steamGameMeta.header_image ?? ''
tags.gameRelease = steamGameMeta.release_date?.date ?? 'N/A'
}
}
}

// Target tags from incoming user tag
let userTag =
this.getFirstUserTagInText(userData?.input ?? '')
?? userData?.input?.split(' ')?.shift()
?? ''
if(userTag.includes('https://')) userTag = userTag.split('/').pop() ?? ''

const modules = ModulesSingleton.getInstance()
if(userTag) {
const channelData = await modules.twitchHelix.getChannelByName(userTag)
if(channelData) {
tags.targetName = channelData.broadcaster_name
tags.targetTag = `@${channelData.broadcaster_name}`
tags.targetNick = await this.loadCleanName(channelData.broadcaster_login)
tags.targetGame = channelData.game_name
tags.targetTitle = channelData.title
tags.targetLink = `https://twitch.tv/${channelData.broadcaster_login}`
// Target tags
if(text.includes('%target')) {
let userTag =
this.getFirstUserTagInText(userData?.input ?? '') // @-tag
?? userData?.input?.split(' ')?.shift() // Just first word
?? ''

// Was it a full link? If so use last word after /
if(userTag.includes('https://')) userTag = userTag.split('/').pop() ?? ''

// If we have a tag, get the user data, if they exist
if(userTag) {
const channelData = await modules.twitchHelix.getChannelByName(userTag)
if(channelData) {
tags.targetName = channelData.broadcaster_name
tags.targetTag = `@${channelData.broadcaster_name}`
tags.targetNick = await this.loadCleanName(channelData.broadcaster_login)
tags.targetGame = channelData.game_name
tags.targetTitle = channelData.title
tags.targetLink = `https://twitch.tv/${channelData.broadcaster_login}`
}
}
}

Expand Down

0 comments on commit a0926f7

Please sign in to comment.