initial commit

This commit is contained in:
Darius
2025-09-26 13:41:46 +02:00
commit 9a961363f3
17 changed files with 1553 additions and 0 deletions

9
src/events/collection.ts Normal file
View File

@@ -0,0 +1,9 @@
import { Collection } from "@discordjs/collection";
import ConnectedEvent from "./event-connected.ts";
import MessageEvent from "./event-message.ts";
import type { IEvent } from "./interface.ts";
export const events = new Collection<string, IEvent>();
events.set(ConnectedEvent.name, new ConnectedEvent());
events.set(MessageEvent.name, new MessageEvent());

View File

@@ -0,0 +1,11 @@
import type { Events } from "tmi.js";
import { logSuccess } from "../logger/logger.ts";
import type { IEvent } from "./interface.ts";
export default class ConnectedEvent implements IEvent {
name: keyof Events = "connected";
triggered = async () => {
logSuccess("connected");
};
}

View File

@@ -0,0 +1,78 @@
import { Collection } from "@discordjs/collection";
import type { ChatUserstate, Client, Events } from "tmi.js";
import { commands } from "../commands/collection.ts";
import type { ICommand } from "../commands/interface.ts";
import { Config } from "../config/config.ts";
import type { IEvent } from "./interface.ts";
const Cooldowns = new Collection<string, number>();
export default class MessageEvent implements IEvent {
name: keyof Events = "message";
triggered = async (
client: Client,
channel: string,
state: ChatUserstate,
message: string,
self: boolean,
) => {
if (self) return;
await checkMessage(client, channel, state, message);
};
}
async function checkMessage(
client: Client,
channel: string,
state: ChatUserstate,
message: string,
) {
const prefix = Config.prefix;
if (!message.startsWith(prefix)) return;
const args = message.slice(prefix.length).trim().split(/ +/g);
const commandName = args[0].toLowerCase();
const command = commands.get(commandName);
if (!command) return;
if (!command.enabled) return;
const userId = state["user-id"] as string;
if (command.requirements.developer && !isDeveloper(userId)) return;
if (command.requirements.mod && !isMod(state)) return;
const timeLeft = checkCooldown(command);
if (timeLeft > 0) {
return client.say(
channel,
`you must wait ${timeLeft} more seconds to use the command again`,
);
}
await command.triggered(client, channel, state, message, args);
}
function isDeveloper(userId: string): boolean {
return Config.developers.includes(userId);
}
function isMod(state: ChatUserstate): boolean {
return state.mod as boolean;
}
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;
}

6
src/events/interface.ts Normal file
View File

@@ -0,0 +1,6 @@
import type { Events } from "tmi.js";
export interface IEvent {
name: keyof Events;
triggered(...args: unknown[]): Promise<unknown>;
}