Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add chatFeed for slash commands #2527

Merged
merged 7 commits into from
Jun 29, 2024
Merged
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
Loading