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/
|
target/
|
||||||
tsconfig.tsbuildinfo
|
tsconfig.tsbuildinfo
|
||||||
|
voices
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@ import {
|
||||||
type SlashCommandOptionsOnlyBuilder,
|
type SlashCommandOptionsOnlyBuilder,
|
||||||
SlashCommandSubcommandBuilder,
|
SlashCommandSubcommandBuilder,
|
||||||
} from "discord.js";
|
} from "discord.js";
|
||||||
import { join } from "node:path";
|
|
||||||
import { requireDirectory } from "../utils/requireDirectory";
|
|
||||||
import { AdminUsers } from "./admin";
|
import { AdminUsers } from "./admin";
|
||||||
|
|
||||||
|
export const commandExecuteNameHashMap = [];
|
||||||
|
|
||||||
export type DiscordCommandData =
|
export type DiscordCommandData =
|
||||||
| SlashCommandBuilder
|
| SlashCommandBuilder
|
||||||
| SlashCommandOptionsOnlyBuilder
|
| SlashCommandOptionsOnlyBuilder
|
||||||
|
|
@ -45,12 +45,3 @@ export function defineCommand(
|
||||||
execute: execute,
|
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 { type ClientEvents } from "discord.js";
|
||||||
import { join } from "node:path";
|
|
||||||
import { requireDirectory } from "../utils/requireDirectory";
|
|
||||||
|
|
||||||
export interface DiscordEvent<Event extends keyof ClientEvents> {
|
export interface DiscordEvent<Event extends keyof ClientEvents> {
|
||||||
event: Event;
|
event: Event;
|
||||||
|
|
@ -16,7 +14,3 @@ export function defineEvent<Event extends keyof ClientEvents>(
|
||||||
callback: callback,
|
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";
|
import { defineEvent } from "../event";
|
||||||
|
|
||||||
export default defineEvent("interactionCreate", async (interaction) => {
|
export default defineEvent("interactionCreate", async (interaction) => {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { Client, Events, GatewayIntentBits, REST, Routes } from "discord.js";
|
import { Client, Events, GatewayIntentBits, REST, Routes } from "discord.js";
|
||||||
import { commandExecuteNameHashMap, defineCommands } from "./command";
|
import { commandExecuteNameHashMap, defineCommands } from "./commands";
|
||||||
import { eventMap } from "./event";
|
import { eventMap } from "./events";
|
||||||
import { OutputHandler } from "../utils/outputHandler";
|
import { OutputHandler } from "../utils/outputHandler";
|
||||||
import { APPLICATION_ID } from "../env";
|
import { APPLICATION_ID } from "../env";
|
||||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||||
|
|
@ -87,10 +87,8 @@ export class DiscordBot {
|
||||||
|
|
||||||
public registerEvents() {
|
public registerEvents() {
|
||||||
try {
|
try {
|
||||||
for (let index = 0; index < eventMap.length; index++) {
|
for (const event of eventMap) {
|
||||||
const event = eventMap[index];
|
this.client.on(event.event as any, event.callback as any);
|
||||||
if (!event) continue;
|
|
||||||
this.client.on(event.event, event.callback);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
OutputHandler.errorLog("[Event Register Error]", err);
|
OutputHandler.errorLog("[Event Register Error]", err);
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
import { readdir } from "fs/promises";
|
import { readdir } from "fs/promises";
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
|
|
||||||
export async function requireDirectory<T>(directory: string): Promise<T[]> {
|
export async function requireDirectory<T>(
|
||||||
const requireFiles = (await readdir(directory)).filter((file) =>
|
directory: string,
|
||||||
file.endsWith(".js"),
|
ignores: string[] = [],
|
||||||
|
): Promise<T[]> {
|
||||||
|
const requireFiles = (await readdir(directory)).filter(
|
||||||
|
(file) => file.endsWith(".js") && !ignores.includes(file),
|
||||||
);
|
);
|
||||||
return await Promise.all(
|
return await Promise.all(
|
||||||
requireFiles.map(
|
requireFiles.map(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue