Discord.js 13 + some additional preparation for slash commands

This commit is contained in:
mmacierzanka committed 2021-08-07 01:39:36 +02:00
1 parent ff14817f32
commit 861faec976
8 files changed
+173 -70

No files matched your search

+16 -10
View File
@@ -1,4 +1,4 @@
import { Client, Message } from "discord.js";
import { Client, Intents, Message, TextChannel } from "discord.js";
import { Command } from "./Command.js";
import { CommandManager } from "./CommandManager.js";
import {
@@ -27,6 +27,7 @@ export class Bot extends EventEmitter {
client: Client;
commands: CommandManager;
token: string;
applicationId: string;
messages: {
help: HelpMessageParams;
system: SystemMessageManager;
@@ -45,12 +46,15 @@ export class Bot extends EventEmitter {
constructor(options: InitOptions) {
super();
this.name = options.name;
this.client = new Client(options.clientOptions);
this.client = new Client(
options.clientOptions || { intents: Intents.FLAGS.GUILDS }
);
this.commands = new CommandManager(
options.prefix,
options.argumentSeparator
);
this.token = options.token || "";
this.token = options.token;
this.applicationId = options.applicationId;
this.messages = {
help: {
enabled: true,
@@ -70,11 +74,10 @@ export class Bot extends EventEmitter {
* @param {string} [token] - app token from Discord Developer Portal
* @returns *Promise<boolean>*
*/
async start(port?: number, token?: string): Promise<boolean> {
async start(port?: number): Promise<boolean> {
try {
console.log(`\nBot name: ${this.name}`);
console.log(`Prefix: ${this.commands.prefix} \n`);
this.token = token || this.token;
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'
@@ -96,8 +99,11 @@ export class Bot extends EventEmitter {
this.commands.add(helpMsg);
}
process.stdout.write("Connecting to Discord... ");
await this.client.login(this.token);
this.client.on("ready", () => {
this.client.login(this.token);
this.client.on("ready", async () => {
console.log("✔");
process.stdout.write("Registering commands... ");
console.log("✔\n");
this.emit("READY");
});
@@ -115,7 +121,7 @@ export class Bot extends EventEmitter {
user: m.member || undefined,
command: cmdMsg.command,
},
m.channel
m.channel as TextChannel
);
} else {
this.messages.system.send(
@@ -125,7 +131,7 @@ export class Bot extends EventEmitter {
user: m.member || undefined,
error: e,
},
m.channel
m.channel as TextChannel
);
console.error(e);
}
@@ -137,7 +143,7 @@ export class Bot extends EventEmitter {
this.messages.system.send(
"NOT_FOUND",
{ phrase: m.content, user: m.member || undefined },
m.channel
m.channel as TextChannel
);
} else {
this.emit("MESSAGE", m);
+6 -4
View File
@@ -1,5 +1,5 @@
//IMPORTS
import { Permissions, Message, MessageEmbed } from "discord.js";
import { Permissions, Message, MessageEmbed, Guild } from "discord.js";
import {
CommandBuilder,
PermissionCheckTypes,
@@ -18,6 +18,7 @@ export class Command {
usage: string;
permissionCheck: PermissionCheckTypes;
permissions: Permissions;
guilds: Guild[];
visible: boolean;
private function: (
message?: Message,
@@ -49,7 +50,8 @@ export class Command {
options.permissionCheck == "ALL" || options.permissionCheck == "ANY"
? options.permissionCheck
: "ANY";
this.permissions = new Permissions(options.permissions || 0);
this.permissions = new Permissions(options.permissions || undefined);
this.guilds = options.guilds || [];
this.visible = options.visible != undefined ? options.visible : true;
this.function = options.function;
}
@@ -62,7 +64,7 @@ export class Command {
*/
async start(message?: Message, cmdParams?: string[]): Promise<void> {
const memberPermissions: Readonly<Permissions> =
message?.member?.permissions || new Permissions(0);
message?.member?.permissions || new Permissions();
if (
this.permissionCheck == "ALL"
? memberPermissions.has(this.permissions, true)
@@ -85,7 +87,7 @@ export class Command {
if (typeof fnResult == "string") {
await message?.reply(fnResult);
} else if (fnResult instanceof MessageEmbed) {
await message?.channel.send(fnResult);
await message?.channel.send({ embeds: [fnResult] });
}
} else {
throw new PermissionsError(this, message?.member);
+2 -2
View File
@@ -12,13 +12,13 @@ export class HelpMessage extends Command {
super({
name: "help",
usage: params.usage,
permissions: 0,
permissions: undefined,
description: params.description,
parameters: [
{
name: "Command name",
optional: true,
type: "TEXT",
type: "string",
},
],
function: (_, a) => {
+4 -3
View File
@@ -10,7 +10,7 @@ export const ProcessArgument = (
type: ParameterType
): ParameterResolvable => {
switch (type) {
case "TEXT":
/* case "TEXT":
return a;
case "NUMBER":
const numF = parseFloat(a);
@@ -26,8 +26,9 @@ export const ProcessArgument = (
return false;
} else {
throw new ParameterTypeError(a, type);
}
}*/
default:
throw new TypeError("Invalied type specified");
return a;
//throw new TypeError("Invalied type specified");
}
};
+6 -4
View File
@@ -79,10 +79,10 @@ export class SystemMessageManager {
const embed = new MessageEmbed();
embed.setTitle(this[type].title);
if (this[type].bottomText)
embed.setDescription(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);
if (this[type].footer) embed.setFooter(this[type].footer || "");
if (data) {
switch (type) {
case "ERROR":
@@ -153,9 +153,11 @@ export class SystemMessageManager {
}
}
if (channel) {
const message = await channel.send(embed);
const message = await channel.send({ embeds: [embed] });
if (this.deleteTimeout != Infinity && message.deletable) {
await message.delete({ timeout: this.deleteTimeout });
setTimeout(async () => {
await message.delete();
}, this.deleteTimeout || 0);
}
return message;
} else {
+30 -4
View File
@@ -1,4 +1,13 @@
import { ClientOptions, ColorResolvable } from "discord.js";
import {
ClientOptions,
ColorResolvable,
Guild,
GuildMember,
NewsChannel,
Role,
TextChannel,
VoiceChannel,
} from "discord.js";
import { Parameter } from "./Parameter.js";
import { PermissionResolvable, Message, MessageEmbed } from "discord.js";
import type { Command } from "./Command.js";
@@ -8,7 +17,8 @@ export interface InitOptions {
prefix: string;
argumentSeparator?: string;
clientOptions?: ClientOptions;
token?: string;
token: string;
applicationId: string;
}
export type GetMode = "ALL" | "PREFIX" | "NO_PREFIX";
export type PermissionCheckTypes = "ALL" | "ANY";
@@ -21,6 +31,7 @@ export interface CommandBuilder {
usage?: string;
permissionCheck?: PermissionCheckTypes;
permissions?: PermissionResolvable;
guilds?: Guild[];
visible?: boolean;
function: (
message?: Message,
@@ -43,5 +54,20 @@ export interface HelpMessageParams {
description: string;
usage: string;
}
export type ParameterType = "TEXT" | "TRUE/FALSE" | "NUMBER";
export type ParameterResolvable = string | boolean | number;
export type ParameterType =
| "string"
| "boolean"
| "number"
| "user"
| "role"
| "channel"
| "mentionable";
export type ParameterResolvable =
| string
| boolean
| number
| GuildMember
| Role
| TextChannel
| VoiceChannel
| NewsChannel;