From b22bafd6e7e09484170964adcdbc03d70eb960cd Mon Sep 17 00:00:00 2001 From: John Costa Date: Mon, 30 Jun 2025 18:20:41 +0100 Subject: [PATCH] feat: changing lights on follow --- src/lib/index.ts | 28 ++++++++++++++++++++++++++-- src/routes/api/webhook/+server.ts | 9 +++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 2389e5f..3d1b7e0 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -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); + } }; }; diff --git a/src/routes/api/webhook/+server.ts b/src/routes/api/webhook/+server.ts index 23d9b8b..1431902 100644 --- a/src/routes/api/webhook/+server.ts +++ b/src/routes/api/webhook/+server.ts @@ -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(); }