Skip to content

Commit

Permalink
chore: consolidate Twitch API listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 26, 2024
1 parent 5fec4f6 commit 2bc5e84
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 69 deletions.
3 changes: 0 additions & 3 deletions src/backend/app-management/electron/events/when-ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ exports.whenReady = async () => {
windowManagement.updateSplashScreenStatus("Refreshing Twitch account data...");
await accountAccess.refreshTwitchData();

const twitchFrontendListeners = require("../../../twitch-api/frontend-twitch-listeners");
twitchFrontendListeners.setupListeners();

windowManagement.updateSplashScreenStatus("Starting stream status poll...");
connectionManager.startOnlineCheckInterval();

Expand Down
62 changes: 61 additions & 1 deletion src/backend/twitch-api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ApiClient } from "@twurple/api";
import { ApiClient, HelixChannelUpdate } from "@twurple/api";
import { AuthProvider } from "@twurple/auth";

import logger from "../logwrapper";
import accountAccess from "../common/account-access";
import frontendCommunicator from "../common/frontend-communicator";

import { UserContextApiClient } from "./user-context-api-client";

Expand All @@ -28,6 +29,65 @@ class TwitchApi {
private _streamerClient: ApiClient;
private _botClient: UserContextApiClient;

constructor() {
frontendCommunicator.onAsync("search-twitch-games", (query: string) => {
return this.categories.searchCategories(query);
});

frontendCommunicator.onAsync("search-twitch-channels", async (query: string) => {
const response = await this.streamerClient.search.searchChannels(query, { limit: 10 });
return (response?.data ?? []).map(c => ({
id: c.id,
username: c.name,
displayName: c.displayName,
avatarUrl: c.thumbnailUrl
}));
});

frontendCommunicator.onAsync("process-automod-message", async ({ messageId, allow }: { messageId: string, allow: boolean }) => {
const accountAccess = require("../common/account-access");
const streamerChannelId = accountAccess.getAccounts().streamer.channelId;
try {
await this.streamerClient.moderation.processHeldAutoModMessage(streamerChannelId, messageId, allow);
} catch (error) {
const likelyExpired = error?.body?.includes("attempted to update a message status that was either already set");
frontendCommunicator.send("twitch:chat:automod-update-error", { messageId, likelyExpired });
logger.error(error);
}
});

frontendCommunicator.onAsync("get-twitch-game", async (gameId: string) => {
return await this.categories.getCategoryById(gameId);
});

frontendCommunicator.onAsync("get-channel-info", async () => {
try {
const channelInfo = await this.channels.getChannelInformation();
return {
title: channelInfo.title,
gameId: channelInfo.gameId,
tags: channelInfo.tags
};
} catch (error) {
return null;
}
});

frontendCommunicator.onAsync("set-channel-info", async (data: HelixChannelUpdate) => {
try {
await this.channels.updateChannelInformation(data);
return true;
} catch (error) {
return false;
}
});

frontendCommunicator.onAsync("get-channel-rewards", async () => {
const rewards = await this.channelRewards.getCustomChannelRewards();
return rewards || [];
});
}

setupApiClients(streamerProvider: AuthProvider, botProvider: AuthProvider): void {
if (!streamerProvider && !botProvider) {
return;
Expand Down
65 changes: 0 additions & 65 deletions src/backend/twitch-api/frontend-twitch-listeners.js

This file was deleted.

0 comments on commit 2bc5e84

Please sign in to comment.