mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-07 08:33:59 +00:00
[AB-47] adding feature to define custom actions once members reach a certain level
This commit is contained in:
@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.core.service;
|
||||
import dev.sheldan.abstracto.core.models.database.AChannel;
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.*;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
|
||||
@@ -11,6 +12,7 @@ import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
||||
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -79,4 +81,7 @@ public interface ChannelService {
|
||||
CompletableFuture<Void> setSlowModeInChannel(TextChannel textChannel, Integer seconds);
|
||||
List<CompletableFuture<Message>> sendFileToChannel(String fileContent, String fileNameTemplate, String messageTemplate, Object model, MessageChannel channel);
|
||||
List<CompletableFuture<Message>> sendFileToChannel(String fileContent, String fileName, MessageChannel channel);
|
||||
|
||||
CompletableFuture<Void> addMemberViewToChannel(Guild guild, Long channelId, Long memberId, Collection<Permission> permissions);
|
||||
CompletableFuture<Void> removeChannelOverrideForMember(Guild guild, Long channelId, Long memberId);
|
||||
}
|
||||
|
||||
@@ -17,12 +17,14 @@ public interface RoleService {
|
||||
CompletableFuture<Void> addRoleToUserAsync(AUserInAServer aUserInAServer, ARole role);
|
||||
CompletableFuture<Void> addRoleToMemberAsync(Member member, Long roleId);
|
||||
CompletableFuture<Void> updateRolesIds(Member member, List<Long> rolesToRemove, List<Long> rolesToAdd);
|
||||
CompletableFuture<Void> updateRolesIds(AUserInAServer aUserInAServer, List<Long> rolesToAdd, List<Long> rolesToRemove);
|
||||
CompletableFuture<Void> updateRolesObj(Member member, List<Role> rolesToRemove, List<Role> rolesToAdd);
|
||||
CompletableFuture<Void> addRoleToMemberAsync(Member member, Role role);
|
||||
void addRoleToMember(Member member, ARole role);
|
||||
CompletableFuture<Void> addRoleToMemberAsync(Member member, ARole role);
|
||||
void removeRoleFromMember(Member member, ARole role);
|
||||
CompletableFuture<Void> removeRoleFromMemberAsync(Member member, ARole role);
|
||||
CompletableFuture<Void> removeRoleFromMemberAsync(AUserInAServer user, ARole role);
|
||||
CompletableFuture<Void> removeRoleFromMemberAsync(Member member, Long roleId);
|
||||
CompletableFuture<Void> addRoleToMemberAsync(Guild guild, Long userId, Role roleById);
|
||||
CompletableFuture<Void> removeRoleFromUserAsync(Guild guild, Long userId, Role roleById);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
import dev.sheldan.abstracto.core.Prioritized;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@NoArgsConstructor
|
||||
public class BeanUtils {
|
||||
|
||||
public static <T extends Prioritized> void sortPrioritizedListeners(List<T> prioritized) {
|
||||
if(prioritized != null) {
|
||||
prioritized.sort(Comparator.comparing(Prioritized::getPriority).reversed());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,18 @@
|
||||
package dev.sheldan.abstracto.core.utils;
|
||||
|
||||
|
||||
import dev.sheldan.abstracto.core.command.exception.AbstractoTemplatedException;
|
||||
import dev.sheldan.abstracto.core.exception.DurationFormatException;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.ISnowflake;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
|
||||
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -44,4 +53,64 @@ public class ParseUtils {
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
public static Role parseRoleFromText(String text, Guild guild) {
|
||||
Role role;
|
||||
Matcher matcher = Message.MentionType.ROLE.getPattern().matcher(text);
|
||||
if(matcher.matches()) {
|
||||
String roleId = matcher.group(1);
|
||||
role = guild.getRoleById(roleId);
|
||||
} else {
|
||||
if(NumberUtils.isParsable(text)) {
|
||||
role = guild.getRoleById(text);
|
||||
} else {
|
||||
List<Role> roles = guild.getRolesByName(text, true);
|
||||
if(roles.isEmpty()) {
|
||||
throw new AbstractoTemplatedException("No role found with name.", "no_role_found_by_name_exception");
|
||||
}
|
||||
if(roles.size() > 1) {
|
||||
throw new AbstractoTemplatedException("Multiple roles found with name.", "multiple_roles_found_by_name_exception");
|
||||
}
|
||||
role = roles.get(0);
|
||||
}
|
||||
}
|
||||
if(role != null && role.isPublicRole()) {
|
||||
throw new AbstractoTemplatedException("Public role cannot be used for role parameter.", "everyone_role_not_allowed_exception");
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
public static GuildChannel parseGuildChannelFromText(String text, Guild guild) {
|
||||
Matcher matcher = Message.MentionType.CHANNEL.getPattern().matcher(text);
|
||||
GuildChannel textChannel;
|
||||
if(matcher.matches()) {
|
||||
String channelId = matcher.group(1);
|
||||
textChannel = guild.getChannelById(GuildChannel.class, channelId);
|
||||
} else {
|
||||
if(NumberUtils.isParsable(text)) {
|
||||
long channelId = Long.parseLong(text);
|
||||
return guild.getGuildChannelById(channelId);
|
||||
} else {
|
||||
List<ISnowflake> potentialMatches = new ArrayList<>();
|
||||
potentialMatches.addAll(getByName(guild.getTextChannelCache(), text));
|
||||
potentialMatches.addAll(getByName(guild.getCategoryCache(), text));
|
||||
potentialMatches.addAll(getByName(guild.getVoiceChannelCache(), text));
|
||||
potentialMatches.addAll(getByName(guild.getThreadChannelCache(), text));
|
||||
potentialMatches.addAll(getByName(guild.getForumChannelCache(), text));
|
||||
potentialMatches.addAll(getByName(guild.getStageChannelCache(), text));
|
||||
if(potentialMatches.isEmpty()) {
|
||||
throw new AbstractoTemplatedException("No channel found with name.", "no_channel_found_by_name_exception");
|
||||
}
|
||||
if(potentialMatches.size() > 1) {
|
||||
throw new AbstractoTemplatedException("Multiple channels found..", "multiple_channels_found_by_name_exception");
|
||||
}
|
||||
return guild.getGuildChannelById(potentialMatches.get(0).getId());
|
||||
}
|
||||
}
|
||||
return textChannel;
|
||||
}
|
||||
|
||||
private static <T extends ISnowflake> List<T> getByName(SnowflakeCacheView<T> cache, String name) {
|
||||
return cache.getElementsByName(name, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user