Comparing version 2.5.0 to 2.5.1-beta
import { CommandObject } from "./handler/commandHandler"; | ||
/** | ||
* Get't All the command objects. This is the same function inhereted from the CoreHandler used by the CommandHandler | ||
* @param path Path to the commands | ||
* @param exceptions Commands to not get | ||
* Get all the command objects. This is the same function inherited from the CoreHandler used by the CommandHandler. | ||
* @param path Path to the commands. | ||
* @param exceptions Commands to not get. | ||
* @returns CommandObject[] | ||
*/ | ||
export declare function getLocalCommands(path: string, exceptions?: string[]): CommandObject[]; | ||
/** | ||
* Add deprecated string. | ||
* @param isOld | ||
*/ | ||
export declare function deprecated(txt: string, isOld: boolean): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getLocalCommands = void 0; | ||
exports.getLocalCommands = getLocalCommands; | ||
exports.deprecated = deprecated; | ||
const fs = require("fs"); | ||
const path2 = require("path"); | ||
const clc = require("cli-color"); | ||
/** | ||
* Get't All the command objects. This is the same function inhereted from the CoreHandler used by the CommandHandler | ||
* @param path Path to the commands | ||
* @param exceptions Commands to not get | ||
* Get all the command objects. This is the same function inherited from the CoreHandler used by the CommandHandler. | ||
* @param path Path to the commands. | ||
* @param exceptions Commands to not get. | ||
* @returns CommandObject[] | ||
*/ | ||
function getLocalCommands(path, exceptions) { | ||
exceptions = exceptions !== undefined ? exceptions : []; | ||
let localCommands = []; | ||
function getLocalCommands(path, exceptions = []) { | ||
const scanDirectory = (directory) => { | ||
const items = fs.readdirSync(directory); | ||
let arr = []; | ||
for (const item of items) { | ||
return items.flatMap((item) => { | ||
const itemPath = path2.join(directory, item); | ||
const isDirectory = fs.statSync(itemPath).isDirectory(); | ||
if (isDirectory) { | ||
arr = arr.concat(scanDirectory(itemPath)); | ||
return scanDirectory(itemPath); | ||
} | ||
@@ -30,14 +29,21 @@ else if (item.endsWith(".js")) { | ||
exceptions.includes(commandObject.name)) { | ||
continue; | ||
return []; | ||
} | ||
commandObject.filePath = itemPath; | ||
arr.push(commandObject); | ||
return [commandObject]; | ||
} | ||
} | ||
return arr; | ||
return []; | ||
}); | ||
}; | ||
localCommands = scanDirectory(path); | ||
return localCommands; | ||
return scanDirectory(path); | ||
} | ||
exports.getLocalCommands = getLocalCommands; | ||
/** | ||
* Add deprecated string. | ||
* @param isOld | ||
*/ | ||
function deprecated(txt, isOld) { | ||
return isOld | ||
? txt + clc.bold.bgRedBright.white(" (Command uses deprecated syntax!)") | ||
: txt; | ||
} | ||
//# sourceMappingURL=funcs.js.map |
@@ -49,2 +49,6 @@ import { Client, PermissionFlags, SlashCommandBuilder, ChatInputCommandInteraction, RESTPostAPIApplicationCommandsJSONBody } from "discord.js"; | ||
deleted: boolean; | ||
/** | ||
* | ||
*/ | ||
protected isOld: boolean; | ||
constructor(commandObject: { | ||
@@ -51,0 +55,0 @@ /** |
@@ -21,2 +21,6 @@ "use strict"; | ||
this.botPermissions = []; | ||
/** | ||
* | ||
*/ | ||
this.isOld = false; | ||
this.data = obj.data.toJSON(); | ||
@@ -23,0 +27,0 @@ this.name = obj.data.name; |
@@ -20,2 +20,3 @@ import { CoreHandler, Option } from "./coreHandler"; | ||
filePath?: string; | ||
isOld?: boolean; | ||
permissionsRequired?: PermissionFlags[]; | ||
@@ -31,3 +32,2 @@ botPermissions?: PermissionFlags[]; | ||
commandPath: string; | ||
private emitErr; | ||
options: LoaderOptions; | ||
@@ -44,7 +44,2 @@ readerOptions: ReaderOptions; | ||
/** | ||
* Set whether the command handler should throw or emit errors. Defaults to false. | ||
* @param bool Boolean value | ||
*/ | ||
emitErrors(bool: boolean): void; | ||
/** | ||
* Register Slash Commands | ||
@@ -59,4 +54,4 @@ * @param logAll Log when loading a command and no changes are made | ||
*/ | ||
handleCommands(...middleWare: ((commandObject: CommandObject, interaction?: ChatInputCommandInteraction) => number)[]): Promise<void>; | ||
handleCommands(...middleWare: ((commandObject: CommandObject, interaction?: ChatInputCommandInteraction) => number | Promise<number>)[]): Promise<void>; | ||
} | ||
export {}; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const errs = require("./Errors"); | ||
const funcs_1 = require("../funcs"); | ||
/** | ||
@@ -22,3 +23,2 @@ * @class | ||
super(client); | ||
this.emitErr = false; | ||
this.options = { | ||
@@ -55,9 +55,2 @@ loadedNoChanges: "NAME was loaded. No changes were made.", | ||
/** | ||
* Set whether the command handler should throw or emit errors. Defaults to false. | ||
* @param bool Boolean value | ||
*/ | ||
emitErrors(bool) { | ||
this.emitErr = bool == true ? true : false; | ||
} | ||
/** | ||
* Register Slash Commands | ||
@@ -79,3 +72,3 @@ * @param logAll Log when loading a command and no changes are made | ||
} | ||
let { name, description, options, filePath, data } = localCommand; | ||
let { name, description, options, filePath, isOld, data } = localCommand; | ||
try { | ||
@@ -103,3 +96,3 @@ const existingCommand = await applicationCommands.cache.find((cmd) => cmd.name === name); | ||
noChanges = false; | ||
console.log(this.options.edited.replace("NAME", name)); | ||
console.log((0, funcs_1.deprecated)(this.options.edited.replace("NAME", name), isOld)); | ||
} | ||
@@ -111,3 +104,3 @@ } | ||
noChanges = false; | ||
console.log(this.options.skipped.replace("NAME", name)); | ||
console.log((0, funcs_1.deprecated)(this.options.skipped.replace("NAME", name), isOld)); | ||
continue; | ||
@@ -124,3 +117,3 @@ } | ||
noChanges = false; | ||
console.log(this.options.loaded.replace("NAME", name)); | ||
console.log((0, funcs_1.deprecated)(this.options.loaded.replace("NAME", name), isOld)); | ||
} | ||
@@ -132,3 +125,3 @@ } | ||
if (logAll && noChanges == true) { | ||
console.log(this.options.loadedNoChanges.replace("NAME", name)); | ||
console.log((0, funcs_1.deprecated)(this.options.loadedNoChanges.replace("NAME", name), isOld)); | ||
} | ||
@@ -144,8 +137,3 @@ } | ||
: msg; | ||
if (this.emitErr == true) { | ||
this.emit("error", Lerr); | ||
} | ||
else { | ||
throw new Error(Lerr); | ||
} | ||
throw new Error(Lerr); | ||
} | ||
@@ -212,8 +200,3 @@ } | ||
console.error(error); | ||
if (this.emitErr) { | ||
this.emit("error", err.message); | ||
} | ||
else { | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -220,0 +203,0 @@ }); |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
import { Client, ApplicationCommandManager, GuildApplicationCommandManager, ApplicationCommandOptionType, ApplicationCommand } from "discord.js"; | ||
@@ -6,3 +5,2 @@ import { CommandObject } from "./commandHandler"; | ||
import { Interactions } from "./builders/builders"; | ||
import { EventEmitter } from "events"; | ||
export interface LoaderOptions { | ||
@@ -42,3 +40,3 @@ /** | ||
export declare function extractAllInteractions(interactionsObject: Interactions, filePath: string): InteractionObject[]; | ||
export declare class CoreHandler extends EventEmitter { | ||
export declare class CoreHandler { | ||
client: Client; | ||
@@ -45,0 +43,0 @@ constructor(client: Client); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CoreHandler = exports.extractAllInteractions = void 0; | ||
exports.CoreHandler = void 0; | ||
exports.extractAllInteractions = extractAllInteractions; | ||
const path2 = require("path"); | ||
const fs = require("fs"); | ||
const events_1 = require("events"); | ||
const builders_1 = require("./builders/builders"); | ||
function change(cI) { | ||
@@ -24,7 +25,5 @@ return { | ||
} | ||
exports.extractAllInteractions = extractAllInteractions; | ||
const isEmpty = (obj) => Object.keys(obj).length === 0; | ||
class CoreHandler extends events_1.EventEmitter { | ||
class CoreHandler { | ||
constructor(client) { | ||
super(); | ||
this.client = client; | ||
@@ -89,2 +88,6 @@ } | ||
} | ||
// It's a valid command, now proceed with checks. | ||
commandObject.isOld = !(commandObject instanceof builders_1.SlashCommandManager) | ||
? true | ||
: false; | ||
commandObject.filePath = itemPath; | ||
@@ -91,0 +94,0 @@ return [commandObject]; |
@@ -34,3 +34,2 @@ import { CoreHandler } from "./coreHandler"; | ||
}; | ||
emitErrors: boolean; | ||
options: LoaderOptions; | ||
@@ -42,5 +41,4 @@ /** | ||
* @param loaderOptions Loader options (for context menus) | ||
* @param emitErrors Log any errors that occur. True will emit errors, false will throw errors and null will not log any errors. | ||
*/ | ||
constructor(client: Client, path: string, loaderOptions?: LoaderOptions, emitErrors?: boolean); | ||
constructor(client: Client, path: string, loaderOptions?: LoaderOptions); | ||
private sortInteractionObjects; | ||
@@ -47,0 +45,0 @@ /** |
@@ -17,7 +17,5 @@ "use strict"; | ||
* @param loaderOptions Loader options (for context menus) | ||
* @param emitErrors Log any errors that occur. True will emit errors, false will throw errors and null will not log any errors. | ||
*/ | ||
constructor(client, path, loaderOptions, emitErrors) { | ||
constructor(client, path, loaderOptions) { | ||
super(client); | ||
this.emitErrors = undefined; | ||
this.options = { | ||
@@ -33,3 +31,2 @@ loadedNoChanges: "NAME was loaded. No changes were made.", | ||
this.interactions = this.sortInteractionObjects(interactions); | ||
this.emitErrors = emitErrors; | ||
this.options = { | ||
@@ -137,8 +134,3 @@ loadedNoChanges: clc.magenta.bold((loaderOptions === null || loaderOptions === void 0 ? void 0 : loaderOptions.loadedNoChanges) || this.options.loadedNoChanges), | ||
let err = new errs.ButtonError("Button $NAME$ failed with the error:\n\n" + error, buttonObj.filePath, interaction.customId); | ||
if (this.emitErrors) { | ||
this.emit("error", err.message); | ||
} | ||
else if (this.emitErrors == false) { | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -191,8 +183,3 @@ }); | ||
let err = new errs.ButtonError("Select Menu $NAME$ failed with the error:\n\n" + error, selectObj.filePath, interaction.customId); | ||
if (this.emitErrors) { | ||
this.emit("error", err.message); | ||
} | ||
else if (this.emitErrors == false) { | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -223,8 +210,3 @@ }); | ||
error, contextObj.filePath, interaction.commandName); | ||
if (this.emitErrors) { | ||
this.emit("error", err.message); | ||
} | ||
else if (this.emitErrors == false) { | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -254,8 +236,3 @@ }); | ||
let err = new errs.ModalError("Modal $NAME$ failed with the error:\n\n" + error, modalObj.filePath, interaction.customId); | ||
if (this.emitErrors) { | ||
this.emit("error", err.message); | ||
} | ||
else if (this.emitErrors == false) { | ||
throw err; | ||
} | ||
throw err; | ||
} | ||
@@ -340,8 +317,3 @@ }); | ||
: msg; | ||
if (this.emitErrors) { | ||
this.emit("error", Lmsg); | ||
} | ||
else if (this.emitErrors == false) { | ||
throw new Error(Lmsg); | ||
} | ||
throw new Error(Lmsg); | ||
} | ||
@@ -348,0 +320,0 @@ } |
@@ -12,3 +12,3 @@ import { CoreHandler } from "./coreHandler"; | ||
*/ | ||
constructor(client: Client, ...functions: ((client?: Client) => void)[]); | ||
constructor(client: Client, ...functions: ((client?: Client) => Promise<void> | void)[]); | ||
/** | ||
@@ -15,0 +15,0 @@ * Set whether the ready handler should throw or emit errors. Defaults to false. |
@@ -41,8 +41,3 @@ "use strict"; | ||
console.error(error); | ||
if (this.emitErr) { | ||
this.emit("error", msg); | ||
} | ||
else { | ||
throw new Error(msg); | ||
} | ||
throw new Error(msg); | ||
} | ||
@@ -49,0 +44,0 @@ } |
{ | ||
"name": "ic4d", | ||
"version": "2.5.0", | ||
"version": "2.5.1-beta", | ||
"description": "Discord.js Interaction and Command handler 4 Dummies", | ||
@@ -43,5 +43,5 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/node": "^20.14.2", | ||
"@types/node": "^20.14.9", | ||
"discord.js": "^14.15.3", | ||
"typescript": "^5.4.5" | ||
"typescript": "^5.5.2" | ||
}, | ||
@@ -48,0 +48,0 @@ "dependencies": { |
101
README.md
@@ -7,5 +7,11 @@ # ic4d | ||
With npm | ||
``` | ||
npm i ic4d | ||
sudo npm i install ic4d | ||
``` | ||
With yarn | ||
``` | ||
yarn add ic4d | ||
@@ -41,2 +47,3 @@ ``` | ||
- - [ContextMenuBuilder](#contextmenubuilder) | ||
- [Common Problems](#common-problems) | ||
- [Credit](#credits) | ||
@@ -136,16 +143,2 @@ - [Links](#links) | ||
### `emitErrors()` | ||
Set whether the ready handler should throw or emit errors. Defaults to false. | ||
```js | ||
const ready = new ReadyHandler(client, ...); | ||
ready.emitErrors(true); | ||
// Listen for the error | ||
ready.on("error", (msg) => { | ||
// do something with the error message | ||
}) | ||
``` | ||
# CommandHandler | ||
@@ -155,16 +148,2 @@ | ||
## Pre-Read | ||
Before you use the CommandHandler class, make sure you follow the [command layout](#command-object) or else the CommandHandler may not work properly. But these below are the minimum properties needed. | ||
```js | ||
module.exports = { | ||
name: "command-name", | ||
description: "Command Description that looks cool", | ||
callback: async (client, interaction) => { | ||
interaction.reply("Wow!"); | ||
}, | ||
}; | ||
``` | ||
## Constructor | ||
@@ -283,10 +262,17 @@ | ||
```js | ||
module.exports = { | ||
name: "rob", | ||
description: "Rob users", | ||
canBeServerDisabled: true, | ||
callback: async (client, interaction) => { | ||
const {SlashCommandManager} = require("ic4d"); | ||
const {SlashCommandBuilder} = require("discord.js"); | ||
const rob = new SlashCommandManager({ | ||
data: new SlashCommandBuilder() | ||
.setName("rob") | ||
.setDescription("Rob users") | ||
execute: (interaction, client) => { | ||
interaction.reply("bang bang!"); | ||
}, | ||
}; | ||
}); | ||
rob.canBeServerDisabled = true; | ||
module.exports = rob | ||
``` | ||
@@ -346,3 +332,2 @@ | ||
- `loaderOptions`**(optional)**: Context Menu [Loader Options](#loaderoptions) | ||
- `emitErrors`**(optional)**: Log any errors that occur. True will emit errors, false will throw errors and null will not log any errors. | ||
@@ -682,2 +667,28 @@ ```js | ||
# Common Problems | ||
1. _Files in the `commands` directory trying to be read as slash commmands by the `CommandHandler` class._ | ||
- Example: This function is in the `commands` direcotory as it is used by multiple commands, but is not a commands itself. | ||
```js | ||
const function a(userBalance) { | ||
return userBalance > 0 ? true : false; | ||
} | ||
module.exports = a; | ||
``` | ||
- The Command Reader will try to read it but error as it is not a command it can read, to avoid this, make sure you export the `isCommand` (Set to false) property with the function. | ||
```js | ||
const function a(userBalance) { | ||
return userBalance > 0 ? true : false; | ||
} | ||
module.exports = {a, isCommand = false}; | ||
``` | ||
- Usually, the reader should skip over anything it can read, but if needed, this will immediately make it skip. | ||
# Credits | ||
@@ -704,2 +715,5 @@ | ||
> [!IMPORTANT] | ||
> This can still be used and will work as intended (if the following is used correctly) but it's encouraged to use the [SlashCommandManager](#slashcommandmanager) class instead. | ||
This package requires your command object to be layed out specifically, (If you're coming from a normal discord.js handler that uses the execute and data properties, skip to [Tradtional discord.js object](#tradtional-discordjs-object)) | ||
@@ -815,2 +829,5 @@ | ||
> [!IMPORTANT] | ||
> This can still be used and will work as intended (if the following is used correctly) but it's encouraged to use the [InteractionBuilder](#interactionbuilder) class instead. | ||
Package also requires that wherever you store your interaction object (buttons, select menus, context menus etc), they have thesse minimum requirements: | ||
@@ -881,2 +898,5 @@ | ||
> [!IMPORTANT] | ||
> This is deprecated, use [ContextMenuBuilder](#contextmenubuilder) class instead. | ||
This works a little differently to ones above, that's why it has it's own category lol. | ||
@@ -918,2 +938,5 @@ | ||
> [!IMPORTANT] | ||
> This is deprecated, use [addInteractions()](#addinteractions) method in the [SlashCommandManager](#slashcommandmanager) class instead. | ||
If you do not like having random buttons everywhere in different files, don't worry the following classess are here to help you! | ||
@@ -967,2 +990,5 @@ | ||
> [!IMPORTANT] | ||
> This is deprecated, use [InteractionBuilder](#interactionbuilder) class instead. | ||
### Constructor | ||
@@ -995,2 +1021,5 @@ | ||
> [!IMPORTANT] | ||
> This is deprecated, use [SlashCommandManager](#slashcommandmanager) class instead. | ||
Represents a slash command object. | ||
@@ -997,0 +1026,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1037
130459
1708
1