Skip to content

Commit

Permalink
Feat: add chat feed for slash commands (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
CKY- authored Jun 29, 2024
1 parent 7de8cf0 commit 16b135f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/backend/chat/twitch-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ class TwitchChat extends EventEmitter {
return;
}
}
if (slashCommandValidationResult != null &&
slashCommandValidationResult.success === false &&
slashCommandValidationResult.foundCommand !== false) {
global.renderWindow.webContents.send("chatUpdate", {
fbEvent: "ChatAlert",
message: slashCommandValidationResult.errorMessage
});
}

// split message into fragments that don't exceed the max message length
const messageFragments = message
Expand Down
10 changes: 8 additions & 2 deletions src/backend/chat/twitch-commands/chat-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ export const shoutoutHandler: TwitchSlashCommandHandler<[string]> = {
if (targetUserId == null) {
return false;
}

return await twitchApi.chat.sendShoutout(targetUserId);
const result = await twitchApi.chat.sendShoutout(targetUserId);
if (!result.success) {
global.renderWindow.webContents.send("chatUpdate", {
fbEvent: "ChatAlert",
message: result.error
});
}
return result.success;
}
};

Expand Down
10 changes: 8 additions & 2 deletions src/backend/effects/builtin/twitch/shoutout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ const model: EffectType<{
logger.error(`Unable to shoutout channel. Twitch user ${effect.username} does not exist.`);
return false;
}

return await twitchApi.chat.sendShoutout(targetUserId);
const result = await twitchApi.chat.sendShoutout(targetUserId);
if (!result.success) {
global.renderWindow.webContents.send("chatUpdate", {
fbEvent: "ChatAlert",
message: result.error
});
}
return result.success;
}
};

Expand Down
15 changes: 11 additions & 4 deletions src/backend/twitch-api/resource/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import logger from '../../logwrapper';
import accountAccess from "../../common/account-access";
import { ApiClient, HelixChatAnnouncementColor, HelixChatChatter, HelixSendChatAnnouncementParams, HelixSentChatMessage, HelixUpdateChatSettingsParams } from "@twurple/api";

interface ResultWithError<TResult, TError> {
success: boolean;
result?: TResult;
error?: TError;
}

export class TwitchChatApi {
private _streamerClient: ApiClient;
private _botClient: ApiClient;
Expand Down Expand Up @@ -123,18 +129,19 @@ export class TwitchChatApi {
* Sends a Twitch shoutout to another channel
*
* @param targetUserId The Twitch user ID whose channel to shoutout
* @returns true when successful, error message string when unsuccessful
*/
async sendShoutout(targetUserId: string): Promise<boolean> {
async sendShoutout(targetUserId: string): Promise<ResultWithError<undefined, string>> {
const streamerId = accountAccess.getAccounts().streamer.userId;

try {
await this._streamerClient.chat.shoutoutUser(streamerId, targetUserId);
} catch (error) {
logger.error("Error sending shoutout", error.message);
return false;
const body = JSON.parse(error._body);
return { success: false, error: body.message };
}

return true;
return { success: true };
}

/**
Expand Down

0 comments on commit 16b135f

Please sign in to comment.