mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-11-21 01:01:35 +08:00
Add discord roles blacklist and aliases for chat format (#5157)
Co-authored-by: Josh Roy <10731363+JRoy@users.noreply.github.com>
This commit is contained in:
parent
ad5f17481c
commit
e3e52db10e
@ -6,6 +6,7 @@ import com.earth2me.essentials.config.EssentialsConfiguration;
|
||||
import com.earth2me.essentials.utils.FormatUtil;
|
||||
import net.dv8tion.jda.api.OnlineStatus;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.essentialsx.api.v2.ChatType;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -33,6 +34,7 @@ public class DiscordSettings implements IConf {
|
||||
private Activity statusActivity;
|
||||
|
||||
private List<Pattern> discordFilter;
|
||||
private Map<String, String> roleAliases;
|
||||
|
||||
private MessageFormat consoleFormat;
|
||||
private Level consoleLogLevel;
|
||||
@ -92,6 +94,18 @@ public class DiscordSettings implements IConf {
|
||||
return config.getBoolean("show-discord-attachments", true);
|
||||
}
|
||||
|
||||
public List<String> getDiscordRolesBlacklist() {
|
||||
return config.getList("discord-role-blacklist", String.class);
|
||||
}
|
||||
|
||||
public Boolean getInvertDiscordRoleBlacklist() {
|
||||
return config.getBoolean("invert-discord-role-blacklist", false);
|
||||
}
|
||||
|
||||
public String getRoleAlias(final Role role) {
|
||||
return roleAliases.getOrDefault(role.getId(), roleAliases.getOrDefault(role.getName(), role.getName()));
|
||||
}
|
||||
|
||||
public List<String> getPermittedFormattingRoles() {
|
||||
return config.getList("permit-formatting-roles", String.class);
|
||||
}
|
||||
@ -508,6 +522,13 @@ public class DiscordSettings implements IConf {
|
||||
}
|
||||
}
|
||||
|
||||
final Map<String, String> roleAliases = new HashMap<>();
|
||||
|
||||
for (Map.Entry<String, String> entry : config.getStringMap("discord-roles-aliases").entrySet()) {
|
||||
roleAliases.put(entry.getKey(), FormatUtil.replaceFormat(entry.getValue()));
|
||||
}
|
||||
this.roleAliases = roleAliases;
|
||||
|
||||
consoleLogLevel = Level.toLevel(config.getString("console.log-level", null), Level.INFO);
|
||||
|
||||
if (config.isList("console.console-filter")) {
|
||||
|
@ -102,7 +102,7 @@ public class DiscordListener extends ListenerAdapter {
|
||||
|
||||
String formattedMessage = EmojiParser.parseToAliases(MessageUtil.formatMessage(plugin.getPlugin().getSettings().getDiscordToMcFormat(),
|
||||
event.getChannel().getName(), user.getName(), user.getDiscriminator(), user.getAsTag(),
|
||||
effectiveName, DiscordUtil.getRoleColorFormat(member), finalMessage, DiscordUtil.getRoleFormat(member)), EmojiParser.FitzpatrickAction.REMOVE);
|
||||
effectiveName, DiscordUtil.getRoleColorFormat(plugin, member), finalMessage, DiscordUtil.getRoleFormat(plugin, member)), EmojiParser.FitzpatrickAction.REMOVE);
|
||||
|
||||
for (final String group : keys) {
|
||||
if (plugin.getSettings().getRelayToConsoleList().contains(group)) {
|
||||
|
@ -4,7 +4,6 @@ import club.minnced.discord.webhook.WebhookClient;
|
||||
import club.minnced.discord.webhook.send.AllowedMentions;
|
||||
import com.earth2me.essentials.utils.DownsampleUtil;
|
||||
import com.earth2me.essentials.utils.FormatUtil;
|
||||
import com.earth2me.essentials.utils.NumberUtil;
|
||||
import com.earth2me.essentials.utils.VersionUtil;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
@ -21,10 +20,12 @@ import okhttp3.OkHttpClient;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public final class DiscordUtil {
|
||||
public final static String ADVANCED_RELAY_NAME = "EssX Advanced Relay";
|
||||
@ -126,20 +127,36 @@ public final class DiscordUtil {
|
||||
return future;
|
||||
}
|
||||
|
||||
public static Role getHighestRole(final JDADiscordService jda, final Member member, final Predicate<Role> fail) {
|
||||
final List<Role> roles = member == null ? Collections.emptyList() : member.getRoles();
|
||||
final List<String> blacklist = jda.getPlugin().getSettings().getDiscordRolesBlacklist();
|
||||
final boolean invert = jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist();
|
||||
|
||||
for (final Role role : roles) {
|
||||
final boolean blacklisted = blacklist.contains(role.getName()) || blacklist.contains(role.getId());
|
||||
if ((blacklisted && !invert) || (!blacklisted && invert)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fail != null && fail.test(role)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return role;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the highest role of a given member or an empty string if the member has no roles.
|
||||
*
|
||||
* @param member The target member.
|
||||
* @return The highest role or blank string.
|
||||
*/
|
||||
public static String getRoleFormat(Member member) {
|
||||
final List<Role> roles = member == null ? null : member.getRoles();
|
||||
|
||||
if (roles == null || roles.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return roles.get(0).getName();
|
||||
public static String getRoleFormat(final JDADiscordService jda, Member member) {
|
||||
final Role role = getHighestRole(jda, member, null);
|
||||
return role != null ? jda.getSettings().getRoleAlias(role) : "";
|
||||
}
|
||||
|
||||
/**
|
||||
@ -148,11 +165,13 @@ public final class DiscordUtil {
|
||||
* @param member The target member.
|
||||
* @return The bukkit color code or blank string.
|
||||
*/
|
||||
public static String getRoleColorFormat(Member member) {
|
||||
if (member == null || member.getColorRaw() == Role.DEFAULT_COLOR_RAW) {
|
||||
public static String getRoleColorFormat(final JDADiscordService jda, Member member) {
|
||||
final Role topRole = getHighestRole(jda, member, role -> role.getColorRaw() == Role.DEFAULT_COLOR_RAW);
|
||||
if (topRole == null || topRole.getColorRaw() == Role.DEFAULT_COLOR_RAW) {
|
||||
return "";
|
||||
}
|
||||
final int rawColor = 0xff000000 | member.getColorRaw();
|
||||
|
||||
final int rawColor = 0xff000000 | topRole.getColorRaw();
|
||||
|
||||
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_16_1_R01)) {
|
||||
// Essentials' FormatUtil allows us to not have to use bungee's chatcolor since bukkit's own one doesn't support rgb
|
||||
@ -176,14 +195,13 @@ public final class DiscordUtil {
|
||||
final List<Role> roles = member.getRoles();
|
||||
for (String roleDefinition : roleDefinitions) {
|
||||
roleDefinition = roleDefinition.trim();
|
||||
final boolean id = NumberUtil.isNumeric(roleDefinition);
|
||||
|
||||
if (roleDefinition.equals("*") || member.getId().equals(roleDefinition)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (final Role role : roles) {
|
||||
if (role.getId().equals(roleDefinition) || (!id && role.getName().equalsIgnoreCase(roleDefinition))) {
|
||||
if (matchesRole(role, roleDefinition)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -191,6 +209,15 @@ public final class DiscordUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided role matches the provided role definition (string representation of id or the role's name)
|
||||
*
|
||||
* @return true if the provided definition matches the provided role.
|
||||
*/
|
||||
public static boolean matchesRole(Role role, String roleDefinition) {
|
||||
return role.getId().equals(roleDefinition) || role.getName().equalsIgnoreCase(roleDefinition);
|
||||
}
|
||||
|
||||
public static String getAvatarUrl(final JDADiscordService jda, final Player player) {
|
||||
return jda.getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()).replace("{name}", player.getName());
|
||||
}
|
||||
|
@ -224,6 +224,18 @@ commands:
|
||||
# If this is set to false and a message from Discord only contains an image/file and not any text, nothing will be sent.
|
||||
show-discord-attachments: true
|
||||
|
||||
# A list of roles which should be ignored by the {color} and {role} placeholders.
|
||||
# for the Discord->MC chat format.
|
||||
discord-role-blacklist:
|
||||
- "123456789012345678"
|
||||
- "Members"
|
||||
|
||||
# Role aliases allow you to replace the role names with something different in the Discord->MC chat relay format.
|
||||
# If you are using role aliases, make sure to remove the '#' at the start to allow the setting to be read.
|
||||
discord-roles-aliases:
|
||||
# "123456789012345678": "&c&lAdmin"
|
||||
# "Members": "Member"
|
||||
|
||||
# A list of roles allowed to send Minecraft color/formatting codes from Discord to MC.
|
||||
# This applies to all aspects such as that Discord->MC chat relay as well as commands.
|
||||
# You can either use '*' (for everyone), a role name/ID, or a user ID.
|
||||
|
Loading…
Reference in New Issue
Block a user