Archived
Reformatted source files (print width: 180)
This commit is contained in:
1 parent
54c4c38d62
commit
f57c3b3f86
8 files changed
+94
-403
No files matched your search
@@ -5,12 +5,4 @@ import { MessageEmbed } from "discord.js";
|
||||
import { ObjectID, InputParameter } from "./src/Parameter.js";
|
||||
|
||||
export default Bot;
|
||||
export {
|
||||
Bot,
|
||||
Command,
|
||||
MessageEmbed,
|
||||
ParameterResolvable,
|
||||
ParameterType,
|
||||
ObjectID,
|
||||
InputParameter,
|
||||
};
|
||||
export { Bot, Command, MessageEmbed, ParameterResolvable, ParameterType, ObjectID, InputParameter };
|
||||
+16
-66
@@ -1,11 +1,5 @@
|
||||
import axios from "axios";
|
||||
import {
|
||||
Client,
|
||||
CommandInteraction,
|
||||
GuildMember,
|
||||
Intents,
|
||||
Message,
|
||||
} from "discord.js";
|
||||
import { Client, CommandInteraction, GuildMember, Intents, Message } from "discord.js";
|
||||
import { EventEmitter } from "events";
|
||||
import * as http from "http";
|
||||
import { Command } from "./Command.js";
|
||||
@@ -13,22 +7,12 @@ import { CommandManager } from "./CommandManager.js";
|
||||
import { OperationSuccess, PermissionsError } from "./errors.js";
|
||||
import { HelpMessage } from "./Help.js";
|
||||
import { SystemMessageManager } from "./SystemMessage.js";
|
||||
import {
|
||||
CommandMessageStructure,
|
||||
InitOptions,
|
||||
HelpMessageParams,
|
||||
} from "./types.js";
|
||||
import { CommandMessageStructure, InitOptions, HelpMessageParams } from "./types.js";
|
||||
|
||||
export declare interface Bot {
|
||||
on(event: "READY", listener: Function): this;
|
||||
on(event: "MESSAGE", listener: (m: Message) => void): this;
|
||||
on(
|
||||
event: "COMMAND",
|
||||
listener: (
|
||||
m: Message | CommandInteraction,
|
||||
cmdMsg: CommandMessageStructure
|
||||
) => void
|
||||
): this;
|
||||
on(event: "COMMAND", listener: (m: Message | CommandInteraction, cmdMsg: CommandMessageStructure) => void): this;
|
||||
on(event: "ERROR", listener: (e: any) => void): this;
|
||||
}
|
||||
|
||||
@@ -78,10 +62,7 @@ export class Bot extends EventEmitter {
|
||||
],
|
||||
}
|
||||
);
|
||||
this.commands = new CommandManager(
|
||||
options.prefix,
|
||||
options.argumentSeparator
|
||||
);
|
||||
this.commands = new CommandManager(options.prefix, options.argumentSeparator);
|
||||
this.token = options.token;
|
||||
this.applicationId = options.applicationId;
|
||||
this.messages = {
|
||||
@@ -106,29 +87,17 @@ export class Bot extends EventEmitter {
|
||||
async start(port?: number, register?: boolean): Promise<boolean> {
|
||||
try {
|
||||
console.log(`\nBot name: ${this.name}`);
|
||||
console.log(
|
||||
`Prefix: ${
|
||||
this.commands.prefix || "/ (only slash commands)"
|
||||
} \n`
|
||||
);
|
||||
console.log(`Prefix: ${this.commands.prefix || "/ (only slash commands)"} \n`);
|
||||
if (this.token === "") {
|
||||
throw new ReferenceError(
|
||||
'No token specified. Please pass your Discord application token as an argument to the "start" method or in the constructor'
|
||||
);
|
||||
throw new ReferenceError('No token specified. Please pass your Discord application token as an argument to the "start" method or in the constructor');
|
||||
}
|
||||
if (port) {
|
||||
process.stdout.write(
|
||||
`Creating http server on port ${port}... `
|
||||
);
|
||||
process.stdout.write(`Creating http server on port ${port}... `);
|
||||
http.createServer().listen(port);
|
||||
console.log("✔");
|
||||
}
|
||||
if (this.messages.help.enabled === true) {
|
||||
const helpMsg: Command = new HelpMessage(
|
||||
this.commands,
|
||||
this.messages.help,
|
||||
this.name
|
||||
);
|
||||
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name);
|
||||
this.commands.add(helpMsg);
|
||||
}
|
||||
process.stdout.write("Connecting to Discord... ");
|
||||
@@ -139,9 +108,7 @@ export class Bot extends EventEmitter {
|
||||
process.stdout.write("Registering commands... ");
|
||||
await axios.put(
|
||||
`https://discord.com/api/v8/applications/${this.applicationId}/commands`,
|
||||
this.commands.list
|
||||
.filter((c) => !c.guilds && c.slash)
|
||||
.map((c) => (!c.guilds ? c.toCommandObject() : {})),
|
||||
this.commands.list.filter((c) => !c.guilds && c.slash).map((c) => (!c.guilds ? c.toCommandObject() : {})),
|
||||
{ headers: { Authorization: `Bot ${this.token}` } }
|
||||
);
|
||||
const guilds: any = {};
|
||||
@@ -158,11 +125,9 @@ export class Bot extends EventEmitter {
|
||||
});
|
||||
});
|
||||
for (let i in guilds) {
|
||||
await axios.put(
|
||||
`https://discord.com/api/v8/applications/${this.applicationId}/guilds/${i}/commands`,
|
||||
guilds[i],
|
||||
{ headers: { Authorization: `Bot ${this.token}` } }
|
||||
);
|
||||
await axios.put(`https://discord.com/api/v8/applications/${this.applicationId}/guilds/${i}/commands`, guilds[i], {
|
||||
headers: { Authorization: `Bot ${this.token}` },
|
||||
});
|
||||
}
|
||||
console.log("✔\n");
|
||||
} else {
|
||||
@@ -177,16 +142,9 @@ export class Bot extends EventEmitter {
|
||||
if (cmdMsg) {
|
||||
this.emit("COMMAND", m, cmdMsg);
|
||||
await cmdMsg.command.start(m, cmdMsg.parameters);
|
||||
} else if (
|
||||
this.commands.prefix &&
|
||||
m.content.startsWith(this.commands.prefix)
|
||||
) {
|
||||
} else if (this.commands.prefix && m.content.startsWith(this.commands.prefix)) {
|
||||
this.emit("MESSAGE", m);
|
||||
await this.messages.system.send(
|
||||
"NOT_FOUND",
|
||||
{ phrase: m.content, user: m.member || undefined },
|
||||
m
|
||||
);
|
||||
await this.messages.system.send("NOT_FOUND", { phrase: m.content, user: m.member || undefined }, m);
|
||||
} else {
|
||||
this.emit("MESSAGE", m);
|
||||
}
|
||||
@@ -201,11 +159,7 @@ export class Bot extends EventEmitter {
|
||||
m
|
||||
);
|
||||
} else if (e instanceof OperationSuccess) {
|
||||
await this.messages.system.send(
|
||||
"SUCCESS",
|
||||
undefined,
|
||||
m
|
||||
);
|
||||
await this.messages.system.send("SUCCESS", undefined, m);
|
||||
} else {
|
||||
await this.messages.system.send(
|
||||
"ERROR",
|
||||
@@ -251,11 +205,7 @@ export class Bot extends EventEmitter {
|
||||
i as CommandInteraction
|
||||
);
|
||||
} else if (e instanceof OperationSuccess) {
|
||||
await this.messages.system.send(
|
||||
"SUCCESS",
|
||||
undefined,
|
||||
i as CommandInteraction
|
||||
);
|
||||
await this.messages.system.send("SUCCESS", undefined, i as CommandInteraction);
|
||||
} else {
|
||||
await this.messages.system.send(
|
||||
"ERROR",
|
||||
|
||||
+23
-89
@@ -1,17 +1,6 @@
|
||||
//IMPORTS
|
||||
import {
|
||||
Permissions,
|
||||
Message,
|
||||
MessageEmbed,
|
||||
CommandInteraction,
|
||||
GuildMember,
|
||||
ReplyMessageOptions,
|
||||
} from "discord.js";
|
||||
import {
|
||||
CommandBuilder,
|
||||
ParameterResolvable,
|
||||
PermissionCheckTypes,
|
||||
} from "./types.js";
|
||||
import { Permissions, Message, MessageEmbed, CommandInteraction, GuildMember, ReplyMessageOptions } from "discord.js";
|
||||
import { CommandBuilder, ParameterResolvable, PermissionCheckTypes } from "./types.js";
|
||||
import { OperationSuccess, PermissionsError } from "./errors.js";
|
||||
import { DefaultParameter, InputParameter, Parameter } from "./Parameter.js";
|
||||
|
||||
@@ -29,17 +18,9 @@ export class Command {
|
||||
slash: boolean;
|
||||
announceSuccess: boolean;
|
||||
private function: (
|
||||
params: (
|
||||
query: string,
|
||||
returnType?: "value" | "object"
|
||||
) => ParameterResolvable | InputParameter | null,
|
||||
params: (query: string, returnType?: "value" | "object") => ParameterResolvable | InputParameter | null,
|
||||
interaction?: Message | CommandInteraction
|
||||
) =>
|
||||
| void
|
||||
| string
|
||||
| MessageEmbed
|
||||
| ReplyMessageOptions
|
||||
| Promise<void | string | MessageEmbed | ReplyMessageOptions>;
|
||||
) => void | string | MessageEmbed | ReplyMessageOptions | Promise<void | string | MessageEmbed | ReplyMessageOptions>;
|
||||
|
||||
/**
|
||||
* Command constructor
|
||||
@@ -68,20 +49,12 @@ export class Command {
|
||||
this.aliases = Command.processPhrase(options.aliases);
|
||||
this.description = options.description || "No description";
|
||||
this.usage = options.usage || this.generateUsageFromArguments();
|
||||
this.permissionCheck =
|
||||
options.permissionCheck == "ALL" || options.permissionCheck == "ANY"
|
||||
? options.permissionCheck
|
||||
: "ANY";
|
||||
this.permissions = options.permissions
|
||||
? new Permissions(options.permissions)
|
||||
: undefined;
|
||||
this.permissionCheck = options.permissionCheck == "ALL" || options.permissionCheck == "ANY" ? options.permissionCheck : "ANY";
|
||||
this.permissions = options.permissions ? new Permissions(options.permissions) : undefined;
|
||||
this.guilds = options.guilds;
|
||||
this.visible = options.visible !== undefined ? options.visible : true;
|
||||
this.slash = options.slash !== undefined ? options.slash : true;
|
||||
this.announceSuccess =
|
||||
options.announceSuccess !== undefined
|
||||
? options.announceSuccess
|
||||
: true;
|
||||
this.announceSuccess = options.announceSuccess !== undefined ? options.announceSuccess : true;
|
||||
this.function = options.function;
|
||||
if (!/^[\w-]{1,32}$/.test(this.name)) {
|
||||
throw new Error(`Incorrect command name: ${this.name}`);
|
||||
@@ -97,35 +70,16 @@ export class Command {
|
||||
* @param {InputParameter[]} [cmdParams] - list of processed parameters passed in a Discord message
|
||||
* @returns *Promise<void>*
|
||||
*/
|
||||
async start(
|
||||
interaction?: Message | CommandInteraction,
|
||||
cmdParams?: InputParameter[]
|
||||
): Promise<void> {
|
||||
const paramFindFn = function (
|
||||
query: string,
|
||||
returnType?: "value" | "object"
|
||||
) {
|
||||
return returnType === "object"
|
||||
? cmdParams?.find((p) => p.name === query) || null
|
||||
: cmdParams?.find((p) => p.name === query)?.value || null;
|
||||
async start(interaction?: Message | CommandInteraction, cmdParams?: InputParameter[]): Promise<void> {
|
||||
const paramFindFn = function (query: string, returnType?: "value" | "object") {
|
||||
return returnType === "object" ? cmdParams?.find((p) => p.name === query) || null : cmdParams?.find((p) => p.name === query)?.value || null;
|
||||
};
|
||||
const memberPermissions: Readonly<Permissions> =
|
||||
(interaction?.member?.permissions as Permissions) ||
|
||||
new Permissions();
|
||||
if (
|
||||
!this.permissions ||
|
||||
(this.permissionCheck == "ALL"
|
||||
? memberPermissions.has(this.permissions, true)
|
||||
: memberPermissions.any(this.permissions, true))
|
||||
) {
|
||||
const memberPermissions: Readonly<Permissions> = (interaction?.member?.permissions as Permissions) || new Permissions();
|
||||
if (!this.permissions || (this.permissionCheck == "ALL" ? memberPermissions.has(this.permissions, true) : memberPermissions.any(this.permissions, true))) {
|
||||
if (interaction instanceof CommandInteraction) {
|
||||
await interaction.deferReply();
|
||||
}
|
||||
const fnResult = await this.function.call(
|
||||
this,
|
||||
paramFindFn,
|
||||
interaction
|
||||
);
|
||||
const fnResult = await this.function.call(this, paramFindFn, interaction);
|
||||
if (
|
||||
fnResult instanceof Object &&
|
||||
("content" in (fnResult as any) ||
|
||||
@@ -134,34 +88,24 @@ export class Command {
|
||||
"components" in (fnResult as any) ||
|
||||
"sticker" in (fnResult as any))
|
||||
) {
|
||||
if (interaction instanceof Message)
|
||||
await interaction.reply(fnResult as ReplyMessageOptions);
|
||||
else if (interaction instanceof CommandInteraction)
|
||||
await interaction.editReply(
|
||||
fnResult as ReplyMessageOptions
|
||||
);
|
||||
if (interaction instanceof Message) await interaction.reply(fnResult as ReplyMessageOptions);
|
||||
else if (interaction instanceof CommandInteraction) await interaction.editReply(fnResult as ReplyMessageOptions);
|
||||
} else if (typeof fnResult == "string") {
|
||||
if (interaction instanceof Message)
|
||||
await interaction?.reply({ content: fnResult });
|
||||
if (interaction instanceof Message) await interaction?.reply({ content: fnResult });
|
||||
else if (interaction instanceof CommandInteraction)
|
||||
await interaction.editReply({
|
||||
content: fnResult,
|
||||
});
|
||||
} else if (fnResult instanceof MessageEmbed) {
|
||||
if (interaction instanceof Message)
|
||||
await interaction?.reply({ embeds: [fnResult] });
|
||||
else if (interaction instanceof CommandInteraction)
|
||||
await interaction.editReply({ embeds: [fnResult] });
|
||||
if (interaction instanceof Message) await interaction?.reply({ embeds: [fnResult] });
|
||||
else if (interaction instanceof CommandInteraction) await interaction.editReply({ embeds: [fnResult] });
|
||||
} else if (this.announceSuccess) {
|
||||
throw new OperationSuccess(this);
|
||||
} else if (interaction instanceof CommandInteraction) {
|
||||
await interaction.deleteReply();
|
||||
}
|
||||
} else {
|
||||
throw new PermissionsError(
|
||||
this,
|
||||
interaction?.member as GuildMember
|
||||
);
|
||||
throw new PermissionsError(this, interaction?.member as GuildMember);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,14 +119,10 @@ export class Command {
|
||||
options = this.parameters.map((p) => {
|
||||
let type = 3;
|
||||
if (!/^[\w-]{1,32}$/.test(p.name)) {
|
||||
throw new Error(
|
||||
`Failed to register ${p.name} parameter for ${this.name}: The option name is incorrect`
|
||||
);
|
||||
throw new Error(`Failed to register ${p.name} parameter for ${this.name}: The option name is incorrect`);
|
||||
}
|
||||
if ((p.description || "No description").length > 100) {
|
||||
throw new Error(
|
||||
`Failed to register ${p.name} parameter for ${this.name}: The description is too long`
|
||||
);
|
||||
throw new Error(`Failed to register ${p.name} parameter for ${this.name}: The description is too long`);
|
||||
}
|
||||
if (p.type == "boolean") type = 5;
|
||||
else if (p.type == "user") type = 6;
|
||||
@@ -206,9 +146,7 @@ export class Command {
|
||||
};
|
||||
}
|
||||
|
||||
private static processPhrase(
|
||||
phrase?: string | string[]
|
||||
): string[] | undefined {
|
||||
private static processPhrase(phrase?: string | string[]): string[] | undefined {
|
||||
if (Array.isArray(phrase)) {
|
||||
const buff = phrase.map((p) => {
|
||||
return p.split(" ").join("_");
|
||||
@@ -232,11 +170,7 @@ export class Command {
|
||||
let usageTemplate: string = "";
|
||||
this.parameters &&
|
||||
this.parameters.map((e) => {
|
||||
usageTemplate += `[${e.name} (${
|
||||
e.choices
|
||||
? e.choices.map((c) => c.name).join(" / ")
|
||||
: e.type
|
||||
}${e.optional ? ", optional" : ""})] `;
|
||||
usageTemplate += `[${e.name} (${e.choices ? e.choices.map((c) => c.name).join(" / ") : e.type}${e.optional ? ", optional" : ""})] `;
|
||||
});
|
||||
return usageTemplate;
|
||||
}
|
||||
|
||||
+17
-54
@@ -1,12 +1,6 @@
|
||||
import { CommandInteraction, Message } from "discord.js";
|
||||
import { MissingParameterError } from "./errors.js";
|
||||
import {
|
||||
BooleanParameter,
|
||||
InputParameter,
|
||||
NumberParameter,
|
||||
ObjectParameter,
|
||||
StringParameter,
|
||||
} from "./Parameter.js";
|
||||
import { BooleanParameter, InputParameter, NumberParameter, ObjectParameter, StringParameter } from "./Parameter.js";
|
||||
import { Command } from "./Command.js";
|
||||
import { CommandMessageStructure, PhraseOccurrenceData } from "./types.js";
|
||||
|
||||
@@ -53,17 +47,13 @@ export class CommandManager {
|
||||
if (!(command instanceof Command)) {
|
||||
throw new TypeError("Inavlid argument type");
|
||||
}
|
||||
const nameOccurrence: PhraseOccurrenceData | null =
|
||||
this.findPhraseOccurrence(command.name);
|
||||
const nameOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(command.name);
|
||||
if (nameOccurrence) {
|
||||
throw new Error(
|
||||
`The name "${command.name}" has already been registered as ${nameOccurrence.type} in the "${nameOccurrence.command.name}" command.`
|
||||
);
|
||||
throw new Error(`The name "${command.name}" has already been registered as ${nameOccurrence.type} in the "${nameOccurrence.command.name}" command.`);
|
||||
}
|
||||
command.aliases &&
|
||||
command.aliases.map((a, i, ar) => {
|
||||
const aliasOccurrence: PhraseOccurrenceData | null =
|
||||
this.findPhraseOccurrence(a);
|
||||
const aliasOccurrence: PhraseOccurrenceData | null = this.findPhraseOccurrence(a);
|
||||
if (aliasOccurrence) {
|
||||
console.warn(
|
||||
`[⚠ WARN] The name "${a}" is already registered as ${aliasOccurrence.type} in the "${aliasOccurrence.command.name}" command. It will be removed from the "${command.name}" command.`
|
||||
@@ -92,15 +82,10 @@ export class CommandManager {
|
||||
const command = this.get(name);
|
||||
if (command) {
|
||||
const argumentsText = content.replace(name, "");
|
||||
const paramsList = argumentsText
|
||||
.split(this.argumentSeparator)
|
||||
.map((a) => {
|
||||
const paramsList = argumentsText.split(this.argumentSeparator).map((a) => {
|
||||
return a.replace(" ", "");
|
||||
});
|
||||
if (
|
||||
(paramsList[0] == "" || paramsList[0] == " ") &&
|
||||
paramsList.length == 1
|
||||
) {
|
||||
if ((paramsList[0] == "" || paramsList[0] == " ") && paramsList.length == 1) {
|
||||
paramsList.splice(0, 1);
|
||||
}
|
||||
const parameters: InputParameter[] = [];
|
||||
@@ -115,29 +100,19 @@ export class CommandManager {
|
||||
case "channel":
|
||||
case "role":
|
||||
case "user":
|
||||
parameters.push(
|
||||
new ObjectParameter(p, paramsList[i])
|
||||
);
|
||||
parameters.push(new ObjectParameter(p, paramsList[i]));
|
||||
break;
|
||||
case "string":
|
||||
parameters.push(
|
||||
new StringParameter(p, paramsList[i])
|
||||
);
|
||||
parameters.push(new StringParameter(p, paramsList[i]));
|
||||
break;
|
||||
case "boolean":
|
||||
parameters.push(
|
||||
new BooleanParameter(p, paramsList[i])
|
||||
);
|
||||
parameters.push(new BooleanParameter(p, paramsList[i]));
|
||||
break;
|
||||
case "number":
|
||||
parameters.push(
|
||||
new NumberParameter(p, paramsList[i])
|
||||
);
|
||||
parameters.push(new NumberParameter(p, paramsList[i]));
|
||||
break;
|
||||
default:
|
||||
parameters.push(
|
||||
new InputParameter(p, paramsList[i])
|
||||
);
|
||||
parameters.push(new InputParameter(p, paramsList[i]));
|
||||
break;
|
||||
}
|
||||
});
|
||||
@@ -158,9 +133,7 @@ export class CommandManager {
|
||||
* @param {CommandInteraction} interaction - interaction object
|
||||
* @returns *CommandMessagesStructure* | *null*
|
||||
*/
|
||||
fetchFromInteraction(
|
||||
interaction: CommandInteraction
|
||||
): CommandMessageStructure | null {
|
||||
fetchFromInteraction(interaction: CommandInteraction): CommandMessageStructure | null {
|
||||
const cmd = this.get(interaction.commandName);
|
||||
if (cmd) {
|
||||
if (interaction.options?.data) {
|
||||
@@ -178,29 +151,19 @@ export class CommandManager {
|
||||
case "channel":
|
||||
case "role":
|
||||
case "user":
|
||||
parameters.push(
|
||||
new ObjectParameter(p, inputParam?.value)
|
||||
);
|
||||
parameters.push(new ObjectParameter(p, inputParam?.value));
|
||||
break;
|
||||
case "string":
|
||||
parameters.push(
|
||||
new StringParameter(p, inputParam?.value)
|
||||
);
|
||||
parameters.push(new StringParameter(p, inputParam?.value));
|
||||
break;
|
||||
case "boolean":
|
||||
parameters.push(
|
||||
new BooleanParameter(p, inputParam?.value)
|
||||
);
|
||||
parameters.push(new BooleanParameter(p, inputParam?.value));
|
||||
break;
|
||||
case "number":
|
||||
parameters.push(
|
||||
new NumberParameter(p, inputParam?.value)
|
||||
);
|
||||
parameters.push(new NumberParameter(p, inputParam?.value));
|
||||
break;
|
||||
default:
|
||||
parameters.push(
|
||||
new InputParameter(p, inputParam?.value)
|
||||
);
|
||||
parameters.push(new InputParameter(p, inputParam?.value));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
+9
-42
@@ -4,11 +4,7 @@ import { CommandManager } from "./CommandManager.js";
|
||||
import { HelpMessageParams } from "./types.js";
|
||||
|
||||
export class HelpMessage extends Command {
|
||||
constructor(
|
||||
cmdManager: CommandManager,
|
||||
params: HelpMessageParams,
|
||||
botName?: string
|
||||
) {
|
||||
constructor(cmdManager: CommandManager, params: HelpMessageParams, botName?: string) {
|
||||
super({
|
||||
name: "help",
|
||||
usage: params.usage,
|
||||
@@ -17,8 +13,7 @@ export class HelpMessage extends Command {
|
||||
parameters: [
|
||||
{
|
||||
name: "command_name",
|
||||
description:
|
||||
"Name of the command that you want to get details about",
|
||||
description: "Name of the command that you want to get details about",
|
||||
optional: true,
|
||||
type: "string",
|
||||
},
|
||||
@@ -30,35 +25,17 @@ export class HelpMessage extends Command {
|
||||
helpMsg.setFooter(botName || "");
|
||||
if (helpMsg != null) {
|
||||
if (param("command_name")) {
|
||||
const cmd: Command | null = cmdManager.get(
|
||||
param("command_name")?.toString() || ""
|
||||
);
|
||||
const cmd: Command | null = cmdManager.get(param("command_name")?.toString() || "");
|
||||
if (cmd) {
|
||||
helpMsg.setTitle(
|
||||
`${cmd.name} ${cmd.visible ? "" : "[HIDDEN]"}`
|
||||
);
|
||||
helpMsg.setTitle(`${cmd.name} ${cmd.visible ? "" : "[HIDDEN]"}`);
|
||||
helpMsg.setDescription(cmd.description);
|
||||
if (cmd.usage)
|
||||
helpMsg.addField(
|
||||
"Usage:",
|
||||
`${cmdManager.prefix || "/"}${cmd.name} ${
|
||||
cmd.usage
|
||||
}`,
|
||||
false
|
||||
);
|
||||
if (
|
||||
cmd.permissions &&
|
||||
cmd.permissions.toArray(false).length > 0
|
||||
) {
|
||||
if (cmd.usage) helpMsg.addField("Usage:", `${cmdManager.prefix || "/"}${cmd.name} ${cmd.usage}`, false);
|
||||
if (cmd.permissions && cmd.permissions.toArray(false).length > 0) {
|
||||
let permList: string = "";
|
||||
cmd.permissions.toArray(false).map((p) => {
|
||||
permList += p + "\n";
|
||||
});
|
||||
helpMsg.addField(
|
||||
"Permissions:",
|
||||
permList,
|
||||
false
|
||||
);
|
||||
helpMsg.addField("Permissions:", permList, false);
|
||||
}
|
||||
if (cmd.aliases && cmd.aliases.length > 0) {
|
||||
let aList: string = "";
|
||||
@@ -68,11 +45,7 @@ export class HelpMessage extends Command {
|
||||
helpMsg.addField("Aliases:", aList, false);
|
||||
}
|
||||
} else {
|
||||
throw new ReferenceError(
|
||||
`Command "${param(
|
||||
"command_name"
|
||||
)}" does not exist`
|
||||
);
|
||||
throw new ReferenceError(`Command "${param("command_name")}" does not exist`);
|
||||
}
|
||||
} else {
|
||||
helpMsg.setTitle(params.title);
|
||||
@@ -80,13 +53,7 @@ export class HelpMessage extends Command {
|
||||
|
||||
cmdManager.list.map((c) => {
|
||||
if (c.visible) {
|
||||
helpMsg.addField(
|
||||
`${cmdManager.prefix || "/"}${c.name} ${
|
||||
c.usage
|
||||
}`,
|
||||
c.description,
|
||||
false
|
||||
);
|
||||
helpMsg.addField(`${cmdManager.prefix || "/"}${c.name} ${c.usage}`, c.description, false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+10
-53
@@ -1,14 +1,4 @@
|
||||
import {
|
||||
CategoryChannel,
|
||||
Guild,
|
||||
GuildMember,
|
||||
NewsChannel,
|
||||
Role,
|
||||
StageChannel,
|
||||
StoreChannel,
|
||||
TextChannel,
|
||||
VoiceChannel,
|
||||
} from "discord.js";
|
||||
import { CategoryChannel, Guild, GuildMember, NewsChannel, Role, StageChannel, StoreChannel, TextChannel, VoiceChannel } from "discord.js";
|
||||
import { ParameterType, ParameterResolvable, Choice } from "./types.js";
|
||||
export interface ParameterSchema {
|
||||
name: string;
|
||||
@@ -31,9 +21,7 @@ export class Parameter {
|
||||
this.type = options.type;
|
||||
this.choices = options.choices;
|
||||
if (!/^[\w-]{1,32}$/.test(this.name)) {
|
||||
throw new Error(
|
||||
`Parameter name ${this.name} doesn't match the pattern`
|
||||
);
|
||||
throw new Error(`Parameter name ${this.name} doesn't match the pattern`);
|
||||
}
|
||||
if (this.description.length > 100) {
|
||||
throw new Error(`Parameter ${this.name}: Description too long`);
|
||||
@@ -65,16 +53,10 @@ export class StringParameter extends InputParameter {
|
||||
throw new Error(`Parameter type mismatch`);
|
||||
}
|
||||
if (parameter.choices) {
|
||||
const match = parameter.choices.filter(
|
||||
(c) =>
|
||||
c.name.toLowerCase() === value?.toString().toLowerCase() ||
|
||||
c.value.toLowerCase() === value?.toString().toLowerCase()
|
||||
);
|
||||
const match = parameter.choices.filter((c) => c.name.toLowerCase() === value?.toString().toLowerCase() || c.value.toLowerCase() === value?.toString().toLowerCase());
|
||||
if (match.length === 0) {
|
||||
throw new Error(
|
||||
`Parameter "${
|
||||
parameter.name
|
||||
}" has incorrect value. Please enter one of the following values: ${parameter.choices
|
||||
`Parameter "${parameter.name}" has incorrect value. Please enter one of the following values: ${parameter.choices
|
||||
.map((c) => [c.name, c.value])
|
||||
.flat(1)
|
||||
.join(", ")}`
|
||||
@@ -97,9 +79,7 @@ export class BooleanParameter extends InputParameter {
|
||||
} else if (value?.toString().toLowerCase() === "false") {
|
||||
super(parameter, false);
|
||||
} else {
|
||||
throw new Error(
|
||||
`Cannot convert "${parameter.name}" parameter to boolean. Please enter either "true" or "false".`
|
||||
);
|
||||
throw new Error(`Cannot convert "${parameter.name}" parameter to boolean. Please enter either "true" or "false".`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -113,12 +93,7 @@ export class NumberParameter extends InputParameter {
|
||||
}
|
||||
export class ObjectParameter extends InputParameter {
|
||||
constructor(parameter: Parameter, value: ParameterResolvable) {
|
||||
if (
|
||||
parameter.type != "channel" &&
|
||||
parameter.type != "mentionable" &&
|
||||
parameter.type != "user" &&
|
||||
parameter.type != "role"
|
||||
) {
|
||||
if (parameter.type != "channel" && parameter.type != "mentionable" && parameter.type != "user" && parameter.type != "role") {
|
||||
throw new Error(`Parameter type mismatch`);
|
||||
}
|
||||
super(parameter, new ObjectID(value?.toString() || ""));
|
||||
@@ -132,32 +107,14 @@ export class ObjectID {
|
||||
async toObject(
|
||||
guild: Guild,
|
||||
type: "channel" | "user" | "role"
|
||||
): Promise<
|
||||
| Role
|
||||
| TextChannel
|
||||
| VoiceChannel
|
||||
| CategoryChannel
|
||||
| GuildMember
|
||||
| NewsChannel
|
||||
| StoreChannel
|
||||
| StageChannel
|
||||
| null
|
||||
> {
|
||||
): Promise<Role | TextChannel | VoiceChannel | CategoryChannel | GuildMember | NewsChannel | StoreChannel | StageChannel | null> {
|
||||
switch (type) {
|
||||
case "channel":
|
||||
return (
|
||||
(await guild.channels.fetch(this.id.toString() || "")) ||
|
||||
null
|
||||
);
|
||||
return (await guild.channels.fetch(this.id.toString() || "")) || null;
|
||||
case "role":
|
||||
return (
|
||||
(await guild.roles.fetch(this.id.toString() || "")) || null
|
||||
);
|
||||
return (await guild.roles.fetch(this.id.toString() || "")) || null;
|
||||
case "user":
|
||||
return (
|
||||
(await guild.members.fetch(this.id.toString() || "")) ||
|
||||
null
|
||||
);
|
||||
return (await guild.members.fetch(this.id.toString() || "")) || null;
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-60
@@ -1,11 +1,4 @@
|
||||
import {
|
||||
ColorResolvable,
|
||||
CommandInteraction,
|
||||
GuildMember,
|
||||
Message,
|
||||
MessageEmbed,
|
||||
User,
|
||||
} from "discord.js";
|
||||
import { ColorResolvable, CommandInteraction, GuildMember, Message, MessageEmbed, User } from "discord.js";
|
||||
import { Command } from "./Command.js";
|
||||
import { PermissionsError } from "./errors.js";
|
||||
|
||||
@@ -73,19 +66,14 @@ export class SystemMessageManager {
|
||||
* @param {Message | CommandInteraction} [interaction] - if specified, the generated message will be sent in this channel
|
||||
* @returns *Promise<MessageEmbed | Message | void>*
|
||||
*/
|
||||
async send(
|
||||
type: MessageType,
|
||||
data?: SystemMessageData,
|
||||
interaction?: Message | CommandInteraction
|
||||
): Promise<MessageEmbed | Message | void> {
|
||||
async send(type: MessageType, data?: SystemMessageData, interaction?: Message | CommandInteraction): Promise<MessageEmbed | Message | void> {
|
||||
if (this[type]) {
|
||||
if (this[type].enabled === false) {
|
||||
return;
|
||||
}
|
||||
const embed = new MessageEmbed();
|
||||
embed.setTitle(this[type].title);
|
||||
if (this[type].bottomText)
|
||||
embed.setDescription(this[type].bottomText || "");
|
||||
if (this[type].bottomText) embed.setDescription(this[type].bottomText || "");
|
||||
embed.setColor(this[type].accentColor || "#000");
|
||||
if (this[type].showTimestamp) embed.setTimestamp();
|
||||
if (this[type].footer) embed.setFooter(this[type].footer || "");
|
||||
@@ -93,33 +81,17 @@ export class SystemMessageManager {
|
||||
switch (type) {
|
||||
case "ERROR":
|
||||
if (data.command) {
|
||||
embed.addField(
|
||||
"Command name:",
|
||||
data.command.name,
|
||||
false
|
||||
);
|
||||
embed.addField("Command name:", data.command.name, false);
|
||||
}
|
||||
if (data.error) {
|
||||
if (data.error instanceof Error) {
|
||||
embed.addField(
|
||||
"Error details:",
|
||||
data.error.toString(),
|
||||
false
|
||||
);
|
||||
embed.addField("Error details:", data.error.toString(), false);
|
||||
} else if (typeof data.error == "string") {
|
||||
embed.addField(
|
||||
"Error details:",
|
||||
data.error,
|
||||
false
|
||||
);
|
||||
embed.addField("Error details:", data.error, false);
|
||||
}
|
||||
}
|
||||
if (data.user) {
|
||||
embed.addField(
|
||||
"User:",
|
||||
data.user.toString(),
|
||||
false
|
||||
);
|
||||
embed.addField("User:", data.user.toString(), false);
|
||||
}
|
||||
break;
|
||||
case "NOT_FOUND":
|
||||
@@ -129,30 +101,16 @@ export class SystemMessageManager {
|
||||
break;
|
||||
case "PERMISSION":
|
||||
if (data.user) {
|
||||
embed.addField(
|
||||
"User:",
|
||||
data.user.toString(),
|
||||
false
|
||||
);
|
||||
embed.addField("User:", data.user.toString(), false);
|
||||
}
|
||||
if (data.command) {
|
||||
embed.addField(
|
||||
"Command name:",
|
||||
data.command.name,
|
||||
false
|
||||
);
|
||||
embed.addField("Command name:", data.command.name, false);
|
||||
if (data.command.permissions) {
|
||||
let permList: string = "";
|
||||
data.command.permissions
|
||||
.toArray(false)
|
||||
.map((p) => {
|
||||
data.command.permissions.toArray(false).map((p) => {
|
||||
permList += p + "\n";
|
||||
});
|
||||
embed.addField(
|
||||
"Required permissions:",
|
||||
permList,
|
||||
false
|
||||
);
|
||||
embed.addField("Required permissions:", permList, false);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -168,13 +126,8 @@ export class SystemMessageManager {
|
||||
}, this.deleteTimeout || 0);
|
||||
}
|
||||
return message;
|
||||
} else if (
|
||||
interaction &&
|
||||
interaction instanceof CommandInteraction
|
||||
) {
|
||||
interaction.replied || interaction.deferred
|
||||
? await interaction.editReply({ embeds: [embed] })
|
||||
: await interaction.reply({ embeds: [embed] });
|
||||
} else if (interaction && interaction instanceof CommandInteraction) {
|
||||
interaction.replied || interaction.deferred ? await interaction.editReply({ embeds: [embed] }) : await interaction.reply({ embeds: [embed] });
|
||||
} else {
|
||||
return embed;
|
||||
}
|
||||
|
||||
+5
-30
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
ClientOptions,
|
||||
ColorResolvable,
|
||||
CommandInteraction,
|
||||
ReplyMessageOptions,
|
||||
} from "discord.js";
|
||||
import { ClientOptions, ColorResolvable, CommandInteraction, ReplyMessageOptions } from "discord.js";
|
||||
import { InputParameter, ObjectID, ParameterSchema } from "./Parameter.js";
|
||||
import { PermissionResolvable, Message, MessageEmbed } from "discord.js";
|
||||
import type { Command } from "./Command.js";
|
||||
@@ -31,17 +26,9 @@ export interface CommandBuilder {
|
||||
slash?: boolean;
|
||||
announceSuccess?: boolean;
|
||||
function: (
|
||||
params: (
|
||||
query: string,
|
||||
returnType?: "value" | "object"
|
||||
) => ParameterResolvable | InputParameter | null,
|
||||
params: (query: string, returnType?: "value" | "object") => ParameterResolvable | InputParameter | null,
|
||||
interaction?: Message | CommandInteraction
|
||||
) =>
|
||||
| void
|
||||
| string
|
||||
| MessageEmbed
|
||||
| ReplyMessageOptions
|
||||
| Promise<void | string | MessageEmbed | ReplyMessageOptions>;
|
||||
) => void | string | MessageEmbed | ReplyMessageOptions | Promise<void | string | MessageEmbed | ReplyMessageOptions>;
|
||||
}
|
||||
export interface PhraseOccurrenceData {
|
||||
command: Command;
|
||||
@@ -59,20 +46,8 @@ export interface HelpMessageParams {
|
||||
description: string;
|
||||
usage: string;
|
||||
}
|
||||
export type ParameterType =
|
||||
| "string"
|
||||
| "boolean"
|
||||
| "number"
|
||||
| "user"
|
||||
| "role"
|
||||
| "channel"
|
||||
| "mentionable";
|
||||
export type ParameterResolvable =
|
||||
| string
|
||||
| boolean
|
||||
| number
|
||||
| ObjectID
|
||||
| undefined;
|
||||
export type ParameterType = "string" | "boolean" | "number" | "user" | "role" | "channel" | "mentionable";
|
||||
export type ParameterResolvable = string | boolean | number | ObjectID | undefined;
|
||||
export interface Choice {
|
||||
name: string;
|
||||
value: string;
|
||||
|
||||
Reference in new issue
Block a user