Archived
beta1d - removed timeouts, cleaned up code, updated JSDoc, added options to disable command as a slash command
This commit is contained in:
1 parent
027c5aa15a
commit
4b37c92781
5 files changed
+23
-24
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "commandbot",
|
"name": "commandbot",
|
||||||
"version": "3.0.0s-beta1c",
|
"version": "3.0.0-s-beta1d",
|
||||||
"description": "A framework that helps you create your own Discord bot easier.",
|
"description": "A framework that helps you create your own Discord bot easier.",
|
||||||
"main": "dist/cjs/index.js",
|
"main": "dist/cjs/index.js",
|
||||||
"module": "dist/mjs/index.js",
|
"module": "dist/mjs/index.js",
|
||||||
|
|||||||
+1
-1
@@ -121,7 +121,7 @@ export class Bot extends EventEmitter {
|
|||||||
await axios.put(
|
await axios.put(
|
||||||
`https://discord.com/api/v8/applications/${this.applicationId}/commands`,
|
`https://discord.com/api/v8/applications/${this.applicationId}/commands`,
|
||||||
this.commands.list
|
this.commands.list
|
||||||
.filter((c) => !c.guilds)
|
.filter((c) => !c.guilds && c.slash)
|
||||||
.map((c) => (!c.guilds ? c.toCommandObject() : {})),
|
.map((c) => (!c.guilds ? c.toCommandObject() : {})),
|
||||||
{ headers: { Authorization: `Bot ${this.token}` } }
|
{ headers: { Authorization: `Bot ${this.token}` } }
|
||||||
);
|
);
|
||||||
|
|||||||
+15
-22
@@ -26,6 +26,7 @@ export class Command {
|
|||||||
permissions?: Permissions;
|
permissions?: Permissions;
|
||||||
guilds?: string[];
|
guilds?: string[];
|
||||||
visible: boolean;
|
visible: boolean;
|
||||||
|
slash: boolean;
|
||||||
private function: (
|
private function: (
|
||||||
params: (
|
params: (
|
||||||
query: string,
|
query: string,
|
||||||
@@ -44,13 +45,14 @@ export class Command {
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @param {CommandBuilder} options - all command properties
|
* @param {CommandBuilder} options - all command properties
|
||||||
* @param {string} options.name - command name (used to trigger the command)
|
* @param {string} options.name - command name (used to trigger the command)
|
||||||
|
* @param {ParameterSchema} options.parameters - all parameters that can be passed with the command
|
||||||
* @param {string | string[]} [options.aliases] - other words that can trigger the command with prefix (not used in slash commands)
|
* @param {string | string[]} [options.aliases] - other words that can trigger the command with prefix (not used in slash commands)
|
||||||
* @param {string | string[]} [options.keywords] - other words that can trigger the command without prefix (not used in slash commands)
|
|
||||||
* @param {string} [options.description="No description"] - command description shown in the help message
|
* @param {string} [options.description="No description"] - command description shown in the help message
|
||||||
* @param {string} [options.usage] - command usage description shown in the help message (usage message can be automatically generated using parameters)
|
* @param {string} [options.usage] - command usage description shown in the help message (usage message can be automatically generated using parameters)
|
||||||
* @param {PermissionCheckTypes} [options.permissionCheck='ANY'] - specifies if the caller has to have all of the specified permissions or any of that
|
* @param {PermissionCheckTypes} [options.permissionCheck='ANY'] - specifies if the caller has to have all of the specified permissions or any of that
|
||||||
* @param {PermissionResolvable} [options.permissions] - permissions needed to run the command
|
* @param {PermissionResolvable} [options.permissions] - permissions needed to run the command
|
||||||
* @param {boolean} [options.visible=true] - show command in the help message (visible as a slash command)
|
* @param {boolean} [options.visible=true] - show command in the help message (visible as a slash command)
|
||||||
|
* @param {boolean} [options.slash=true] - whether the command should be available as a slash command
|
||||||
* @param {Function} options.function - function that will trigger when the commands gets called
|
* @param {Function} options.function - function that will trigger when the commands gets called
|
||||||
*/
|
*/
|
||||||
constructor(options: CommandBuilder) {
|
constructor(options: CommandBuilder) {
|
||||||
@@ -73,7 +75,8 @@ export class Command {
|
|||||||
? new Permissions(options.permissions)
|
? new Permissions(options.permissions)
|
||||||
: undefined;
|
: undefined;
|
||||||
this.guilds = options.guilds;
|
this.guilds = options.guilds;
|
||||||
this.visible = options.visible != undefined ? options.visible : true;
|
this.visible = options.visible !== undefined ? options.visible : true;
|
||||||
|
this.slash = options.slash !== undefined ? options.slash : true;
|
||||||
this.function = options.function;
|
this.function = options.function;
|
||||||
if (!/^[\w-]{1,32}$/.test(this.name)) {
|
if (!/^[\w-]{1,32}$/.test(this.name)) {
|
||||||
throw new Error(`Incorrect command name: ${this.name}`);
|
throw new Error(`Incorrect command name: ${this.name}`);
|
||||||
@@ -85,8 +88,8 @@ export class Command {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the command
|
* Starts the command
|
||||||
* @param {Message} [message] - a *Message* object used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor)
|
* @param {Message | CommandInteraction} [interaction] - a *Message* or *CommandInteraction* object used to check caller's permissions. It will get passed to the execution function (specified in *function* property of command's constructor)
|
||||||
* @param {string[]} [cmdParams] - list of processed parameters passed in a Discord message
|
* @param {InputParameter[]} [cmdParams] - list of processed parameters passed in a Discord message
|
||||||
* @returns *Promise<void>*
|
* @returns *Promise<void>*
|
||||||
*/
|
*/
|
||||||
async start(
|
async start(
|
||||||
@@ -111,12 +114,8 @@ export class Command {
|
|||||||
: memberPermissions.any(this.permissions, true))
|
: memberPermissions.any(this.permissions, true))
|
||||||
) {
|
) {
|
||||||
if (interaction instanceof CommandInteraction) {
|
if (interaction instanceof CommandInteraction) {
|
||||||
setTimeout(async () => {
|
|
||||||
if (!interaction.replied) {
|
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
}
|
}
|
||||||
}, 1500);
|
|
||||||
}
|
|
||||||
const fnResult = await this.function.call(
|
const fnResult = await this.function.call(
|
||||||
this,
|
this,
|
||||||
paramFindFn,
|
paramFindFn,
|
||||||
@@ -133,31 +132,21 @@ export class Command {
|
|||||||
if (interaction instanceof Message)
|
if (interaction instanceof Message)
|
||||||
await interaction.reply(fnResult as ReplyMessageOptions);
|
await interaction.reply(fnResult as ReplyMessageOptions);
|
||||||
else if (interaction instanceof CommandInteraction)
|
else if (interaction instanceof CommandInteraction)
|
||||||
interaction.replied || interaction.deferred
|
await interaction.editReply(
|
||||||
? await interaction.editReply(
|
|
||||||
fnResult as ReplyMessageOptions
|
|
||||||
)
|
|
||||||
: await interaction.reply(
|
|
||||||
fnResult as ReplyMessageOptions
|
fnResult as ReplyMessageOptions
|
||||||
);
|
);
|
||||||
} else if (typeof fnResult == "string") {
|
} else if (typeof fnResult == "string") {
|
||||||
if (interaction instanceof Message)
|
if (interaction instanceof Message)
|
||||||
await interaction?.reply({ content: fnResult });
|
await interaction?.reply({ content: fnResult });
|
||||||
else if (interaction instanceof CommandInteraction)
|
else if (interaction instanceof CommandInteraction)
|
||||||
interaction.replied || interaction.deferred
|
await interaction.editReply({
|
||||||
? await interaction.editReply({
|
|
||||||
content: fnResult,
|
|
||||||
})
|
|
||||||
: await interaction.reply({
|
|
||||||
content: fnResult,
|
content: fnResult,
|
||||||
});
|
});
|
||||||
} else if (fnResult instanceof MessageEmbed) {
|
} else if (fnResult instanceof MessageEmbed) {
|
||||||
if (interaction instanceof Message)
|
if (interaction instanceof Message)
|
||||||
await interaction?.channel.send({ embeds: [fnResult] });
|
await interaction?.reply({ embeds: [fnResult] });
|
||||||
else if (interaction instanceof CommandInteraction)
|
else if (interaction instanceof CommandInteraction)
|
||||||
interaction.replied || interaction.deferred
|
await interaction.editReply({ embeds: [fnResult] });
|
||||||
? await interaction.editReply({ embeds: [fnResult] })
|
|
||||||
: await interaction.reply({ embeds: [fnResult] });
|
|
||||||
} else if (
|
} else if (
|
||||||
interaction instanceof CommandInteraction &&
|
interaction instanceof CommandInteraction &&
|
||||||
!interaction.replied
|
!interaction.replied
|
||||||
@@ -172,6 +161,10 @@ export class Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns object that is ready to be registered in the Discord API
|
||||||
|
* @returns {Object} object
|
||||||
|
*/
|
||||||
toCommandObject() {
|
toCommandObject() {
|
||||||
let options: any[] = [];
|
let options: any[] = [];
|
||||||
if (this.parameters) {
|
if (this.parameters) {
|
||||||
|
|||||||
@@ -154,6 +154,11 @@ export class CommandManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetches command and parameters from the given *CommandInteraction* object
|
||||||
|
* @param {CommandInteraction} interaction - interaction object
|
||||||
|
* @returns *CommandMessagesStructure* | *null*
|
||||||
|
*/
|
||||||
fetchFromInteraction(
|
fetchFromInteraction(
|
||||||
interaction: CommandInteraction
|
interaction: CommandInteraction
|
||||||
): CommandMessageStructure | null {
|
): CommandMessageStructure | null {
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export interface CommandBuilder {
|
|||||||
permissions?: PermissionResolvable;
|
permissions?: PermissionResolvable;
|
||||||
guilds?: string[];
|
guilds?: string[];
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
|
slash?: boolean;
|
||||||
function: (
|
function: (
|
||||||
params: (
|
params: (
|
||||||
query: string,
|
query: string,
|
||||||
|
|||||||
Reference in new issue
Block a user