Skip to content

Commit 382f86b

Browse files
committed
chore: abstract user emotes call to API wrapper
1 parent a5ac3ed commit 382f86b

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

‎src/backend/chat/chat-helpers.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,17 @@ class FirebotChatHelpers {
8282

8383
try {
8484
let streamerEmotes: HelixEmoteBase[] = [];
85-
let botEmotes: HelixEmoteBase[] = [];
8685

8786
if (this._getAllTwitchEmotes) {
8887
logger.debug(`Caching all available Twitch emotes for streamer ${streamer.username}`);
8988

9089
// This includes: global, streamer channel, all channels streamer is subscribed to, etc.
9190
// This may take SEVERAL calls so it can take several seconds to complete
92-
streamerEmotes = await client.chat.getUserEmotesPaginated(streamer.userId).getAll();
91+
streamerEmotes = await twitchApi.chat.getAllUserEmotes();
9392

9493
if (!streamerEmotes) {
9594
return;
9695
}
97-
98-
// Make sure to remove any duplicates, because Twitch does that for some reason
99-
streamerEmotes = streamerEmotes.filter((emote, index, arr) => arr.findIndex(e => emote.id === e.id) === index);
10096
} else {
10197
logger.debug(`Caching Twitch channel emotes for ${streamer.username}`);
10298
const channelEmotes = await client.chat.getChannelEmotes(streamer.userId);
@@ -120,11 +116,7 @@ class FirebotChatHelpers {
120116
if (bot.loggedIn) {
121117
logger.debug(`Caching all available Twitch emotes for bot ${bot.username}`);
122118

123-
const botClient = twitchApi.botClient;
124-
if (botClient != null) {
125-
botEmotes = await botClient.chat.getUserEmotesPaginated(bot.userId, streamer.userId).getAll();
126-
this._twitchEmotes.bot = botEmotes.filter((emote, index, arr) => arr.findIndex(e => emote.id === e.id) === index);
127-
}
119+
this._twitchEmotes.bot = await twitchApi.chat.getAllUserEmotes("bot") ?? [];
128120
} else {
129121
logger.debug("Bot account not logged in; Skipping Twitch bot emotes");
130122
}

‎src/backend/twitch-api/resource/chat.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import logger from '../../logwrapper';
22
import accountAccess from "../../common/account-access";
3-
import { ApiClient, HelixChatAnnouncementColor, HelixChatChatter, HelixSendChatAnnouncementParams, HelixSentChatMessage, HelixUpdateChatSettingsParams } from "@twurple/api";
3+
import {
4+
ApiClient,
5+
HelixChatAnnouncementColor,
6+
HelixChatChatter,
7+
HelixSendChatAnnouncementParams,
8+
HelixSentChatMessage,
9+
HelixUpdateChatSettingsParams,
10+
HelixUserEmote
11+
} from "@twurple/api";
412

513
interface ResultWithError<TResult, TError> {
614
success: boolean;
@@ -321,4 +329,36 @@ export class TwitchChatApi {
321329
return null;
322330
}
323331
}
332+
333+
/**
334+
* Gets all emotes that can be used on Twitch by the specified Firebot account.
335+
*
336+
* @param account Either `streamer` or `bot`
337+
* @returns An array of {@link HelixUserEmote} containing all the emotes the user can use on Twitch, or null if the request failed
338+
*/
339+
async getAllUserEmotes(account: "streamer" | "bot" = "streamer"): Promise<HelixUserEmote[]> {
340+
const streamerUserId = accountAccess.getAccounts().streamer.userId;
341+
let userId = streamerUserId;
342+
let client = this._streamerClient;
343+
344+
if (account === "bot") {
345+
const bot = accountAccess.getAccounts().bot;
346+
if (bot.loggedIn !== true) {
347+
return [];
348+
}
349+
350+
userId = bot.userId;
351+
client = this._botClient;
352+
}
353+
354+
try {
355+
const emotes = await client.chat.getUserEmotesPaginated(userId, streamerUserId).getAll();
356+
357+
// Filter out any duplicates that may come back from the API
358+
return emotes.filter((emote, index, arr) => arr.findIndex(e => emote.id === e.id) === index);
359+
} catch (error) {
360+
logger.error(`Error getting all user emotes for ${account}`, error.message);
361+
return null;
362+
}
363+
}
324364
}

0 commit comments

Comments
 (0)