Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Allow Terminations To Reply In Dms #85

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
*/
package com.jagrosh.jdautilities.command;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Predicate;

/**
* <h1><b>Commands In JDA-Utilities</b></h1>
*
Expand Down Expand Up @@ -155,6 +156,12 @@ public abstract class Command
*/
protected boolean hidden = false;

/**
* {@code true} if this command should reply in dms when terminated.
* <br>Default {@code false}
*/
protected boolean terminateInDms = false;

/**
* The {@link com.jagrosh.jdautilities.command.Command.CooldownScope CooldownScope}
* of the command. This defines how far of a scope cooldowns have.
Expand Down Expand Up @@ -531,10 +538,25 @@ public boolean isHidden()
return hidden;
}

/**
* Checks whether or not this command should reply in dms upon termination
*
* @return {@code true} if the command should reply in dms, otherwise {@code false}
*/
public boolean isTerminatingInDms()
{
return terminateInDms;
}

private void terminate(CommandEvent event, String message)
{
if(message!=null)
event.reply(message);
if (terminateInDms) {
event.replyInDm(message);
} else {
event.reply(message);
}

if(event.getClient().getListener()!=null)
event.getClient().getListener().onTerminatedCommand(event, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package com.jagrosh.jdautilities.command;

import com.jagrosh.jdautilities.command.Command.*;

import com.jagrosh.jdautilities.command.Command.Category;
import com.jagrosh.jdautilities.command.Command.CooldownScope;
import net.dv8tion.jda.core.Permission;

import java.util.Collection;
Expand Down Expand Up @@ -56,6 +56,7 @@ public class CommandBuilder
private boolean usesTopicTags = true;
private CooldownScope cooldownScope = CooldownScope.USER;
private boolean hidden = false;
private boolean terminateInDms = false;

/**
* Sets the {@link com.jagrosh.jdautilities.command.Command#name name}
Expand Down Expand Up @@ -450,6 +451,23 @@ public CommandBuilder setHidden(boolean hidden)
return this;
}

/**
* Sets the Command built to {@link com.jagrosh.jdautilities.command.Command#terminateInDms when
* an error occurs.
*
*
* @param terminateInDms
* {@code true} if the Command built is replying in dms upon termination, {@code false} if it will not.
*
* @return This CommandBuilder
*/

public CommandBuilder setTerminatingInDms(boolean terminateInDms)
{
this.terminateInDms = terminateInDms;
return this;
}

/**
* Builds the {@link com.jagrosh.jdautilities.command.Command Command}
* using the previously provided information.
Expand Down Expand Up @@ -492,7 +510,7 @@ public Command build(BiConsumer<Command,CommandEvent> execution)
guildOnly, requiredRole, ownerCommand, cooldown,
userPermissions, botPermissions, aliases.toArray(new String[aliases.size()]),
children.toArray(new Command[children.size()]), helpBiConsumer, usesTopicTags,
cooldownScope, hidden)
cooldownScope, hidden, terminateInDms)
{
@Override
protected void execute(CommandEvent event)
Expand All @@ -509,7 +527,7 @@ private abstract class BlankCommand extends Command
boolean ownerCommand, int cooldown, Permission[] userPermissions,
Permission[] botPermissions, String[] aliases, Command[] children,
BiConsumer<CommandEvent, Command> helpBiConsumer,
boolean usesTopicTags, CooldownScope cooldownScope, boolean hidden)
boolean usesTopicTags, CooldownScope cooldownScope, boolean hidden, boolean terminateInDms)
{
this.name = name;
this.help = help;
Expand All @@ -527,6 +545,7 @@ private abstract class BlankCommand extends Command
this.usesTopicTags = usesTopicTags;
this.cooldownScope = cooldownScope;
this.hidden = hidden;
this.terminateInDms = terminateInDms;
}
}
}