Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
feat(Command): add user and bot permission checking
Browse files Browse the repository at this point in the history
  • Loading branch information
alexthemaster committed Jul 26, 2020
1 parent 4ba7437 commit b4e2e56
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/events/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ export default class extends Event {
if (cmd.nsfw && message.channel.type !== 'dm' && !message.channel.nsfw) return message.channel.send('This command can only be used in NSFW chnanels.');


if (message.guild) {
const missingPerms = message.member?.permissionsIn(message.channel).missing(cmd.requiredPermissions);
const missingBotPerms = message.guild.me?.permissionsIn(message.channel).missing(cmd.requiredBotPermissions);

if (missingBotPerms?.length) return message.channel.send(`Insufficient bot permissions, missing: \`${missingBotPerms.map(perm => FriendlyPermission[perm]).join(', ')}\``);
if (missingPerms?.length) return message.channel.send(`Insufficient user permissions, missing: \`${missingPerms.map(perm => FriendlyPermission[perm]).join(', ')}\``);
}

// We parse the flags
while (args.some(arg => arg.startsWith('--'))) {
const find = args.find(arg => arg.startsWith('--'));
Expand All @@ -98,4 +106,38 @@ export default class extends Event {

cmd.run(message, parsedArgs);
}
}

enum FriendlyPermission {
CREATE_INSTANT_INVITE = "Create Invite",
KICK_MEMBERS = "Kick Members",
BAN_MEMBERS = "Ban Members",
ADMINISTRATOR = "Administrator",
MANAGE_CHANNELS = "Manage Channels",
MANAGE_GUILD = "Manage Server",
ADD_REACTIONS = "Add Reactions",
VIEW_AUDIT_LOG = "View Audit Log",
PRIORITY_SPEAKER = "Priority Speaker",
STREAM = "Video",
VIEW_CHANNEL = "Read Messages",
SEND_MESSAGES = "Send Messages",
SEND_TTS_MESSAGES = "Send TTS Messages",
MANAGE_MESSAGES = "Manage Messages",
EMBED_LINKS = "Embed Links",
ATTACH_FILES = "Attach Files",
READ_MESSAGE_HISTORY = "Read Message History",
MENTION_EVERYONE = "Mention @everyone, @here, and All Roles",
USE_EXTERNAL_EMOJIS = "Use External Emojis",
VIEW_GUILD_INSIGHTS = "View Guild Insights",
CONNECT = "Connect",
SPEAK = "Speak",
MUTE_MEMBERS = "Mute Members",
DEAFEN_MEMBERS = "Deafen Members",
MOVE_MEMBERS = "Move Members",
USE_VAD = "Use Voice Activity",
CHANGE_NICKNAME = "Change Nickname",
MANAGE_NICKNAMES = "Manage Nicknames",
MANAGE_ROLES = "Manage Roles",
MANAGE_WEBHOOKS = "Manage Webhooks",
MANAGE_EMOJIS = "Manage Emojis"
}
12 changes: 11 additions & 1 deletion src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MoonlightClient } from "../..";
import { BasePool } from './Pools/Base/BasePool';
import { BasePiece, BasePieceOptions } from './BasePiece';
import { Message } from "discord.js";
import { Message, PermissionString } from "discord.js";

/**
* @class
Expand All @@ -22,6 +22,10 @@ export class Command extends BasePiece<Command> {
public readonly usage: string;
/** The usage delimiter */
public readonly usageDelim: string | undefined;
/** The permissions the user is required to have to run the command */
public readonly requiredPermissions: PermissionString[];
/** The permissions the bot requires to run the command */
public readonly requiredBotPermissions: PermissionString[];
/** The map containing the customized responses */
public readonly customizedResponses: Map<string, string> = new Map<string, string>();
/** The flags provided by the user when running the command */
Expand All @@ -45,6 +49,8 @@ export class Command extends BasePiece<Command> {
this.ownerOnly = options?.ownerOnly ?? false;
this.usage = options?.usage ?? '';
this.usageDelim = options?.usageDelim ?? undefined;
this.requiredPermissions = options?.requiredPermissions ?? new Array();
this.requiredBotPermissions = options?.requiredBotPermissions ?? new Array();
}
}

Expand All @@ -63,4 +69,8 @@ interface CommandOptions extends BasePieceOptions {
usage?: string;
/** The usage delimiter */
usageDelim?: string | undefined;
/** The permissions the user is required to have to run the command */
requiredPermissions?: PermissionString[];
/** The permissions the bot requires to run the command */
requiredBotPermissions?: PermissionString[];
}

0 comments on commit b4e2e56

Please sign in to comment.