home assistant integration; volume for tidal; do some abstracting
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { Collection } from "@discordjs/collection";
|
||||
import { PingCommand } from "./impl/ping.ts";
|
||||
import { PositionCommand } from "./impl/position.ts";
|
||||
import { SongCommand } from "./impl/song.ts";
|
||||
import { TempCommand } from "./impl/temp.ts";
|
||||
import { VanishCommand } from "./impl/vanish.ts";
|
||||
import { VolumeCommand } from "./impl/volume.ts";
|
||||
import { WhereCommand } from "./impl/where.ts";
|
||||
import type { ICommand } from "./interface.ts";
|
||||
|
||||
export const commands = new Collection<string, ICommand>();
|
||||
@@ -10,6 +14,10 @@ const cmds: Array<ICommand> = [];
|
||||
cmds.push(new SongCommand());
|
||||
cmds.push(new PingCommand());
|
||||
cmds.push(new VanishCommand());
|
||||
cmds.push(new PositionCommand());
|
||||
cmds.push(new TempCommand());
|
||||
cmds.push(new WhereCommand());
|
||||
cmds.push(new VolumeCommand());
|
||||
|
||||
for (const command of cmds) {
|
||||
commands.set(command.name, command);
|
||||
|
||||
@@ -28,7 +28,7 @@ export class PingCommand extends BaseCommand {
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} ping command triggered`);
|
||||
const uptime = getUptime(this.started, Date.now());
|
||||
const message = `uptime: ${uptime}`;
|
||||
const message = `StickDank uptime: ${uptime}`;
|
||||
this.chatClient.say(channel, message, {
|
||||
replyTo: msg,
|
||||
});
|
||||
|
||||
48
src/commands/impl/position.ts
Normal file
48
src/commands/impl/position.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import { getDeskHeight } from "../../util/api-homeassistant.ts";
|
||||
import { logSuccess } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
|
||||
export class PositionCommand extends BaseCommand {
|
||||
name = "position";
|
||||
cooldown = 0;
|
||||
enabled = true;
|
||||
|
||||
requirements: ICommandRequirements = {
|
||||
developer: false,
|
||||
mod: false,
|
||||
};
|
||||
|
||||
triggered = async (
|
||||
channel: string,
|
||||
user: string,
|
||||
_text: string,
|
||||
msg: ChatMessage,
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} position command triggered`);
|
||||
const position = await getPosition();
|
||||
this.chatClient.say(channel, `darius is ${position} right now`, {
|
||||
replyTo: msg,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
async function getPosition(): Promise<string> {
|
||||
const heightFromHA = await getDeskHeight();
|
||||
if (!heightFromHA) {
|
||||
return "unkown";
|
||||
}
|
||||
const height = convertHeightToCentimeters(heightFromHA.state);
|
||||
if (height > 95) {
|
||||
return "standing";
|
||||
} else {
|
||||
return "sitting";
|
||||
}
|
||||
}
|
||||
|
||||
function convertHeightToCentimeters(height: string): number {
|
||||
const meters = parseFloat(height);
|
||||
const centimeters = Math.round(meters * 100);
|
||||
return centimeters;
|
||||
}
|
||||
@@ -1,21 +1,9 @@
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import axios from "axios";
|
||||
import { Config } from "../../config/config.ts";
|
||||
import { logSuccess, logWarning } from "../../util/logger.ts";
|
||||
import { getSongFromTidalFormatted } from "../../util/api-tidal.ts";
|
||||
import { logSuccess } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
|
||||
interface ISong {
|
||||
title: string;
|
||||
artists: string;
|
||||
album: string;
|
||||
playingFrom: string;
|
||||
status: "playing" | "paused";
|
||||
url: string;
|
||||
current: string;
|
||||
duration: string;
|
||||
}
|
||||
|
||||
export class SongCommand extends BaseCommand {
|
||||
name = "song";
|
||||
cooldown = 0;
|
||||
@@ -33,7 +21,7 @@ export class SongCommand extends BaseCommand {
|
||||
msg: ChatMessage,
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} song command triggered`);
|
||||
const song = await getSongFromTidal();
|
||||
const song = await getSongFromTidalFormatted();
|
||||
if (song) {
|
||||
logSuccess(song);
|
||||
this.chatClient.say(channel, song, { replyTo: msg });
|
||||
@@ -42,20 +30,3 @@ export class SongCommand extends BaseCommand {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function getSongFromTidal(): Promise<string> {
|
||||
try {
|
||||
const response = await axios.get<ISong>(
|
||||
`${Config.tidal.host}:${Config.tidal.port}/current`,
|
||||
);
|
||||
|
||||
const currentSong = response.data;
|
||||
|
||||
const status = currentSong.status === "playing" ? "▶️" : "⏸️";
|
||||
|
||||
return `listening to ${currentSong.title} by ${currentSong.artists}. ${status} ${currentSong.current}/${currentSong.duration}. link: ${currentSong.url}`;
|
||||
} catch {
|
||||
logWarning("error getting song from tidal");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
41
src/commands/impl/temp.ts
Normal file
41
src/commands/impl/temp.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import { getTemperatures } from "../../util/api-homeassistant.ts";
|
||||
import { logSuccess } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
|
||||
export class TempCommand extends BaseCommand {
|
||||
name = "temp";
|
||||
cooldown = 0;
|
||||
enabled = true;
|
||||
|
||||
requirements: ICommandRequirements = {
|
||||
developer: false,
|
||||
mod: false,
|
||||
};
|
||||
|
||||
triggered = async (
|
||||
channel: string,
|
||||
user: string,
|
||||
_text: string,
|
||||
msg: ChatMessage,
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} temp command triggered`);
|
||||
const temp = await getTemp();
|
||||
this.chatClient.say(channel, `it is ${temp}°C in darius room right now`, {
|
||||
replyTo: msg,
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
async function getTemp(): Promise<string> {
|
||||
const entities = await getTemperatures();
|
||||
const values = entities
|
||||
.map((entity) => parseFloat(entity.state))
|
||||
.filter((value) => !Number.isNaN(value));
|
||||
const average =
|
||||
values.length > 0
|
||||
? values.reduce((sum, value) => sum + value, 0) / values.length
|
||||
: 0;
|
||||
return average.toFixed(2);
|
||||
}
|
||||
72
src/commands/impl/volume.ts
Normal file
72
src/commands/impl/volume.ts
Normal 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);
|
||||
}
|
||||
27
src/commands/impl/where.ts
Normal file
27
src/commands/impl/where.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import { logSuccess } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
|
||||
export class WhereCommand extends BaseCommand {
|
||||
name = "where";
|
||||
cooldown = 0;
|
||||
enabled = true;
|
||||
|
||||
requirements: ICommandRequirements = {
|
||||
developer: false,
|
||||
mod: false,
|
||||
};
|
||||
|
||||
triggered = async (
|
||||
channel: string,
|
||||
user: string,
|
||||
_text: string,
|
||||
msg: ChatMessage,
|
||||
) => {
|
||||
logSuccess(`${channel} ${user} where command triggered`);
|
||||
this.chatClient.say(channel, `leck eier`, {
|
||||
replyTo: msg,
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user