Skip to content

Commit

Permalink
Adds support for version 3.5.0 of Javacord.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShindouMihou authored Jun 21, 2022
2 parents d07e430 + a534405 commit ba32b20
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 740 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<dependency>
<groupId>org.javacord</groupId>
<artifactId>javacord</artifactId>
<version>3.4.0</version>
<version>3.5.0</version>
<type>pom</type>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pw.mihou.nexus.features.command.core;

import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.interaction.DiscordLocale;
import org.javacord.api.interaction.SlashCommandOption;
import pw.mihou.nexus.core.NexusCore;
import pw.mihou.nexus.core.reflective.annotations.*;
Expand Down Expand Up @@ -28,9 +30,15 @@ public class NexusCommandCore implements NexusCommand {
@Required
public String name;

@WithDefault
public Map<DiscordLocale, String> nameLocalizations = Collections.emptyMap();

@Required
public String description;

@WithDefault
public Map<DiscordLocale, String> descriptionLocalizations = Collections.emptyMap();

@WithDefault
public List<SlashCommandOption> options = Collections.emptyList();

Expand All @@ -47,7 +55,16 @@ public class NexusCommandCore implements NexusCommand {
public List<Long> serverIds = new ArrayList<>();

@WithDefault
public boolean defaultPermission = true;
public boolean defaultEnabledForEveryone = true;

@WithDefault
public boolean enabledInDms = true;

@WithDefault
public boolean defaultDisabled = false;

@WithDefault
public List<PermissionType> defaultEnabledForPermissions = Collections.emptyList();

@InjectNexusCore
public NexusCore core;
Expand Down Expand Up @@ -112,8 +129,33 @@ public Optional<Object> get(String field) {
}

@Override
public boolean isDefaultPermissionEnabled() {
return defaultPermission;
public boolean isDefaultEnabledForEveryone() {
return defaultEnabledForEveryone;
}

@Override
public boolean isEnabledInDms() {
return enabledInDms;
}

@Override
public boolean isDefaultDisabled() {
return defaultDisabled;
}

@Override
public List<PermissionType> getDefaultEnabledForPermissions() {
return defaultEnabledForPermissions;
}

@Override
public Map<DiscordLocale, String> getNameLocalizations() {
return nameLocalizations;
}

@Override
public Map<DiscordLocale, String> getDescriptionLocalizations() {
return descriptionLocalizations;
}

@Override
Expand Down
105 changes: 95 additions & 10 deletions src/main/java/pw/mihou/nexus/features/command/facade/NexusCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package pw.mihou.nexus.features.command.facade;

import org.javacord.api.interaction.SlashCommand;
import org.javacord.api.interaction.SlashCommandBuilder;
import org.javacord.api.interaction.SlashCommandOption;
import org.javacord.api.interaction.SlashCommandUpdater;
import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.interaction.*;
import pw.mihou.nexus.commons.Pair;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface NexusCommand {
Expand Down Expand Up @@ -90,12 +89,67 @@ public interface NexusCommand {
Optional<Object> get(String field);

/**
* Is the default permission configuration of Discord enabled?
* Checks whether the command is default enabled for everyone or not.
*
* @return Whether or not the default permission configuration of Discord is
* enabled.
* @return Whether this command is default enabled for everyone or not.
*/
boolean isDefaultPermissionEnabled();
boolean isDefaultEnabledForEveryone();

/**
* Checks whether the command is enabled in DMs or not.
*
* @return Whether the command is enabled in DMs or not.
*/
boolean isEnabledInDms();

/**
* Checks whether the command is default disabled or not.
*
* @return Whether the command is default disabled or not.
*/
boolean isDefaultDisabled();

/**
* Gets the permission types required to have this command enabled for that user.
*
* @return The permission types required for this command to be enabled for
* that specific user.
*/
List<PermissionType> getDefaultEnabledForPermissions();

/**
* Gets all the names in different localizations for this slash command.
*
* @return All the name localizations of this command.
*/
Map<DiscordLocale, String> getNameLocalizations();

/**
* Gets all the description in different localizations for this slash command.
*
* @return All the description localizations of this command.
*/
Map<DiscordLocale, String> getDescriptionLocalizations();

/**
* Gets the description localized value for the given localization for this slash command.
*
* @param locale The locale to get from.
* @return The localized description for this slash command, if present.
*/
default Optional<String> getDescriptionLocalization(DiscordLocale locale) {
return Optional.ofNullable(getDescriptionLocalizations().get(locale));
}

/**
* Gets the name localized value for the given localization for this slash command.
*
* @param locale The locale to get from.
* @return The localized name for this slash command, if present.
*/
default Optional<String> getNameLocalization(DiscordLocale locale) {
return Optional.ofNullable(getDescriptionLocalizations().get(locale));
}

/**
* Gets the server id of the command.
Expand All @@ -114,7 +168,22 @@ public interface NexusCommand {
*/
default SlashCommandBuilder asSlashCommand() {
SlashCommandBuilder builder = SlashCommand.with(getName().toLowerCase(), getDescription())
.setDefaultPermission(isDefaultPermissionEnabled());
.setEnabledInDms(isEnabledInDms());

getNameLocalizations().forEach(builder::addNameLocalization);
getDescriptionLocalizations().forEach(builder::addDescriptionLocalization);

if (isDefaultDisabled()) {
builder.setDefaultDisabled();
}

if (isDefaultEnabledForEveryone() && !isDefaultDisabled()) {
builder.setDefaultEnabledForEveryone();
}

if (!getDefaultEnabledForPermissions().isEmpty()) {
builder.setDefaultEnabledForPermissions(getDefaultEnabledForPermissions().toArray(PermissionType[]::new));
}

if (!getOptions().isEmpty()) {
return builder.setOptions(getOptions());
Expand All @@ -135,7 +204,23 @@ default SlashCommandUpdater asSlashCommandUpdater(long commandId) {
SlashCommandUpdater updater = new SlashCommandUpdater(commandId)
.setName(getName())
.setDescription(getDescription())
.setDefaultPermission(isDefaultPermissionEnabled());
.setEnabledInDms(isEnabledInDms());

getNameLocalizations().forEach(updater::addNameLocalization);
getDescriptionLocalizations().forEach(updater::addDescriptionLocalization);

if (isDefaultDisabled()) {
updater.setDefaultDisabled();
}

if (isDefaultEnabledForEveryone() && !isDefaultDisabled()) {
updater.setDefaultEnabledForEveryone();
}

if (!getDefaultEnabledForPermissions().isEmpty()) {
updater.setDefaultEnabledForPermissions(getDefaultEnabledForPermissions().toArray(PermissionType[]::new));
}


if(!getOptions().isEmpty()) {
updater.setSlashCommandOptions(getOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.channel.ServerTextChannel;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageFlag;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.interaction.SlashCommandCreateEvent;
import org.javacord.api.interaction.SlashCommandInteraction;
import org.javacord.api.interaction.SlashCommandInteractionOption;
import org.javacord.api.interaction.callback.InteractionCallbackDataFlag;
import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder;
import org.javacord.api.interaction.callback.InteractionOriginalResponseUpdater;
import pw.mihou.nexus.Nexus;
Expand Down Expand Up @@ -172,13 +172,13 @@ default InteractionImmediateResponseBuilder respondNow() {

/**
* Gets the immediate response builder for this command and adds the
* {@link InteractionCallbackDataFlag#EPHEMERAL} flag ahead of time.
* {@link MessageFlag#EPHEMERAL} flag ahead of time.
*
* @return The immediate response builder associated with this command with the
* ephemeral flag added.
*/
default InteractionImmediateResponseBuilder respondNowAsEphemeral() {
return respondNow().setFlags(InteractionCallbackDataFlag.EPHEMERAL);
return respondNow().setFlags(MessageFlag.EPHEMERAL);
}

/**
Expand Down
Loading

0 comments on commit ba32b20

Please sign in to comment.