Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for installation and interaction contexts #86

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ allprojects {
version = versionInfo.values().join('.')

ext {
jdaVersion = '5.0.0-beta.7'
jdaVersion = 'c495b94'
slf4jVersion = '1.7.36'
okhttpVersion = '4.9.3'
findbugsVersion = '3.0.2'
jsonVersion = '20220320'
junitVersion = '4.13.1' // TODO Move to junit 5?

dependencies {
jda = { [group: 'net.dv8tion', name: 'JDA', version: jdaVersion] }
jda = { [group: 'com.github.freya022', name: 'JDA', version: jdaVersion] }
slf4j = { [group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion] }
okhttp = { [group: 'com.squareup.okhttp3', name: 'okhttp', version: okhttpVersion] }
findbugs = { [group: 'com.google.code.findbugs', name: 'jsr305', version: findbugsVersion] }
Expand Down Expand Up @@ -91,6 +91,12 @@ allprojects {

repositories {
mavenCentral()

// jitpack
maven {
name = 'jitpack'
url = 'https://jitpack.io'
}
}

build {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Middleware for child context menu types. Anything that extends this class will inherit the following options.
Expand Down Expand Up @@ -150,7 +154,29 @@ public CommandData buildCommandData()
else
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions));

data.setGuildOnly(this.guildOnly);
Set<InteractionContextType> contexts = getContexts();

// Check for guildOnly state.
if (this.guildOnly == null) {
// don't do anything
} else if (this.guildOnly) {
contexts.remove(InteractionContextType.BOT_DM);
} else if (!this.guildOnly) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure a simple } else { should work here given you already check for null and if it is true. And since it's a boolean can there only be false be left by the logic.

contexts.add(InteractionContextType.BOT_DM);
}

Set<IntegrationType> types = new HashSet<>();
// Mark as a user install if it's a private channel. Only users can access private channels.
if (contexts.contains(InteractionContextType.PRIVATE_CHANNEL)) {
types.add(IntegrationType.USER_INSTALL);
}
// Mark as a guild install if it's a guild or bot dm. Default behavior.
if (contexts.contains(InteractionContextType.BOT_DM) || contexts.contains(InteractionContextType.GUILD)) {
types.add(IntegrationType.GUILD_INSTALL);
}

data.setIntegrationTypes(types);
data.setContexts(contexts);

//Check name localizations
if (!getNameLocalization().isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
package com.jagrosh.jdautilities.command;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.interactions.InteractionContextType;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

/**
* A class that represents an interaction with a user.
Expand All @@ -30,8 +37,9 @@ public abstract class Interaction
* {@code true} if the command may only be used in a {@link net.dv8tion.jda.api.entities.Guild Guild},
* {@code false} if it may be used in both a Guild and a DM.
* <br>Default {@code true}.
* @deprecated In favor of
*/
protected boolean guildOnly = true;
protected Boolean guildOnly = null;

/**
* Any {@link Permission Permissions} a Member must have to use this interaction.
Expand Down Expand Up @@ -70,6 +78,11 @@ public abstract class Interaction
*/
protected CooldownScope cooldownScope = CooldownScope.USER;

/**
* The interaction context of this command.
*/
protected InteractionContextType[] contexts = new InteractionContextType[]{InteractionContextType.GUILD};

/**
* The permission message used when the bot does not have the required permission.
* Requires 3 "%s", first is user mention, second is the permission needed, third is type, e.g. server.
Expand All @@ -82,6 +95,13 @@ public abstract class Interaction
*/
protected String userMissingPermMessage = "%s You must have the %s permission in this %s to use that!";

/**
* {@code true} if the command may only be used in an NSFW {@link TextChannel} or DMs.
* {@code false} if it may be used anywhere
* <br>Default: {@code false}
*/
protected boolean nsfwOnly = false;

/**
* Gets the {@link Interaction#cooldown cooldown} for the Interaction.
*
Expand Down Expand Up @@ -131,4 +151,13 @@ public boolean isOwnerCommand()
{
return ownerCommand;
}

/**
* Returns the installation scope for this interaction.
*
* @return the installation scope for this interaction
*/
public Set<InteractionContextType> getContexts() {
return new HashSet<>(Arrays.asList(contexts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;

import java.util.HashSet;
import java.util.Set;

public abstract class MessageContextMenu extends ContextMenu
{
/**
Expand Down Expand Up @@ -174,7 +179,31 @@ public CommandData buildCommandData()
else
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions));

data.setGuildOnly(this.guildOnly);
Set<InteractionContextType> contexts = getContexts();

// Check for guildOnly state.
if (this.guildOnly == null) {
// don't do anything
} else if (this.guildOnly) {
contexts.remove(InteractionContextType.BOT_DM);
} else if (!this.guildOnly) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment.

contexts.add(InteractionContextType.BOT_DM);
}

Set<IntegrationType> types = new HashSet<>();
// Mark as a user install if it's a private channel. Only users can access private channels.
if (contexts.contains(InteractionContextType.PRIVATE_CHANNEL)) {
types.add(IntegrationType.USER_INSTALL);
}
// Mark as a guild install if it's a guild or bot dm. Default behavior.
if (contexts.contains(InteractionContextType.BOT_DM) || contexts.contains(InteractionContextType.GUILD)) {
types.add(IntegrationType.GUILD_INSTALL);
}

data.setIntegrationTypes(types);
data.setContexts(contexts);

data.setNSFW(this.nsfwOnly);

return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.DiscordLocale;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* <h2><b>Slash Commands In JDA-Chewtils</b></h2>
Expand Down Expand Up @@ -298,11 +302,6 @@ else if(!selfMember.hasPermission(vc, p))
return;
}
}
else if(guildOnly)
{
terminate(event, client.getError()+" This command cannot be used in direct messages", client);
return;
}

// cooldown check, ignoring owner
if(cooldown>0 && !(isOwner(event, client)))
Expand Down Expand Up @@ -470,7 +469,31 @@ public CommandData buildCommandData()
else
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.getUserPermissions()));

data.setGuildOnly(this.guildOnly);
data.setNSFW(this.nsfwOnly);

Set<InteractionContextType> contexts = getContexts();

// Check for guildOnly state.
if (this.guildOnly == null) {
// don't do anything
} else if (this.guildOnly) {
contexts.remove(InteractionContextType.BOT_DM);
} else if (!this.guildOnly) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See first comment.

contexts.add(InteractionContextType.BOT_DM);
}

Set<IntegrationType> types = new HashSet<>();
// Mark as a user install if it's a private channel. Only users can access private channels.
if (contexts.contains(InteractionContextType.PRIVATE_CHANNEL)) {
types.add(IntegrationType.USER_INSTALL);
}
// Mark as a guild install if it's a guild or bot dm. Default behavior.
if (contexts.contains(InteractionContextType.BOT_DM) || contexts.contains(InteractionContextType.GUILD)) {
types.add(IntegrationType.GUILD_INSTALL);
}

data.setIntegrationTypes(types);
data.setContexts(contexts);

return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import net.dv8tion.jda.api.entities.GuildVoiceState;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.InteractionContextType;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;

import java.util.HashSet;
import java.util.Set;

/**
* <h2><b>User Context Menus In JDA-Chewtils</b></h2>
*
Expand Down Expand Up @@ -212,7 +217,40 @@ public CommandData buildCommandData()
else
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions));

data.setGuildOnly(this.guildOnly);
Set<InteractionContextType> contexts = getContexts();

// manually set to true
if (this.guildOnly == null) {
// do nothing!!! nothing!!!!
} else if (this.guildOnly) {
// remove bot dm from contexts
contexts.remove(InteractionContextType.BOT_DM);
} else if (!this.guildOnly) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blablabla first comment.

contexts.add(InteractionContextType.BOT_DM);
}
data.setNSFW(this.nsfwOnly);

// Check for guildOnly state.
if (this.guildOnly == null) {
// don't do anything
} else if (this.guildOnly) {
contexts.remove(InteractionContextType.BOT_DM);
} else if (!this.guildOnly) {
contexts.add(InteractionContextType.BOT_DM);
}

Set<IntegrationType> types = new HashSet<>();
// Mark as a user install if it's a private channel. Only users can access private channels.
if (contexts.contains(InteractionContextType.PRIVATE_CHANNEL)) {
types.add(IntegrationType.USER_INSTALL);
}
// Mark as a guild install if it's a guild or bot dm. Default behavior.
if (contexts.contains(InteractionContextType.BOT_DM) || contexts.contains(InteractionContextType.GUILD)) {
types.add(IntegrationType.GUILD_INSTALL);
}

data.setIntegrationTypes(types);
data.setContexts(contexts);

return data;
}
Expand Down
Loading