home assistant integration; volume for tidal; do some abstracting

This commit is contained in:
Darius
2025-10-02 21:36:19 +02:00
parent 9097266197
commit 0c54b1f776
12 changed files with 390 additions and 33 deletions

View File

@@ -0,0 +1,72 @@
import type { ChatMessage } from "@twurple/chat";
import { getVolumeFromTidal, setVolumeToTidal } from "../../util/api-tidal.ts";
import { logSuccess } from "../../util/logger.ts";
import { BaseCommand } from "../base-command.ts";
import type { ICommandRequirements } from "../interface.ts";
export class VolumeCommand extends BaseCommand {
name = "vol";
cooldown = 0;
enabled = true;
requirements: ICommandRequirements = {
developer: true,
mod: false,
};
triggered = async (
channel: string,
user: string,
text: string,
msg: ChatMessage,
) => {
logSuccess(`${channel} ${user} volume command triggered`);
const volumeText = await parseCommand(text);
if (volumeText) {
this.chatClient.say(channel, volumeText, { replyTo: msg });
} else {
this.chatClient.say(channel, "tidal not running..", { replyTo: msg });
}
};
}
async function parseCommand(message: string): Promise<string | null> {
const args = message.slice(4).trim(); // Remove '!volume'
// Case 1: no args
if (args === "") {
const volume = await getVolumeFromTidal();
if (volume) {
return `volume is at ${volume.volume} right now`;
}
}
const value = parseInt(args, 10);
// Case 2: relative
const adjustMatch = args.match(/^([+-])(\d+)$/);
if (adjustMatch) {
const volume = await getVolumeFromTidal();
if (volume) {
const wantedVolume = volume.volume + value;
const clampWantedVolume = clamp(wantedVolume);
await setVolumeToTidal(clampWantedVolume);
return `volume was at ${volume.volume} and is now ${clampWantedVolume}`;
}
}
// Case 3: absolute
const setMatch = args.match(/^(\d+)$/);
if (setMatch) {
const clampValue = clamp(value);
await setVolumeToTidal(clampValue);
return `set volume to ${clampValue}`;
}
return null;
}
function clamp(value: number): number {
return Math.min(Math.max(value, 0), 100);
}