Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 23x 16x 16x 16x 16x 16x 16x 16x 14x 14x 14x 14x 2x 2x 2x 16x 16x 5x 5x 5x 1x 4x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 12x 23x 16x 16x 2x 14x 14x 14x 13x 1x 1x 12x 12x 1x 1x 23x 17x 2x 15x 15x | import { ChatMessageEvent } from "kick-api-types/payloads";
import { DbClient } from "../prisma";
import type { WebhookContext } from "../app/types";
import { sendOverlayMessage } from "../overlaySocket";
import { sendMessage } from "../functions/messages";
import { KickBroadcasterAuth } from "../functions/middleware";
import { ContentfulStatusCode } from "hono/utils/http-status";
/**
* Processes incoming chat commands and responds when the payload starts with
* the bot command prefix.
*/
export const chatHandler = async (
event: ChatMessageEvent,
_db: DbClient,
ctx: WebhookContext
): Promise<Response> => {
const content = event.content ?? "";
const trimmedContent = content.trim();
const username = (
event.sender?.username ??
event.broadcaster.username ??
""
).trim();
const avatarUrl = await resolveAvatarUrl(
username,
event.sender?.profile_picture ?? undefined
);
const roomId = `overlay-chat-${event.broadcaster.user_id}`;
console.log(
`[Chat] Received message broadcaster=${event.broadcaster.username}[${
event.broadcaster.user_id
}] sender=${username || "<unknown>"} contentLength=${content.length}`
);
if (trimmedContent.length > 0) {
const overlayOverrides = ctx.env.WS_URL
? {
endpoint: ctx.env.WS_URL,
}
: undefined;
console.debug(
`[Chat] Forwarding overlay message room=${roomId} sender=${
username || "<unknown>"
}`
);
const overlayPayload = await sendOverlayMessage(
{
roomId,
author: username,
text: content,
platform: "kick",
avatarUrl,
},
overlayOverrides
);
if (!overlayPayload) {
const displayName = username || "<unknown>";
console.debug(
`[Chat] Overlay relay not ready; skipped room=${roomId} sender=${displayName}`
);
}
} else {
console.debug(
`[Chat] Ignoring blank message broadcaster=${event.broadcaster.user_id}`
);
}
const prefix = "!";
if (content.startsWith(prefix)) {
const [command] = content.slice(prefix.length).trim().split(/\s+/);
console.log(
`[Chat] Command detected broadcaster=${event.broadcaster.username}[${
event.broadcaster.user_id
}] command=${command || "<none>"}`
);
if (!command) {
return ctx.json({ ok: true }, { status: 200 });
}
if (command === "ping") {
const broadcasterAuth = ctx.get(
"kickBroadcasterAuth"
) as KickBroadcasterAuth | null;
if (!broadcasterAuth) {
console.error(
`[event:${event.eventType}:error] Broadcaster ${event.broadcaster.username}[${event.broadcaster.user_id}] is not registered.`
);
return ctx.json(
{ message: "Broadcaster not registered" },
{ status: 404 }
);
}
const message = "Pong!";
const sent = await sendMessage({
broadcaster: {
name: event.broadcaster.username!,
accessToken: broadcasterAuth.accessToken,
},
message,
});
if (sent.sent) {
console.log("[Chat-Command] Message sent");
return ctx.json(
{ message: sent.message },
{ status: sent.status as ContentfulStatusCode }
);
} else {
console.log("[Chat-Command] Message not sent");
return ctx.json(
{ message: sent.message },
{ status: sent.status as ContentfulStatusCode }
);
}
} else {
console.debug(
`[Chat] Unknown command ignored broadcaster=${event.broadcaster.user_id} command=${command}`
);
}
}
return ctx.json({ ok: true }, { status: 200 });
};
type AvatarLookupResponse = {
profile_pic?: string | null;
user?: string | null;
};
const resolveAvatarUrl = async (
username: string,
fallback?: string
): Promise<string | undefined> => {
const trimmedUsername = username.trim();
if (!trimmedUsername) {
return sanitizeAvatar(fallback);
}
const requestUrl = `https://api.stream-stuff.com/kickpfp.php?streamer=${encodeURIComponent(
trimmedUsername
)}`;
try {
const response = await fetch(requestUrl, {
method: "GET",
headers: { accept: "application/json" },
});
if (!response.ok) {
console.warn(
`[Chat] Avatar lookup failed streamer=${trimmedUsername} status=${response.status}`
);
return sanitizeAvatar(fallback);
}
const payload = (await response.json()) as AvatarLookupResponse;
return sanitizeAvatar(payload.profile_pic) ?? sanitizeAvatar(fallback);
} catch (error) {
console.error(
`[Chat] Avatar lookup errored streamer=${trimmedUsername}`,
error
);
return sanitizeAvatar(fallback);
}
};
const sanitizeAvatar = (value?: string | null): string | undefined => {
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
};
|