add ping command

This commit is contained in:
Darius
2025-09-29 00:11:55 +02:00
parent 10eee0c0fd
commit dbfb4f5d86
7 changed files with 108 additions and 23 deletions

13
biome.json Normal file
View File

@@ -0,0 +1,13 @@
{
"linter": {
"rules": {
"correctness": {
"useImportExtensions": {
"options": {
"forceJsExtensions": true
}
}
}
}
}
}

View File

@@ -1,4 +1,5 @@
import type { ChatUser } from "@twurple/chat"; import type { ChatUser } from "@twurple/chat";
import { Config } from "../config/config.ts";
import { chatClient } from "../core/chat-client.ts"; import { chatClient } from "../core/chat-client.ts";
import type { ICommand, ICommandRequirements } from "./interface.ts"; import type { ICommand, ICommandRequirements } from "./interface.ts";
@@ -10,7 +11,22 @@ export abstract class BaseCommand implements ICommand {
abstract name: string; abstract name: string;
abstract cooldown: number; abstract cooldown: number;
abstract enabled: boolean; abstract enabled: boolean;
abstract checkPerms(user: ChatUser): boolean;
abstract triggered(...args: unknown[]): Promise<unknown>; abstract triggered(...args: unknown[]): Promise<unknown>;
abstract requirements: ICommandRequirements; abstract requirements: ICommandRequirements;
checkPerms = (user: ChatUser): boolean => {
if (!this.enabled) {
return false;
}
if (
(this.requirements.developer &&
!Config.developers.includes(user.userId)) ||
(this.requirements.mod && !user.isMod)
) {
return false;
}
return true;
};
} }

View File

@@ -1,9 +1,12 @@
import { Collection } from "@discordjs/collection"; import { Collection } from "@discordjs/collection";
import { PingCommand } from "./impl/ping.ts";
import { SongCommand } from "./impl/song.ts"; import { SongCommand } from "./impl/song.ts";
import type { ICommand } from "./interface.ts"; import type { ICommand } from "./interface.ts";
export const commands = new Collection<string, ICommand>(); export const commands = new Collection<string, ICommand>();
const songCommand = new SongCommand(); const songCommand = new SongCommand();
const pingCommand = new PingCommand();
commands.set(songCommand.name, songCommand); commands.set(songCommand.name, songCommand);
commands.set(pingCommand.name, pingCommand);

40
src/commands/impl/ping.ts Normal file
View File

@@ -0,0 +1,40 @@
import type { ChatMessage } from "@twurple/chat";
import { calculateSecondsBetween } from "../../util/general.ts";
import { logSuccess } from "../../util/logger.ts";
import { BaseCommand } from "../base-command.ts";
import type { ICommandRequirements } from "../interface.ts";
export class PingCommand extends BaseCommand {
started: number;
constructor() {
super();
this.started = Date.now();
}
name = "ping";
cooldown = 0;
enabled = true;
requirements: ICommandRequirements = {
developer: false,
mod: false,
};
triggered = async (
channel: string,
user: string,
_text: string,
msg: ChatMessage,
) => {
logSuccess(`${channel} ${user} ping command triggered`);
const uptime = getUptime(this.started, Date.now());
const message = `uptime: ${uptime}`;
this.chatClient.say(channel, message, {
replyTo: msg,
});
};
}
function getUptime(start: number, now: number): string {
return calculateSecondsBetween(start, now).toReadable();
}

View File

@@ -1,7 +1,6 @@
import type { ChatMessage, ChatUser } from "@twurple/chat"; import type { ChatMessage } from "@twurple/chat";
import axios from "axios"; import axios from "axios";
import { Config } from "../../config/config.ts"; import { Config } from "../../config/config.ts";
import { chatClient } from "../../core/chat-client.ts";
import { logSuccess, logWarning } from "../../util/logger.ts"; import { logSuccess, logWarning } from "../../util/logger.ts";
import { BaseCommand } from "../base-command.ts"; import { BaseCommand } from "../base-command.ts";
import type { ICommandRequirements } from "../interface.ts"; import type { ICommandRequirements } from "../interface.ts";
@@ -23,7 +22,7 @@ export class SongCommand extends BaseCommand {
enabled = true; enabled = true;
requirements: ICommandRequirements = { requirements: ICommandRequirements = {
developer: true, developer: false,
mod: false, mod: false,
}; };
@@ -37,27 +36,11 @@ export class SongCommand extends BaseCommand {
const song = await getSongFromTidal(); const song = await getSongFromTidal();
if (song) { if (song) {
logSuccess(song); logSuccess(song);
chatClient.say(channel, song, { replyTo: msg }); this.chatClient.say(channel, song, { replyTo: msg });
} else { } else {
chatClient.say(channel, "tidal not running..", { replyTo: msg }); this.chatClient.say(channel, "tidal not running..", { replyTo: msg });
} }
}; };
checkPerms = (user: ChatUser): boolean => {
if (!this.enabled) {
return false;
}
if (
(this.requirements.developer &&
!Config.developers.includes(user.userId)) ||
(this.requirements.mod && !user.isMod)
) {
return false;
}
return true;
};
} }
async function getSongFromTidal(): Promise<string> { async function getSongFromTidal(): Promise<string> {

View File

@@ -40,7 +40,7 @@ async function checkMessage(
// logInfo(`searching for command: ${commandName}`); // logInfo(`searching for command: ${commandName}`);
const command = commands.get(commandName); const command = commands.get(commandName);
if (!command) return; if (!command) return;
if (!command.checkPerms) return; if (!command.checkPerms(msg.userInfo)) return;
const timeLeft = checkCooldown(command); const timeLeft = checkCooldown(command);
if (timeLeft > 0) { if (timeLeft > 0) {

View File

@@ -25,3 +25,33 @@ export async function promptForInput(prompt: string): Promise<string> {
}); });
}); });
} }
export function calculateSecondsBetween(
start: number,
end: number,
): { seconds: number; toReadable: () => string } {
const seconds = (end - start) / 1000;
return {
seconds,
toReadable: () => secondsToReadable(seconds),
};
}
export function secondsToReadable(secs: number): string {
var days = Math.floor(secs / (3600 * 24));
var hours = Math.floor((secs % (3600 * 24)) / 3600);
var minutes = Math.floor((secs % 3600) / 60);
var seconds = Math.floor(secs % 60);
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
var hourDisplay =
hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
var minuteDisplay =
minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
var secondDisplay =
seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(
/,\s*$/,
"",
);
}