add easy setup
This commit is contained in:
12
src/events/base-event.ts
Normal file
12
src/events/base-event.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { chatClient } from "../core/chat-client.ts";
|
||||
import type { IEvent } from "./interface.ts";
|
||||
import type { EventName } from "./registry.ts";
|
||||
|
||||
export abstract class BaseEvent implements IEvent {
|
||||
protected get chatClient() {
|
||||
return chatClient;
|
||||
}
|
||||
|
||||
abstract name: EventName;
|
||||
abstract triggered(...args: unknown[]): Promise<unknown>;
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { logSuccess } from "../logger/logger.ts";
|
||||
import type { IEvent } from "./interface.ts";
|
||||
import { logSuccess } from "../util/logger.ts";
|
||||
import { BaseEvent } from "./base-event.ts";
|
||||
import type { EventName } from "./registry.ts";
|
||||
|
||||
export default class ConnectedEvent implements IEvent {
|
||||
export default class ConnectedEvent extends BaseEvent {
|
||||
name: EventName = "connected";
|
||||
|
||||
triggered = async () => {
|
||||
|
||||
@@ -3,12 +3,12 @@ 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 type { IEvent } from "./interface.ts";
|
||||
import { BaseEvent } from "./base-event.ts";
|
||||
import type { EventName } from "./registry.ts";
|
||||
|
||||
const Cooldowns = new Collection<string, number>();
|
||||
|
||||
export default class MessageEvent implements IEvent {
|
||||
export default class MessageEvent extends BaseEvent {
|
||||
name: EventName = "message";
|
||||
|
||||
triggered = async (
|
||||
@@ -45,7 +45,7 @@ async function checkMessage(
|
||||
|
||||
const timeLeft = checkCooldown(command);
|
||||
if (timeLeft > 0) {
|
||||
// return client.say(
|
||||
// return chatClient.say(
|
||||
// channel,
|
||||
// `@${user}, you must wait ${timeLeft} more seconds to use the command again`,
|
||||
// );
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
import type { ChatClient } from "@twurple/chat";
|
||||
import { chatClient } from "../core/chat-client.ts";
|
||||
import ConnectedEvent from "./event-connected.ts";
|
||||
import MessageEvent from "./event-message.ts";
|
||||
import type { IEvent } from "./interface.ts";
|
||||
|
||||
export const eventRegistry = {
|
||||
message: (client: ChatClient, handler: IEvent) =>
|
||||
client.onMessage(handler.triggered),
|
||||
connected: (client: ChatClient, handler: IEvent) =>
|
||||
client.onConnect(handler.triggered),
|
||||
message: (handler: IEvent) => chatClient.onMessage(handler.triggered),
|
||||
connected: (handler: IEvent) => chatClient.onConnect(handler.triggered),
|
||||
};
|
||||
|
||||
export function registerAllEvents(client: ChatClient) {
|
||||
export function registerAllEvents() {
|
||||
const events = [new MessageEvent(), new ConnectedEvent()];
|
||||
|
||||
for (const event of events) {
|
||||
const registerFn = eventRegistry[event.name];
|
||||
if (registerFn) {
|
||||
registerFn(client, event);
|
||||
}
|
||||
eventRegistry[event.name](event); // Registers the event
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user