diff --git a/build.gradle b/build.gradle index ff97915c..42aefa17 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,7 @@ 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' @@ -44,7 +44,7 @@ allprojects { 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] } @@ -91,6 +91,12 @@ allprojects { repositories { mavenCentral() + + // jitpack + maven { + name = 'jitpack' + url = 'https://jitpack.io' + } } build { diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java index 55749354..cfb8b182 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java @@ -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. @@ -150,7 +154,29 @@ public CommandData buildCommandData() else data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions)); - data.setGuildOnly(this.guildOnly); + Set 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) { + contexts.add(InteractionContextType.BOT_DM); + } + + Set 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()) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/Interaction.java b/command/src/main/java/com/jagrosh/jdautilities/command/Interaction.java index c87370e2..8c0906f4 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/Interaction.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/Interaction.java @@ -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. @@ -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. *
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. @@ -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. @@ -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 + *
Default: {@code false} + */ + protected boolean nsfwOnly = false; + /** * Gets the {@link Interaction#cooldown cooldown} for the Interaction. * @@ -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 getContexts() { + return new HashSet<>(Arrays.asList(contexts)); + } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java index 722ab772..4e410d79 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java @@ -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 { /** @@ -174,7 +179,31 @@ public CommandData buildCommandData() else data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions)); - data.setGuildOnly(this.guildOnly); + Set 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) { + contexts.add(InteractionContextType.BOT_DM); + } + + Set 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; } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java index 554ab033..5d5929f2 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java @@ -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; /** *

Slash Commands In JDA-Chewtils

@@ -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))) @@ -470,7 +469,31 @@ public CommandData buildCommandData() else data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.getUserPermissions())); - data.setGuildOnly(this.guildOnly); + data.setNSFW(this.nsfwOnly); + + Set 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) { + contexts.add(InteractionContextType.BOT_DM); + } + + Set 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; } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java index b9072b78..59d534fa 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java @@ -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; + /** *

User Context Menus In JDA-Chewtils

* @@ -212,7 +217,40 @@ public CommandData buildCommandData() else data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.userPermissions)); - data.setGuildOnly(this.guildOnly); + Set 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) { + 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 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; }