Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -92,6 +94,18 @@ public boolean isShowDiscordAttachments() {
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);
}
Expand Down Expand Up @@ -508,6 +522,13 @@ public void reloadConfig() {
}
}

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")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) {

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)) {
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,20 +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(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) : "";
}

/**
Expand All @@ -148,11 +165,13 @@ public static String getRoleFormat(Member member) {
* @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
Expand All @@ -176,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
12 changes: 12 additions & 0 deletions EssentialsDiscord/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down