Add DiscordService#getRole & DiscordService#modifyMemberRoles

This commit is contained in:
Josh Roy 2021-08-03 23:19:56 -04:00 committed by MD
parent 376abc4aab
commit 80bbf8c55b
4 changed files with 130 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
/**
@ -56,6 +57,22 @@ public interface DiscordService {
*/
CompletableFuture<InteractionMember> getMemberById(final String id);
/**
* Gets an {@link InteractionRole} by its Discord ID.
* @param id The ID of the role to look up.
* @return the role or null if none by that ID exists.
*/
InteractionRole getRole(final String id);
/**
* Adds or removes {@link InteractionRole roles} to the given {@link InteractionMember}.
* @param member The member to add/remove roles to/from.
* @param addRoles The roles to add to the {@link InteractionMember member}, or null to add none.
* @param removeRoles The roles to remove from the {@link InteractionMember member}, or null to remove none.
* @return A future which will complete when all requests operations have been completed.
*/
CompletableFuture<Void> modifyMemberRoles(final InteractionMember member, final Collection<InteractionRole> addRoles, final Collection<InteractionRole> removeRoles);
/**
* Gets unstable API that is subject to change at any time.
* @return {@link Unsafe the unsafe} instance.

View File

@ -0,0 +1,36 @@
package net.essentialsx.api.v2.services.discord;
/**
* Represents a role of an interaction member.
*/
public interface InteractionRole {
/**
* Gets the name of this role.
* @return this role's name.
*/
String getName();
/**
* Whether this role is mentionable.
* @return true if the role is mentionable.
*/
boolean isManaged();
/**
* Gets the raw RGB color value of this role.
* @return this role's color value.
*/
int getColorRaw();
/**
* Whether this role's color is the default one (has no color).
* @return true if the role has no color.
*/
boolean isDefaultColor();
/**
* Gets the ID of this role.
* @return this role's ID.
*/
String getId();
}

View File

@ -13,6 +13,7 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.Webhook;
import net.dv8tion.jda.api.events.ShutdownEvent;
@ -27,10 +28,12 @@ import net.essentialsx.api.v2.services.discord.DiscordService;
import net.essentialsx.api.v2.services.discord.InteractionController;
import net.essentialsx.api.v2.services.discord.InteractionException;
import net.essentialsx.api.v2.services.discord.InteractionMember;
import net.essentialsx.api.v2.services.discord.InteractionRole;
import net.essentialsx.api.v2.services.discord.MessageType;
import net.essentialsx.api.v2.services.discord.Unsafe;
import net.essentialsx.discord.interactions.InteractionControllerImpl;
import net.essentialsx.discord.interactions.InteractionMemberImpl;
import net.essentialsx.discord.interactions.InteractionRoleImpl;
import net.essentialsx.discord.interactions.commands.ExecuteCommand;
import net.essentialsx.discord.interactions.commands.ListCommand;
import net.essentialsx.discord.interactions.commands.MessageCommand;
@ -48,7 +51,10 @@ import org.bukkit.plugin.ServicePriority;
import org.jetbrains.annotations.NotNull;
import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
@ -481,6 +487,36 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
return future;
}
@Override
public InteractionRole getRole(String id) {
final Role role = getGuild().getRoleById(id);
return role == null ? null : new InteractionRoleImpl(role);
}
@Override
public CompletableFuture<Void> modifyMemberRoles(InteractionMember member, Collection<InteractionRole> addRoles, Collection<InteractionRole> removeRoles) {
if ((addRoles == null || addRoles.isEmpty()) && (removeRoles == null || removeRoles.isEmpty())) {
return CompletableFuture.completedFuture(null);
}
final List<Role> add = new ArrayList<>();
final List<Role> remove = new ArrayList<>();
if (addRoles != null) {
for (final InteractionRole role : addRoles) {
add.add(((InteractionRoleImpl) role).getJdaObject());
}
}
if (removeRoles != null) {
for (final InteractionRole role : removeRoles) {
remove.add(((InteractionRoleImpl) role).getJdaObject());
}
}
final CompletableFuture<Void> future = new CompletableFuture<>();
guild.modifyMemberRoles(((InteractionMemberImpl) member).getJdaObject(), add, remove).queue(future::complete);
return future;
}
public JDA getJda() {
return jda;
}

View File

@ -0,0 +1,41 @@
package net.essentialsx.discord.interactions;
import net.dv8tion.jda.api.entities.Role;
import net.essentialsx.api.v2.services.discord.InteractionRole;
public class InteractionRoleImpl implements InteractionRole {
private final Role role;
public InteractionRoleImpl(Role role) {
this.role = role;
}
@Override
public String getName() {
return role.getName();
}
@Override
public boolean isManaged() {
return role.isManaged();
}
@Override
public int getColorRaw() {
return role.getColorRaw();
}
@Override
public boolean isDefaultColor() {
return role.getColorRaw() == Role.DEFAULT_COLOR_RAW;
}
public Role getJdaObject() {
return role;
}
@Override
public String getId() {
return role.getId();
}
}