change sendMessage wrapper, migrate from websocket connection to webhook for EventSub (reverse proxy)

This commit is contained in:
2025-06-24 01:36:43 +02:00
parent 37a38eb9e0
commit 2a430befbf
9 changed files with 75 additions and 30 deletions

View File

@@ -37,6 +37,6 @@ export { intents };
import { singleUserMode, chatterApi, chatterId, streamerId } from "..";
/** Helper function to send a message to the stream */
export const sendMessage = async (message: string, options?: HelixSendChatMessageParams) => {
singleUserMode ? await chatterApi.chat.sendChatMessage(streamerId, message, options) : chatterApi.asUser(chatterId, async newapi => newapi.chat.sendChatMessage(streamerId, message, options));
export const sendMessage = async (message: string, replyParentMessageId?: string) => {
singleUserMode ? await chatterApi.chat.sendChatMessage(streamerId, message, { replyParentMessageId }) : chatterApi.asUser(chatterId, async newapi => newapi.chat.sendChatMessage(streamerId, message, { replyParentMessageId }));
};

View File

@@ -5,6 +5,6 @@ export default new Command('ping',
['ping'],
[],
async msg => {
await sendMessage('pong!', { replyParentMessageId: msg.messageId });
await sendMessage('pong!', msg.messageId);
}
);

View File

@@ -6,12 +6,11 @@ export default new Command('yabai',
['yabai', 'goon'],
[],
async msg => {
const replyobj: HelixSendChatMessageParams = { replyParentMessageId: msg.messageId }
const rand = Math.floor(Math.random() * 100) + 1;
if (rand < 25) await sendMessage(`${rand}% yabai! chumpi4Bewwy`, replyobj);
else if (rand < 50) await sendMessage(`${rand}% yabai chumpi4Hustle`, replyobj);
else if (rand === 50) await sendMessage(`${rand}% yabai kiawaBlank`, replyobj);
else if (rand < 80) await sendMessage(`${rand}% yabai chumpi4Shock`, replyobj);
else await sendMessage(`${rand}% yabai chumpi4Jail`, replyobj);
if (rand < 25) await sendMessage(`${rand}% yabai! chumpi4Bewwy`, msg.messageId);
else if (rand < 50) await sendMessage(`${rand}% yabai chumpi4Hustle`, msg.messageId);
else if (rand === 50) await sendMessage(`${rand}% yabai kiawaBlank`, msg.messageId);
else if (rand < 80) await sendMessage(`${rand}% yabai chumpi4Shock`, msg.messageId);
else await sendMessage(`${rand}% yabai chumpi4Jail`, msg.messageId);
}
);

View File

@@ -1,4 +1,7 @@
import { eventSub } from "..";
import { eventSub, streamerApi } from "..";
await streamerApi.eventSub.deleteAllSubscriptions();
console.info('Succesfully deleted all unused EventSub subscriptions');
eventSub.onRevoke(event => {
console.info(`Successfully revoked EventSub subscription: ${event.id}`);
@@ -9,9 +12,9 @@ eventSub.onSubscriptionCreateSuccess(event => {
});
eventSub.onSubscriptionCreateFailure(event => {
eventSub.stop()
console.error(`Failed to create EventSub subscription: ${event.id}`);
eventSub.start()
event.stop()
event.start()
});
eventSub.onSubscriptionDeleteSuccess(event => {
@@ -29,3 +32,5 @@ for (const file of files) {
if (file === import.meta.file) continue;
await import(import.meta.dir + '/' + file.slice(0, -3));
};
eventSub.start();

View File

@@ -1,4 +1,3 @@
import { redis } from "bun";
import { chatterId, streamerId, eventSub, commandPrefix, singleUserMode } from "..";
import { User } from "../user";
import commands, { sendMessage } from "../commands";

View File

@@ -1,16 +1,21 @@
import { createAuthProvider } from "./auth";
import { ApiClient } from "@twurple/api";
import { EventSubWsListener } from "@twurple/eventsub-ws";
import { EventSubHttpListener, ReverseProxyAdapter } from "@twurple/eventsub-http";
import { intents } from "./commands";
const CHATTERBASEINTENTS = ["user:read:chat", "user:write:chat", "user:bot"];
const STREAMERBASEINTENTS = ["user:read:chat"];
const STREAMERBASEINTENTS = ["user:read:chat", "channel:bot", "moderation:read"];
export const singleUserMode = process.env.CHATTER_IS_STREAMER === 'true';
export const chatterId = process.env.CHATTER_ID ?? "";
if (chatterId === "") { console.error('Please set a CHATTER_ID in the .env'); process.exit(1); };
export const streamerId = process.env.STREAMER_ID ?? "";
if (streamerId === "") { console.log('Please set a STREAMER_ID in the .env'); process.exit(1); };
if (streamerId === "") { console.error('Please set a STREAMER_ID in the .env'); process.exit(1); };
const hostName = process.env.EVENTSUB_HOSTNAME ?? "";
if (hostName === "") { console.error('Please set a EVENTSUB_HOSTNAME in the .env'); process.exit(1); };
const port = Number(process.env.EVENTSUB_PORT) ?? 0;
if (port === 0) { console.error('Please set a EVENTSUB_PORT in the .env'); process.exit(1); };
const chatterIntents = singleUserMode ? CHATTERBASEINTENTS.concat(STREAMERBASEINTENTS) : CHATTERBASEINTENTS;
const streamerIntents = STREAMERBASEINTENTS.concat(intents);
@@ -25,10 +30,12 @@ export const chatterApi = new ApiClient({ authProvider: chatterAuthProvider });
export const streamerApi = streamerAuthProvider ? new ApiClient({ authProvider: streamerAuthProvider }) : chatterApi; // if there is no streamer user, use the chatter user
/** As the streamerApi has either the streamer or the chatter if the chatter IS the streamer this has streamer permissions */
export const eventSub = new EventSubWsListener({ apiClient: streamerApi });
export const eventSub = new EventSubHttpListener({
apiClient: streamerApi,
adapter: new ReverseProxyAdapter({ hostName, port }),
secret: Bun.randomUUIDv7()
});
export const commandPrefix = process.env.COMMAND_PREFIX ?? "!";
await import("./events");
eventSub.start();