Skip to content

Commit

Permalink
initial support for installation and interaction contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed May 17, 2024
1 parent a86d4de commit 52caca3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
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,15 @@
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.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.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 +152,17 @@ 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) {
contexts.add(InteractionContextType.BOT_DM);
}

//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 Down Expand Up @@ -131,4 +144,13 @@ public boolean isOwnerCommand()
{
return ownerCommand;
}

/**
* Returns the installation scope for this interaction.
*
* @return
*/
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,13 @@
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.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.Set;

public abstract class MessageContextMenu extends ContextMenu
{
/**
Expand Down Expand Up @@ -174,7 +177,18 @@ 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) {
contexts.add(InteractionContextType.BOT_DM);
}
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 @@ -470,7 +474,29 @@ public CommandData buildCommandData()
else
data.setDefaultPermissions(DefaultMemberPermissions.enabledFor(this.getUserPermissions()));

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

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) {
contexts.add(InteractionContextType.BOT_DM);
}

Set<IntegrationType> types = new HashSet<>();
if (contexts.contains(InteractionContextType.PRIVATE_CHANNEL)) {
types.add(IntegrationType.USER_INSTALL);
} else 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,13 @@
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.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.Set;

/**
* <h2><b>User Context Menus In JDA-Chewtils</b></h2>
*
Expand Down Expand Up @@ -212,7 +215,18 @@ 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) {
contexts.add(InteractionContextType.BOT_DM);
}
data.setNSFW(this.nsfwOnly);

return data;
}
Expand Down

0 comments on commit 52caca3

Please sign in to comment.