Skip to content

Commit

Permalink
V1.0.5 Windows Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
RileCraft committed Feb 2, 2024
1 parent 82b57f3 commit ebbfc98
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 16 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<img src="https://media.discordapp.net/attachments/774290264764055582/1093484780525469757/A_banner_for_a_discord_bots_template_made_using_discord.js.png" height="200" width="400"><br>
<img src="https://img.shields.io/badge/version-1.0.4-05122A?style=for-the-badge">
<img src="https://img.shields.io/badge/version-1.0.5-05122A?style=for-the-badge">
<a href="https://discord.gg/VStdRr8nP2"><img src="https://img.shields.io/badge/discord-invite-5865f2?style=for-the-badge&logo=discord&logoColor=white"></a>
<img src="https://img.shields.io/github/issues/RileCraft/DiscordBot-Template-ts.svg?style=for-the-badge">
<img src="https://img.shields.io/github/forks/RileCraft/DiscordBot-Template-ts.svg?style=for-the-badge">
Expand All @@ -12,7 +12,9 @@
The Discord Bot Template provides a solid foundation for creating feature-rich Discord bots using Discord.js. It includes various managers for handling message commands, buttons, select menus, slash commands, context menus, and modal forms. The template offers customization options, colorful logging, and a simple code structure.

## Changelog
### IMPORTANT UPDATE 1.0.5

- **Fixed Windows Support and SlashCommands & ContextMenus not Registering.**
- Latest Discord.js adaptation.
- Following JavaScript Naming Convention.
- Removed `node-recursive-directory` dependency.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discordbot-template-ts",
"version": "1.0.4",
"version": "1.0.5",
"description": "TypeScript version of DiscordBot-Template",
"main": "./src/bot.ts",
"type": "module",
Expand Down
3 changes: 1 addition & 2 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Client, DiscordClient, GatewayIntentBits, Partials } from "discord.js";
import { BOT_TOKEN } from "./config.js";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { ButtonCommand, ClientEvent, ContextMenu, MessageCommand, ModalForm, SelectMenu, SlashCommand } from "./types.js";
import { ButtonManager } from "./structures/managers/buttonCommands.js";
Expand All @@ -10,7 +9,7 @@ import { ModalManager } from "./structures/managers/modalForms.js";
import { SelectMenuManager } from "./structures/managers/selectMenus.js";
import { SlashManager } from "./structures/managers/slashCommands.js";

const __dirname: string = dirname(fileURLToPath(import.meta.url));
const __dirname: string = dirname(import.meta.url);
export const rootPath = __dirname;

(async(): Promise<void> => {
Expand Down
5 changes: 3 additions & 2 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ActivityType, DiscordClient } from "discord.js";
import { rootPath } from "../bot.js";
import { pathToFileURL } from "node:url";
import { ClientEvent, ContextMenu, SlashCommand } from "../types.js";
import { fileReader } from "../utils/fileReader.js";
import { t } from "tasai";
Expand All @@ -14,15 +15,15 @@ export const Event: ClientEvent = {

let allSlashCommands: Array<string> = fileReader(`${rootPath}/interactions/slashCommands`);
allSlashCommands = await allSlashCommands.reduce(async(array: any, slash: string): Promise<Array<string>> => {
const command: SlashCommand | undefined = (await import(slash))?.Slash;
const command: SlashCommand | undefined = (await import(pathToFileURL(slash).href))?.Slash;

if (command?.ignore || !command?.name) return array;
else return (await array).concat(slash)
}, []);

let allContextMenus: Array<string> = fileReader(`${rootPath}/interactions/contextMenus`);
allContextMenus = await allContextMenus.reduce(async(array: any, context: string): Promise<Array<string>> => {
const command: ContextMenu | undefined = (await import(context))?.Context;
const command: ContextMenu | undefined = (await import(pathToFileURL(context).href))?.Context;

if (command?.ignore || !command?.name || !command?.type) return array;
else return (await array).concat(context)
Expand Down
3 changes: 2 additions & 1 deletion src/structures/managers/buttonCommands.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DiscordClient } from "discord.js";
import { ButtonCommand } from "../../types.js";
import { pathToFileURL } from "node:url";
import { fileReader } from "../../utils/fileReader.js";

export const ButtonManager = async(client: DiscordClient, rootPath: string): Promise<void> => {
const buttonCommandFiles: Array<string> = fileReader(`${rootPath}/interactions/buttons`);
if (!buttonCommandFiles.length) return;

for (const buttonCommandFile of buttonCommandFiles) {
const buttonCommand: ButtonCommand = (await import(buttonCommandFile))?.Button;
const buttonCommand: ButtonCommand = (await import(pathToFileURL(buttonCommandFile).href))?.Button;
if (!buttonCommand) continue;

if (!buttonCommand?.ignore && buttonCommand?.name) client.buttonCommands?.set(buttonCommand?.name, buttonCommand);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/managers/events.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DiscordClient } from "discord.js";
import { ClientEvent } from "../../types.js";
import { pathToFileURL } from "node:url";
import { fileReader } from "../../utils/fileReader.js";

export const EventManager = async(client: DiscordClient, rootPath: string): Promise<void> => {
const eventFiles: Array<string> = fileReader(`${rootPath}/events`);
if (!eventFiles.length) return;

for (const event of eventFiles) {
const clientEvent: ClientEvent = (await import(event))?.Event;
const clientEvent: ClientEvent = (await import(pathToFileURL(event).href))?.Event;
if (clientEvent.ignore) continue;

client.events?.set(clientEvent.name, clientEvent);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/managers/messageCommands.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { MessageCommand } from "../../types.js";
import { DiscordClient } from "discord.js";
import { pathToFileURL } from "node:url";
import { fileReader } from "../../utils/fileReader.js";

export const MessageCMDManager = async(client: DiscordClient, rootPath: string): Promise<void> => {
const messageCommandsFiles: Array<string> = fileReader(`${rootPath}/messageCommands`);
if (!messageCommandsFiles.length) return;

for (const messageCommandFile of messageCommandsFiles) {
const messageCommand: MessageCommand = (await import(messageCommandFile))?.MsgCommand;
const messageCommand: MessageCommand = (await import(pathToFileURL(messageCommandFile).href))?.MsgCommand;
if (!messageCommand) continue;

if (!messageCommand.ignore && messageCommand.name) client.messageCommands?.set(messageCommand.name.toLowerCase(), messageCommand);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/managers/modalForms.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { DiscordClient } from "discord.js";
import { ModalForm } from "../../types.js";
import { pathToFileURL } from "node:url";
import { fileReader } from "../../utils/fileReader.js";

export const ModalManager = async(client: DiscordClient, rootPath: string): Promise<void> => {
const modalFormFiles: Array<string> = fileReader(`${rootPath}/interactions/modalForms`);
if (!modalFormFiles.length) return;

for (const modalFormFile of modalFormFiles) {
const modalForm: ModalForm = (await import(modalFormFile))?.Modal;
const modalForm: ModalForm = (await import(pathToFileURL(modalFormFile).href))?.Modal;
if (!modalForm) continue;

if (!modalForm.ignore && modalForm.name) client.modalForms?.set(modalForm.name, modalForm);
Expand Down
3 changes: 2 additions & 1 deletion src/structures/managers/selectMenus.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { SelectMenu } from "../../types.js";
import { DiscordClient } from "discord.js";
import { pathToFileURL } from "node:url";
import { fileReader } from "../../utils/fileReader.js";

export const SelectMenuManager = async(client: DiscordClient, rootPath: string): Promise<void> => {
const selectMenuFiles: Array<string> = fileReader(`${rootPath}/interactions/selectMenus`);
if (!selectMenuFiles.length) return;

for (const selectMenuFile of selectMenuFiles) {
const selectMenu: SelectMenu = (await import(selectMenuFile))?.Menu;
const selectMenu: SelectMenu = (await import(pathToFileURL(selectMenuFile).href))?.Menu;
if (!selectMenu) continue;

if (!selectMenu.ignore && selectMenu.name) client.selectMenus?.set(selectMenu.name, selectMenu);
Expand Down
5 changes: 3 additions & 2 deletions src/structures/managers/slashCommands.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApplicationCommandType, DiscordClient, REST, Routes } from "discord.js";
import { ContextMenu, SlashCommand, SlashCommandOptions } from "../../types.js";
import { fileReader } from "../../utils/fileReader.js";
import { pathToFileURL } from "node:url";

export const SlashManager = async (client: DiscordClient, rootPath: string): Promise<void> => {
const allSlashCommandsFiles = fileReader(`${rootPath}/interactions/slashCommands`);
Expand Down Expand Up @@ -30,7 +31,7 @@ export const SlashManager = async (client: DiscordClient, rootPath: string): Pro

if (allSlashCommandsFiles.length > 0) {
for (const slashCommandFile of allSlashCommandsFiles) {
const slashCommand: SlashCommand | undefined = (await import(slashCommandFile))?.Slash;
const slashCommand: SlashCommand | undefined = (await import(pathToFileURL(slashCommandFile).href))?.Slash;
if (!slashCommand) continue;

if (slashCommand?.ignore || !slashCommand?.name || !slashCommand.description) continue;
Expand Down Expand Up @@ -60,7 +61,7 @@ export const SlashManager = async (client: DiscordClient, rootPath: string): Pro

if (allContextMenusFiles.length > 0) {
for (const contextMenuFile of allContextMenusFiles) {
const contextMenu: ContextMenu | undefined = (await import(contextMenuFile))?.Context;
const contextMenu: ContextMenu | undefined = (await import(pathToFileURL(contextMenuFile).href))?.Context;
if (!contextMenu) continue;

if (contextMenu?.ignore || !contextMenu?.name || !contextMenu?.type) continue;
Expand Down
8 changes: 5 additions & 3 deletions src/utils/fileReader.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { existsSync, readdirSync, statSync } from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { join, extname } from "path";

export const fileReader = (dir: string): Array<string> => {
dir = fileURLToPath(dir);
if (!existsSync(dir)) return [];
const files: Array<string> = [];
const directoryData = readdirSync(dir);

for (const file of directoryData) {
const filePath = path.join(dir, file);
const filePath = join(dir, file);
const stats = statSync(filePath);

if (stats.isFile() && path.extname(filePath) === ".js") files.push(filePath);
if (stats.isFile() && extname(filePath) === ".js") files.push(filePath);
else if (stats.isDirectory()) files.push(...fileReader(filePath));
};

Expand Down

0 comments on commit ebbfc98

Please sign in to comment.