Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JRoy committed Aug 9, 2023
1 parent 14d0344 commit 3869300
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -100,8 +102,8 @@ public Boolean getInvertDiscordRoleBlacklist() {
return config.getBoolean("invert-discord-role-blacklist", false);
}

public String getDiscordRoleAlias(String key) {
return config.getString("discord-roles-aliases." + key, "");
public String getRoleAlias(final Role role) {
return roleAliases.getOrDefault(role.getId(), roleAliases.getOrDefault(role.getName(), role.getName()));
}

public List<String> getPermittedFormattingRoles() {
Expand Down Expand Up @@ -520,6 +522,13 @@ public void reloadConfig() {
}
}

final Map<String, String> roleAliases = new HashMap<>();

for (Map.Entry<String, String> entry : config.getStringMap("chat.world-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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand All @@ -21,10 +20,12 @@
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";
Expand Down Expand Up @@ -126,34 +127,36 @@ public static CompletableFuture<Webhook> createWebhook(TextChannel channel, Stri
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(final JDADiscordService jda, Member member) {
final List<Role> roles = member == null ? null : member.getRoles();

for (final Role role : roles) {
final Boolean contains = jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getName()) ||
jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getId());
if ((!jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && !contains)
|| (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && contains)) {
String alias = jda.getPlugin().getSettings().getDiscordRoleAlias(role.getName());
if (alias == "") {
alias = jda.getPlugin().getSettings().getDiscordRoleAlias(role.getId());
}
if (alias != "") {
return alias;
} else {
return role.getName();
}
}
}

return "";

final Role role = getHighestRole(jda, member, null);
return role != null ? jda.getSettings().getRoleAlias(role) : "";
}

/**
Expand All @@ -163,35 +166,12 @@ public static String getRoleFormat(final JDADiscordService jda, Member member) {
* @return The bukkit color code or blank string.
*/
public static String getRoleColorFormat(final JDADiscordService jda, Member member) {
if (member == null || member.getColorRaw() == Role.DEFAULT_COLOR_RAW) {
final Role topRole = getHighestRole(jda, member, role -> role.getColorRaw() == Role.DEFAULT_COLOR_RAW);
if (topRole == null || topRole.getColorRaw() == Role.DEFAULT_COLOR_RAW) {
return "";
}

final List<Role> roles = member == null ? null : member.getRoles();

int color = Role.DEFAULT_COLOR_RAW;
for (final Role role : roles) {
final Boolean contains = jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getName()) ||
jda.getPlugin().getSettings().getDiscordRolesBlacklist().contains(role.getId());
if ((!jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && !contains)
|| (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && contains)
&& role.getColorRaw() != Role.DEFAULT_COLOR_RAW) {

color = role.getColorRaw();
break;

}
}

if (jda.getPlugin().getSettings().getInvertDiscordRoleBlacklist() && jda.getPlugin().getSettings().getDiscordRolesBlacklist().isEmpty()) {
color = member.getColorRaw();
}

if (color == Role.DEFAULT_COLOR_RAW) {
return "";
}

final int rawColor = 0xff000000 | color;
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
Expand All @@ -215,21 +195,29 @@ public static boolean hasRoles(Member member, List<String> roleDefinitions) {
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;
}
}
}
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());
}
Expand Down
16 changes: 8 additions & 8 deletions EssentialsDiscord/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,17 @@ 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

# List of roles which are used for the top role placeholders. If this is empty, all roles will be allowed.
# A list of roles which should be ignored by the {color} and {role} placeholders.
# for the Discord->MC chat format.
discord-role-blacklist:
- "1234123412341234123"
- "123456789012345678"
- "Members"

invert-discord-role-blacklist: false

# Aliases for Discord roles before they are sent to Minecraft.
discord-roles-aliases: []
# Admins: "Admin"
# Members: "Member"
# 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.
Expand Down

0 comments on commit 3869300

Please sign in to comment.