71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { Collection } from "@discordjs/collection";
|
|
import type { ChatMessage } from "@twurple/chat";
|
|
import { commands } from "../../commands/collection.ts";
|
|
import type { ICommand } from "../../commands/interface.ts";
|
|
import { Config } from "../../config/config.ts";
|
|
import { BaseEvent } from "../base-event.ts";
|
|
import type { EventName } from "../registry.ts";
|
|
|
|
const Cooldowns = new Collection<string, number>();
|
|
|
|
export default class MessageEvent extends BaseEvent {
|
|
name: EventName = "message";
|
|
|
|
triggered = async (
|
|
channel: string,
|
|
user: string,
|
|
text: string,
|
|
msg: ChatMessage,
|
|
) => {
|
|
await checkMessage(channel, user, text, msg);
|
|
};
|
|
}
|
|
|
|
async function checkMessage(
|
|
channel: string,
|
|
user: string,
|
|
text: string,
|
|
msg: ChatMessage,
|
|
) {
|
|
// logInfo(`message seen: ${channel} - ${user} - ${text}`);
|
|
const prefix = Config.prefix;
|
|
if (!text.startsWith(prefix)) return;
|
|
|
|
const commandName = text
|
|
.slice(prefix.length)
|
|
.trim()
|
|
.split(/ +/g)[0]
|
|
.toLowerCase();
|
|
// logInfo(`available commands: ${commands.toJSON()}`);
|
|
// logInfo(`searching for command: ${commandName}`);
|
|
const command = commands.get(commandName);
|
|
if (!command) return;
|
|
if (!command.checkPerms(msg.userInfo)) return;
|
|
|
|
const timeLeft = checkCooldown(command);
|
|
if (timeLeft > 0) {
|
|
// return chatClient.say(
|
|
// channel,
|
|
// `@${user}, you must wait ${timeLeft} more seconds to use the command again`,
|
|
// );
|
|
}
|
|
|
|
await command.triggered(channel, user, text, msg);
|
|
}
|
|
|
|
function checkCooldown(command: ICommand): number {
|
|
const now = Date.now();
|
|
if (command.cooldown > 0) {
|
|
const cooldownTime = Cooldowns.get(command.name);
|
|
if (cooldownTime) {
|
|
if (cooldownTime < now) {
|
|
const timeLeft = 0; // TODO!!!
|
|
return timeLeft;
|
|
} else {
|
|
Cooldowns.set(command.name, now + command.cooldown * 1000);
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|