Skip to content

Commit

Permalink
feat: add pronouns request, update message request options to support…
Browse files Browse the repository at this point in the history
… youtube broadcast id and twitch reply id
  • Loading branch information
Whipstickgostop committed Oct 19, 2024
1 parent 4b26874 commit b03b003
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ watch(client, (client) => {
<UButton color="gray" @click="async () => lastRequestResponse = await client.getUserGlobals('twitch', 'test')">Get Twitch User Globals (test)</UButton>
<UButton color="gray" @click="async () => lastRequestResponse = await client.getUserGlobal('twitch', '54601714')">Get Twitch User Global</UButton>
<UButton color="gray" @click="async () => lastRequestResponse = await client.getUserGlobal('twitch', '54601714', 'test')">Get Twitch User Global (test)</UButton>
<UButton color="gray" @click="async () => lastRequestResponse = await client.sendMessage('twitch', 'test', false, false)">Send Twitch Chat Message</UButton>
<UButton color="gray" @click="async () => lastRequestResponse = await client.sendMessage('twitch', 'test', { bot: false, internal: false })">Send Twitch Chat Message</UButton>
<UButton color="gray" @click="async () => lastRequestResponse = await client.getUserPronouns('twitch', 'whipstickgostop')">Get User Pronouns</UButton>
</div>
<div class="col-span-2 p-3 rounded-lg bg-gray-900 max-h-[600px] overflow-auto">
<code class="whitespace-pre">{{ lastRequestResponse }}</code>
Expand Down
84 changes: 80 additions & 4 deletions packages/client/src/ws/StreamerbotClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
GetMonitoredYouTubeBroadcastsResponse,
GetUserGlobalResponse,
GetUserGlobalsResponse,
GetUserPronounsResponse,
SendMessageResponse,
StreamerbotErrorResponse,
StreamerbotResponseTypes,
Expand Down Expand Up @@ -67,6 +68,48 @@ export const DefaultStreamerbotClientOptions: StreamerbotClientOptions = {
subscribe: {},
} as const;

/**
* The `StreamerbotClient` class provides an interface to connect and interact with a Streamer.bot WebSocket server.
* It allows for authentication, event subscription, and various requests to control and retrieve information from the Streamer.bot instance.
*
* @example
* ```typescript
* const client = new StreamerbotClient({
* host: 'localhost',
* port: 8080,
* password: 'your_password',
* immediate: true,
* autoReconnect: true,
* retries: 5,
* onConnect: (info) => console.log('Connected to Streamer.bot', info),
* onError: (error) => console.error('Error:', error),
* });
*
* client.on('Twitch.ChatMessage', (data) => {
* console.log('Chat message received:', data);
* });
* ```
*
* @remarks
* The client supports both browser and Node.js environments. In a Node.js environment, it uses the `ws` package for WebSocket connections.
*
* @param options - Configuration options for the StreamerbotClient instance.
* @param options.host - The host of the Streamer.bot WebSocket server.
* @param options.port - The port of the Streamer.bot WebSocket server.
* @param options.password - The password for authentication with the Streamer.bot WebSocket server.
* @param options.scheme - The scheme to use for the WebSocket connection (e.g., 'ws' or 'wss').
* @param options.endpoint - The endpoint path for the WebSocket connection.
* @param options.immediate - Whether to immediately connect to the WebSocket server upon instantiation.
* @param options.autoReconnect - Whether to automatically reconnect to the WebSocket server if the connection is lost.
* @param options.retries - The number of reconnection attempts before giving up. A negative value means infinite retries.
* @param options.subscribe - Initial subscriptions to events upon connection.
* @param options.onConnect - Callback function to be called when the client successfully connects to the WebSocket server.
* @param options.onDisconnect - Callback function to be called when the client disconnects from the WebSocket server.
* @param options.onError - Callback function to be called when an error occurs.
* @param options.onData - Callback function to be called when data is received from the WebSocket server.
*
* @public
*/
export class StreamerbotClient {
private readonly options: StreamerbotClientOptions;

Expand Down Expand Up @@ -909,14 +952,17 @@ export class StreamerbotClient {
* @version 0.2.5
* @param platform The platform to send the message to
* @param message The message content to send
* @param bot Whether the message should be sent as the bot account (Default: false)
* @param internal Whether the message should be marked as internal (Default: true)
* @param options Additional options for the message
*/
public async sendMessage(
platform: StreamerbotPlatform,
message: string,
bot = false,
internal = true,
{ bot = false, internal = true, replyId, broadcastId }: {
bot: boolean;
internal: boolean;
replyId: string | undefined;
broadcastId: string | undefined;
}
): Promise<SendMessageResponse> {
if (!this._authenticated) {
return {
Expand All @@ -925,6 +971,17 @@ export class StreamerbotClient {
} as StreamerbotErrorResponse;
}

const request = {
request: 'SendMessage',
platform,
message,
bot,
internal,
};

if (platform === 'twitch' && replyId) Object.assign(request, { replyId });
if (platform === 'youtube' && broadcastId) Object.assign(request, { broadcastId });

return await this.request({
request: 'SendMessage',
platform,
Expand All @@ -933,4 +990,23 @@ export class StreamerbotClient {
internal,
});
}


/**
* Retrieves the pronouns of a user from the specified platform.
*
* @param platform - The platform from which to retrieve the user's pronouns.
* @param userLogin - The login name of the user whose pronouns are to be retrieved.
* @returns A promise that resolves to the user's pronouns.
*/
public async getUserPronouns(
platform: StreamerbotPlatform,
userLogin: string,
): Promise<GetUserPronounsResponse> {
return await this.request<GetUserPronounsResponse>({
request: 'GetUserPronouns',
platform,
userLogin,
});
}
}
20 changes: 20 additions & 0 deletions packages/client/src/ws/types/streamerbot-pronouns.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export type StreamerbotUserPronouns = {
pronouns: string;
pronounSubject: string;
pronounObject: string;
pronounPossessive: string;
pronounPronoun: string;
pronounReflexive: string;
pronounPastTense: string;
pronounSubjectLower: string;
pronounObjectLower: string;
pronounPossessiveLower: string;
pronounPronounLower: string;
pronounReflexiveLower: string;
pronounPastTenseLower: string;
pronounCurrentTense: string;
pronounCurrentTenseLower: string;
pronounPluralLower: string;
pronounLastCached: string;
userFound: boolean;
};
1 change: 1 addition & 0 deletions packages/client/src/ws/types/streamerbot-request.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type StreamerbotRequestName =
| 'TrovoGetUserGlobals'
| 'TrovoGetUserGlobal'
| 'SendMessage'
| 'GetUserPronouns'
;

export type StreamerbotRequest = {
Expand Down
14 changes: 13 additions & 1 deletion packages/client/src/ws/types/streamerbot-response.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './streamerbot-event.types';
import { StreamerbotGlobalVariable, StreamerbotGlobalVariableName, StreamerbotUserGlobalVariable, StreamerbotVariableValue } from './streamerbot-global.types';
import { StreamerbotInfo } from './streamerbot-info.types';
import { StreamerbotUserPronouns } from './streamerbot-pronouns.types';
import { StreamerbotViewer } from './streamerbot-viewer.types.ts';
import { Prettify } from './util.types';

Expand Down Expand Up @@ -131,6 +132,11 @@ export type GetUserGlobalResponse<

export type SendMessageResponse = MaybeStreamerbotResponse<{}>;

export type GetUserPronounsResponse = MaybeStreamerbotResponse<{
userLogin: string;
pronoun: StreamerbotUserPronouns,
}>

export type StreamerbotResponseTypes =
| StreamerbotResponse<unknown>
| SubscribeResponse
Expand All @@ -149,4 +155,10 @@ export type StreamerbotResponseTypes =
| GetCodeTriggersResponse
| GetCommandsResponse
| TwitchGetEmotesResponse
| YouTubeGetEmotesResponse;
| YouTubeGetEmotesResponse
| GetGlobalsResponse
| GetGlobalResponse<StreamerbotVariableValue>
| GetUserGlobalsResponse
| GetUserGlobalResponse
| SendMessageResponse
| GetUserPronounsResponse;

0 comments on commit b03b003

Please sign in to comment.