From 4317c079b1eb35e61fa7fe2dfb28ac33030b5453 Mon Sep 17 00:00:00 2001 From: qwerinope Date: Mon, 1 Sep 2025 00:31:27 +0200 Subject: [PATCH] fixes, alerts/chatwidget websocket error handling, make chatterbot have the new bot badge --- src/commands/index.ts | 4 ++-- src/db/connection.ts | 2 +- src/index.ts | 2 +- src/web/index.ts | 29 +++++++++++++++++------------ 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/commands/index.ts b/src/commands/index.ts index ce26ba2..d46dd22 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -42,9 +42,9 @@ for (const [name, item] of Array.from(items)) { export default commands; export { basecommands }; -import { singleUserMode, chatterApi, chatterId, streamerId } from "main"; +import { chatterApi, chatterId, streamerId } from "main"; /** Helper function to send a message to the stream */ 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 })); + await chatterApi.chat.sendChatMessageAsApp(chatterId, streamerId, message, { replyParentMessageId }) }; diff --git a/src/db/connection.ts b/src/db/connection.ts index 7b07425..204c380 100644 --- a/src/db/connection.ts +++ b/src/db/connection.ts @@ -43,7 +43,7 @@ export type cheerEventRecord = { export type cheerRecord = { id?: string; - user?: string; + user: string; amount: number; }; diff --git a/src/index.ts b/src/index.ts index 95bb29c..c059401 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,7 +7,7 @@ import { addInvuln } from "lib/invuln"; import { redis } from "bun"; const CHATTERINTENTS = ["user:read:chat", "user:write:chat", "user:bot"]; -const STREAMERINTENTS = ["user:read:chat", "moderation:read", "channel:manage:moderators", "moderator:manage:banned_users", "bits:read", "channel:moderate"]; +const STREAMERINTENTS = ["channel:bot", "user:read:chat", "moderation:read", "channel:manage:moderators", "moderator:manage:banned_users", "bits:read", "channel:moderate"]; export const singleUserMode = process.env.CHATTER_IS_STREAMER === 'true'; export const chatterId = process.env.CHATTER_ID ?? ""; diff --git a/src/web/index.ts b/src/web/index.ts index 3d24dc2..0d059aa 100644 --- a/src/web/index.ts +++ b/src/web/index.ts @@ -28,18 +28,23 @@ export default Bun.serve({ }, websocket: { message(ws, omessage) { - const message = JSON.parse(omessage.toString()) as serverInstruction; - if (!message.type) return; - switch (message.type) { - case 'subscribe': - if (!message.target) return; - const target = message.target.toLowerCase(); - ws.subscribe(message.target); - ws.send(JSON.stringify({ - function: 'serverNotification', - message: `Successfully subscribed to ${target} events` - } as serverNotificationEvent)); // Both alerts and chatwidget eventsub subscriptions have the notification field - break; + try { + const message = JSON.parse(omessage.toString()) as serverInstruction; + if (!message.type) return; + switch (message.type) { + case 'subscribe': + if (!message.target) return; + const target = message.target.toLowerCase(); + ws.subscribe(message.target); + ws.send(JSON.stringify({ + function: 'serverNotification', + message: `Successfully subscribed to ${target} events` + } as serverNotificationEvent)); // Both alerts and chatwidget eventsub subscriptions have the notification field + break; + }; + } catch (e) { + ws.send('Incorrect instruction. Closing websocket connection'); + ws.close(); }; }, close(ws) {