fix recursive import
This commit is contained in:
parent
2a6167b576
commit
56c49b8e10
8 changed files with 38 additions and 27 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -12,3 +12,4 @@ docker-compose.yml
|
|||
|
||||
target/
|
||||
tsconfig.tsbuildinfo
|
||||
voices
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ import {
|
|||
type SlashCommandOptionsOnlyBuilder,
|
||||
SlashCommandSubcommandBuilder,
|
||||
} from "discord.js";
|
||||
import { join } from "node:path";
|
||||
import { requireDirectory } from "../utils/requireDirectory";
|
||||
import { AdminUsers } from "./admin";
|
||||
|
||||
export const commandExecuteNameHashMap = [];
|
||||
|
||||
export type DiscordCommandData =
|
||||
| SlashCommandBuilder
|
||||
| SlashCommandOptionsOnlyBuilder
|
||||
|
|
@ -45,12 +45,3 @@ export function defineCommand(
|
|||
execute: execute,
|
||||
};
|
||||
}
|
||||
|
||||
const commandDirectory = join(import.meta.dirname, "commands");
|
||||
export const defineCommands =
|
||||
await requireDirectory<DiscordCommand>(commandDirectory);
|
||||
export const commandExecuteNameHashMap: {
|
||||
[key: string]: DiscordCommandExecute;
|
||||
} = Object.fromEntries(
|
||||
defineCommands.map((command) => [command.data.name, command.execute]),
|
||||
);
|
||||
|
|
|
|||
20
packages/bot/commands/index.ts
Normal file
20
packages/bot/commands/index.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import type { DiscordCommandExecute } from "../command";
|
||||
|
||||
export const defineCommands = [
|
||||
(await import("./getReadChannels")).default,
|
||||
(await import("./getStatus")).default,
|
||||
(await import("./joinVoiceChannel")).default,
|
||||
(await import("./leaveVoiceChannel")).default,
|
||||
(await import("./playVoice")).default,
|
||||
(await import("./readChannel")).default,
|
||||
(await import("./setNya")).default,
|
||||
(await import("./setSupertonicStyle")).default,
|
||||
(await import("./setVoice")).default,
|
||||
(await import("./skipCurrent")).default,
|
||||
(await import("./unreadChannel")).default,
|
||||
];
|
||||
export const commandExecuteNameHashMap: {
|
||||
[key: string]: DiscordCommandExecute;
|
||||
} = Object.fromEntries(
|
||||
defineCommands.map((command) => [command.data.name, command.execute]),
|
||||
);
|
||||
|
|
@ -1,6 +1,4 @@
|
|||
import { type ClientEvents } from "discord.js";
|
||||
import { join } from "node:path";
|
||||
import { requireDirectory } from "../utils/requireDirectory";
|
||||
|
||||
export interface DiscordEvent<Event extends keyof ClientEvents> {
|
||||
event: Event;
|
||||
|
|
@ -16,7 +14,3 @@ export function defineEvent<Event extends keyof ClientEvents>(
|
|||
callback: callback,
|
||||
};
|
||||
}
|
||||
|
||||
export const eventDirectory = join(import.meta.dirname, "events");
|
||||
export const eventMap =
|
||||
await requireDirectory<DiscordEvent<any>>(eventDirectory);
|
||||
|
|
|
|||
4
packages/bot/events/index.ts
Normal file
4
packages/bot/events/index.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export const eventMap = [
|
||||
(await import("./readChannel")).default,
|
||||
(await import("./useCommand")).default,
|
||||
];
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { commandExecuteNameHashMap } from "../command";
|
||||
import { commandExecuteNameHashMap } from "../commands";
|
||||
import { defineEvent } from "../event";
|
||||
|
||||
export default defineEvent("interactionCreate", async (interaction) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Client, Events, GatewayIntentBits, REST, Routes } from "discord.js";
|
||||
import { commandExecuteNameHashMap, defineCommands } from "./command";
|
||||
import { eventMap } from "./event";
|
||||
import { commandExecuteNameHashMap, defineCommands } from "./commands";
|
||||
import { eventMap } from "./events";
|
||||
import { OutputHandler } from "../utils/outputHandler";
|
||||
import { APPLICATION_ID } from "../env";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
|
|
@ -87,10 +87,8 @@ export class DiscordBot {
|
|||
|
||||
public registerEvents() {
|
||||
try {
|
||||
for (let index = 0; index < eventMap.length; index++) {
|
||||
const event = eventMap[index];
|
||||
if (!event) continue;
|
||||
this.client.on(event.event, event.callback);
|
||||
for (const event of eventMap) {
|
||||
this.client.on(event.event as any, event.callback as any);
|
||||
}
|
||||
} catch (err) {
|
||||
OutputHandler.errorLog("[Event Register Error]", err);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,12 @@
|
|||
import { readdir } from "fs/promises";
|
||||
import { join } from "path";
|
||||
|
||||
export async function requireDirectory<T>(directory: string): Promise<T[]> {
|
||||
const requireFiles = (await readdir(directory)).filter((file) =>
|
||||
file.endsWith(".js"),
|
||||
export async function requireDirectory<T>(
|
||||
directory: string,
|
||||
ignores: string[] = [],
|
||||
): Promise<T[]> {
|
||||
const requireFiles = (await readdir(directory)).filter(
|
||||
(file) => file.endsWith(".js") && !ignores.includes(file),
|
||||
);
|
||||
return await Promise.all(
|
||||
requireFiles.map(
|
||||
|
|
|
|||
Loading…
Reference in a new issue