Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Added dmOnly option for commands #312

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/commands/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Command {
* @property {string} [details] - A detailed description of the command and its functionality
* @property {string[]} [examples] - Usage examples of the command
* @property {boolean} [guildOnly=false] - Whether or not the command should only function in a guild channel
* @property {boolean} [dmOnly=false] - Whether or not the command should only function in a dm channel
* @property {boolean} [ownerOnly=false] - Whether or not the command is usable only by an owner
* @property {PermissionResolvable[]} [clientPermissions] - Permissions required by the client to use the command.
* @property {PermissionResolvable[]} [userPermissions] - Permissions required by the user to use the command.
Expand Down Expand Up @@ -133,6 +134,12 @@ class Command {
*/
this.guildOnly = Boolean(info.guildOnly);

/**
* Whether the command can only be run in a dm channel
* @type {boolean}
*/
this.dmOnly = Boolean(info.dmOnly);

/**
* Whether the command can only be used by an owner
* @type {boolean}
Expand Down Expand Up @@ -305,6 +312,8 @@ class Command {
switch(reason) {
case 'guildOnly':
return message.reply(`The \`${this.name}\` command must be used in a server channel.`);
case 'dmOnly':
return message.reply(`The \`${this.name}\` command must be used in a dm channel`);
case 'nsfw':
return message.reply(`The \`${this.name}\` command can only be used in NSFW channels.`);
case 'permission': {
Expand Down Expand Up @@ -419,7 +428,8 @@ class Command {
*/
isUsable(message = null) {
if(!message) return this._globalEnabled;
if(this.guildOnly && message && !message.guild) return false;
if(this.guildOnly && !message.guild) return false;
if(this.dmOnly && message.channel.type !== 'dm') return false;
const hasPermission = this.hasPermission(message);
return this.isEnabledIn(message.guild) && hasPermission && typeof hasPermission !== 'string';
}
Expand Down
6 changes: 5 additions & 1 deletion src/extensions/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ module.exports = Structures.extend('Message', Message => {
* @event CommandoClient#commandBlock
* @param {CommandoMessage} message - Command message that the command is running from
* @param {string} reason - Reason that the command was blocked
* (built-in reasons are `guildOnly`, `nsfw`, `permission`, `throttling`, and `clientPermissions`)
* (built-in reasons are `guildOnly`, `dmOnly`, `nsfw`, `permission`, `throttling`, and `clientPermissions`)
* @param {Object} [data] - Additional data associated with the block. Built-in reason data properties:
* - guildOnly: none
* - nsfw: none
Expand All @@ -149,6 +149,10 @@ module.exports = Structures.extend('Message', Message => {
this.client.emit('commandBlock', this, 'guildOnly');
return this.command.onBlock(this, 'guildOnly');
}
if(this.command.dmOnly && this.channel.type !== 'dm') {
this.client.emit('commandBlock', this, 'dmOnly');
return this.command.onBlock(this, 'dmOnly');
}

// Ensure the channel is a NSFW one if required
if(this.command.nsfw && !this.channel.nsfw) {
Expand Down
10 changes: 6 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'discord.js-commando' {
import { Channel, Client, ClientOptions, Collection, DMChannel, Emoji, Guild, GuildChannel, GuildMember, GuildResolvable, Message, MessageAttachment, MessageEmbed, MessageMentions, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, ReactionEmoji, Role, Snowflake, StringResolvable, TextChannel, User, UserResolvable, VoiceState, Webhook } from 'discord.js';
import { Channel, Client, ClientOptions, Collection, DMChannel, Emoji, Guild, GuildChannel, GuildMember, GuildResolvable, Message, MessageAttachment, MessageEmbed, MessageMentions, MessageOptions, MessageAdditions, MessageReaction, PermissionResolvable, PermissionString, ReactionEmoji, Role, Snowflake, StringResolvable, TextChannel, User, UserResolvable, VoiceState, Webhook } from "discord.js";

export class Argument {
private constructor(client: CommandoClient, info: ArgumentInfo);
Expand Down Expand Up @@ -77,7 +77,8 @@ declare module 'discord.js-commando' {
public groupID: string;
public guarded: boolean;
public hidden: boolean;
public guildOnly: boolean;
public guildOnly: boolean
public dmOnly: boolean;
public memberName: string;
public name: string;
public nsfw: boolean;
Expand All @@ -91,7 +92,7 @@ declare module 'discord.js-commando' {
public isEnabledIn(guild: GuildResolvable, bypassGroup?: boolean): boolean;
public isUsable(message: Message): boolean;
public onBlock(message: CommandoMessage, reason: string, data?: Object): Promise<Message | Message[]>;
public onBlock(message: CommandoMessage, reason: 'guildOnly' | 'nsfw'): Promise<Message | Message[]>;
public onBlock(message: CommandoMessage, reason: 'dmOnly' | 'guildOnly' | 'nsfw'): Promise<Message | Message[]>;
public onBlock(message: CommandoMessage, reason: 'permission', data: { response?: string }): Promise<Message | Message[]>;
public onBlock(message: CommandoMessage, reason: 'clientPermissions', data: { missing: PermissionString[] }): Promise<Message | Message[]>;
public onBlock(message: CommandoMessage, reason: 'throttling', data: { throttle: Object, remaining: number }): Promise<Message | Message[]>;
Expand Down Expand Up @@ -229,7 +230,7 @@ declare module 'discord.js-commando' {

on(event: string, listener: Function): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: string, data?: Object) => void): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: 'guildOnly' | 'nsfw') => void): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: 'dmOnly' |'guildOnly' | 'nsfw') => void): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: 'permission', data: { response?: string }) => void): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: 'throttling', data: { throttle: Object, remaining: number }) => void): this;
on(event: 'commandBlock', listener: (message: CommandoMessage, reason: 'clientPermissions', data: { missing: string }) => void): this;
Expand Down Expand Up @@ -470,6 +471,7 @@ declare module 'discord.js-commando' {
examples?: string[];
nsfw?: boolean;
guildOnly?: boolean;
dmOnly?: boolean;
ownerOnly?: boolean;
clientPermissions?: PermissionResolvable[];
userPermissions?: PermissionResolvable[];
Expand Down