feat: changing lights on follow

This commit is contained in:
2025-06-30 18:20:41 +01:00
parent 40410b1412
commit b22bafd6e7
2 changed files with 29 additions and 8 deletions

View File

@ -27,14 +27,38 @@ const buildReq = (hex: string): WLEDAPI => {
};
};
export const createLedClient = (addr: string) => {
const preset = { ps: 5 };
const timeout = (delay: number) => new Promise((r) => setTimeout(r, delay));
export const createLedClient = (_addr: string) => {
const addr = getAddr(_addr);
const getState = async () => {
return fetch(addr).then(r => r.json());
}
const setState = async (body: object) => {
return fetch(addr, { method: 'POST', body: JSON.stringify(body) });
}
return {
async setColor(hex: string) {
await fetch(getAddr(addr), {
await fetch(addr, {
method: "POST",
body: JSON.stringify(buildReq(hex)),
});
},
async setRave(duration: number) {
const currentState = await getState();
await setState(preset);
await timeout(duration);
await setState(currentState);
}
};
};

View File

@ -2,14 +2,12 @@ import { createHmac, timingSafeEqual } from "node:crypto";
import type { RequestHandler } from "@sveltejs/kit";
import { HASH_SECRET } from "$env/static/private";
import { twitchFollowEvent } from "$lib/twitch";
import { create } from "node:domain";
import { createLedClient } from "$lib";
import { client } from "../../+page.server";
const getHmacMessage = (request: Request, body: string): string => {
return (
request.headers.get(TWITCH_MESSAGE_ID) +
request.headers.get(TWITCH_MESSAGE_TIMESTAMP) + body
request.headers.get(TWITCH_MESSAGE_ID)! +
request.headers.get(TWITCH_MESSAGE_TIMESTAMP)! + body
);
};
@ -52,9 +50,8 @@ export const fallback: RequestHandler = async ({ request }) => {
const { user_name } = event.data.event;
client.setColor("#FFFFFF");
client.setRave(5_000);
// Handle notification...
console.log(`${user_name} has just subscribed`);
return new Response();
}