add ping command
This commit is contained in:
13
biome.json
Normal file
13
biome.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"linter": {
|
||||
"rules": {
|
||||
"correctness": {
|
||||
"useImportExtensions": {
|
||||
"options": {
|
||||
"forceJsExtensions": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { ChatUser } from "@twurple/chat";
|
||||
import { Config } from "../config/config.ts";
|
||||
import { chatClient } from "../core/chat-client.ts";
|
||||
import type { ICommand, ICommandRequirements } from "./interface.ts";
|
||||
|
||||
@@ -10,7 +11,22 @@ export abstract class BaseCommand implements ICommand {
|
||||
abstract name: string;
|
||||
abstract cooldown: number;
|
||||
abstract enabled: boolean;
|
||||
abstract checkPerms(user: ChatUser): boolean;
|
||||
abstract triggered(...args: unknown[]): Promise<unknown>;
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import { Collection } from "@discordjs/collection";
|
||||
import { PingCommand } from "./impl/ping.ts";
|
||||
import { SongCommand } from "./impl/song.ts";
|
||||
import type { ICommand } from "./interface.ts";
|
||||
|
||||
export const commands = new Collection<string, ICommand>();
|
||||
|
||||
const songCommand = new SongCommand();
|
||||
const pingCommand = new PingCommand();
|
||||
|
||||
commands.set(songCommand.name, songCommand);
|
||||
commands.set(pingCommand.name, pingCommand);
|
||||
|
||||
40
src/commands/impl/ping.ts
Normal file
40
src/commands/impl/ping.ts
Normal 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();
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { ChatMessage, ChatUser } from "@twurple/chat";
|
||||
import type { ChatMessage } from "@twurple/chat";
|
||||
import axios from "axios";
|
||||
import { Config } from "../../config/config.ts";
|
||||
import { chatClient } from "../../core/chat-client.ts";
|
||||
import { logSuccess, logWarning } from "../../util/logger.ts";
|
||||
import { BaseCommand } from "../base-command.ts";
|
||||
import type { ICommandRequirements } from "../interface.ts";
|
||||
@@ -23,7 +22,7 @@ export class SongCommand extends BaseCommand {
|
||||
enabled = true;
|
||||
|
||||
requirements: ICommandRequirements = {
|
||||
developer: true,
|
||||
developer: false,
|
||||
mod: false,
|
||||
};
|
||||
|
||||
@@ -37,27 +36,11 @@ export class SongCommand extends BaseCommand {
|
||||
const song = await getSongFromTidal();
|
||||
if (song) {
|
||||
logSuccess(song);
|
||||
chatClient.say(channel, song, { replyTo: msg });
|
||||
this.chatClient.say(channel, song, { replyTo: msg });
|
||||
} 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> {
|
||||
|
||||
@@ -40,7 +40,7 @@ async function checkMessage(
|
||||
// logInfo(`searching for command: ${commandName}`);
|
||||
const command = commands.get(commandName);
|
||||
if (!command) return;
|
||||
if (!command.checkPerms) return;
|
||||
if (!command.checkPerms(msg.userInfo)) return;
|
||||
|
||||
const timeLeft = checkCooldown(command);
|
||||
if (timeLeft > 0) {
|
||||
|
||||
@@ -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*$/,
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user