djs-commander
Advanced tools
Comparing version 0.0.32 to 0.0.33
@@ -7,3 +7,4 @@ const { getFolderPaths, getFilePaths } = require('./utils/getPaths'); | ||
constructor({ client, commandsPath, eventsPath, validationsPath, testServer }) { | ||
if (!client) throw new Error('Property "client" is required when instantiating CommandHandler.'); | ||
if (!client) | ||
throw new Error('Property "client" is required when instantiating CommandHandler.'); | ||
@@ -15,3 +16,4 @@ this._client = client; | ||
this._testServer = testServer; | ||
this._commands = []; | ||
this._commands = []; // includes all the properties and methods exported from command file | ||
this._commandsToRegister = []; // doesn't include the `deleted` or `run` properties | ||
this._validationFuncs = []; | ||
@@ -40,3 +42,12 @@ | ||
_commandsInit() { | ||
this._commands = buildCommandTree(this._commandsPath); | ||
let commands = buildCommandTree(this._commandsPath); | ||
this._commands = commands; | ||
let commandsToRegister = JSON.parse(JSON.stringify(commands)); | ||
commandsToRegister.forEach((cmd) => { | ||
delete cmd['deleted']; | ||
delete cmd['run']; | ||
}); | ||
this._commandsToRegister = commandsToRegister; | ||
} | ||
@@ -47,3 +58,3 @@ | ||
client: this._client, | ||
commands: this._commands, | ||
commands: this._commandsToRegister, | ||
testServer: this._testServer, | ||
@@ -64,3 +75,3 @@ }); | ||
const eventFunc = require(eventFuncPath); | ||
await eventFunc(arg, this._client); | ||
await eventFunc(arg, this._client, this); | ||
} | ||
@@ -96,4 +107,4 @@ }); | ||
for (const validationFunc of this._validationFuncs) { | ||
const result = await validationFunc(interaction, command); | ||
if (result) { | ||
const cantRunCommand = await validationFunc(interaction, command, this); | ||
if (cantRunCommand) { | ||
canRun = false; | ||
@@ -104,5 +115,15 @@ break; | ||
if (canRun) await command.run(interaction, this._client); | ||
if (canRun) { | ||
await command.run({ | ||
interaction, | ||
client: this._client, | ||
handler: this, | ||
}); | ||
} | ||
} else { | ||
await command.run(interaction, this._client); | ||
await command.run({ | ||
interaction, | ||
client: this._client, | ||
handler: this, | ||
}); | ||
} | ||
@@ -114,4 +135,8 @@ } else { | ||
} | ||
get commands() { | ||
return this._commands; | ||
} | ||
} | ||
export { CommandHandler }; |
@@ -199,2 +199,3 @@ var __defProp = Object.defineProperty; | ||
this._commands = []; | ||
this._commandsToRegister = []; | ||
this._validationFuncs = []; | ||
@@ -219,3 +220,10 @@ if (this._validationsPath && !commandsPath) { | ||
_commandsInit() { | ||
this._commands = buildCommandTree2(this._commandsPath); | ||
let commands = buildCommandTree2(this._commandsPath); | ||
this._commands = commands; | ||
let commandsToRegister = JSON.parse(JSON.stringify(commands)); | ||
commandsToRegister.forEach((cmd) => { | ||
delete cmd["deleted"]; | ||
delete cmd["run"]; | ||
}); | ||
this._commandsToRegister = commandsToRegister; | ||
} | ||
@@ -225,3 +233,3 @@ _registerSlashCommands() { | ||
client: this._client, | ||
commands: this._commands, | ||
commands: this._commandsToRegister, | ||
testServer: this._testServer | ||
@@ -239,3 +247,3 @@ }); | ||
const eventFunc = require(eventFuncPath); | ||
await eventFunc(arg, this._client); | ||
await eventFunc(arg, this._client, this); | ||
} | ||
@@ -265,4 +273,4 @@ }); | ||
for (const validationFunc of this._validationFuncs) { | ||
const result = await validationFunc(interaction, command); | ||
if (result) { | ||
const cantRunCommand = await validationFunc(interaction, command, this); | ||
if (cantRunCommand) { | ||
canRun = false; | ||
@@ -272,6 +280,15 @@ break; | ||
} | ||
if (canRun) | ||
await command.run(interaction, this._client); | ||
if (canRun) { | ||
await command.run({ | ||
interaction, | ||
client: this._client, | ||
handler: this | ||
}); | ||
} | ||
} else { | ||
await command.run(interaction, this._client); | ||
await command.run({ | ||
interaction, | ||
client: this._client, | ||
handler: this | ||
}); | ||
} | ||
@@ -283,2 +300,5 @@ } else { | ||
} | ||
get commands() { | ||
return this._commands; | ||
} | ||
}; | ||
@@ -285,0 +305,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "djs-commander", | ||
"version": "0.0.32", | ||
"version": "0.0.33", | ||
"description": "A command and event handler that works seemlessly with Discord.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
@@ -11,2 +11,4 @@ # DJS-Commander: A Library for Discord.js Projects | ||
For npm: | ||
```bash | ||
@@ -16,2 +18,8 @@ npm install djs-commander | ||
For yarn: | ||
```yarn | ||
yarn add djs-commander | ||
``` | ||
## Usage | ||
@@ -34,3 +42,3 @@ | ||
validationsPath: path.join(__dirname, 'validations'), // Only works if commandsPath is provided | ||
testServer: 'TEST_SERVER_ID', // To register guild-based commands | ||
testServer: 'TEST_SERVER_ID', // To register guild-based commands (if not provided commands will be registered globally) | ||
}); | ||
@@ -52,3 +60,3 @@ | ||
└── category/ | ||
└── command3.js | ||
├── command3.js | ||
└── commands4.js | ||
@@ -66,10 +74,14 @@ ``` | ||
run: (interaction, client) => { | ||
run: ({ interaction, client, handler }) => { | ||
interaction.reply(`Pong! ${client.ws.ping}ms`); | ||
}, | ||
// deleted: true, // Deletes the command from Discord | ||
// deleted: true, // Deletes the command from Discord (if you passed in a "testServer" property it'll delete from the guild and not globally) | ||
}; | ||
``` | ||
- `interaction` | ||
- `client` is the discord.js Client instance. | ||
- `handler` is the CommandHandler instance. You can use this to get access to properties such as `commands`. | ||
--- | ||
@@ -99,3 +111,3 @@ | ||
// events/ready/console-log.js | ||
module.exports = (argument, client) => { | ||
module.exports = (argument, client, handler) => { | ||
console.log(`${client.user.tag} is online.`); | ||
@@ -106,2 +118,4 @@ }; | ||
- `argument` is the argument you receive from the event being triggered (you can name this whatever you want). For example, the `messageCreate` event will give you an argument of the message object. | ||
- `client` is the discord.js Client instance. | ||
- `handler` is the CommandHandler instance. You can use this to get access to properties such as `commands`. | ||
@@ -123,7 +137,7 @@ --- | ||
// validations/dev-only.js | ||
module.exports = (interaction, commandObj) => { | ||
module.exports = (interaction, commandObj, handler) => { | ||
if (commandObj.devOnly) { | ||
if (interaction.member.id !== 'DEVELOPER_ID') { | ||
interaction.reply('This command is for the developer only'); | ||
return true; // This will stop the command from being executed. | ||
return true; // This must be added to stop the command from being executed. | ||
} | ||
@@ -136,3 +150,4 @@ } | ||
- `commandObj` is the command object exported from the command file itself. Properties such as `name`, `description` and `options` are all available within. | ||
- `handler` is the CommandHandler instance. You can use this to get access to properties such as `commands`. | ||
It's important to return `true` (or any truthy value) if you don't want the command execution to be stopped (this also ensures the next validation queued up is not run). |
Sorry, the diff of this file is not supported yet
29847
6
704
145