From 4a32b6b5e7f1ccfdde4404f65665238acc3dd263 Mon Sep 17 00:00:00 2001 From: Nguyen Quy Hy Date: Mon, 16 Jan 2017 00:05:10 +0800 Subject: [PATCH] Mentioning channels from MC --- README.md | 8 +-- examples/config-multiple.json | 7 +-- examples/config-simple.json | 7 +-- .../discordbridge/utils/DiscordUtil.java | 15 ++++++ .../discordbridge/utils/TextUtil.java | 50 ++++++++++++------- 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b392c01..d406c01 100644 --- a/README.md +++ b/README.md @@ -90,9 +90,10 @@ Configuration is stored in `config.json` file. - `hoverTemplate`: template for the message shown when you hover over an attachment link - `allowLink`: adds a clickable link in game for attachments sent via discord - `mention`: _(thanks, Mohron)_ - - `userTemplate`: template for @user mentions - accepts `%a`/`%u` - - `roleTemplate`: template for @role mentions - accepts `%r` - - `everyoneTemplate`: template for @here & @everyone mentions - accepts `%a` + - `userTemplate`: template for @user mentions - accepts `%s`/`%u` + - `roleTemplate`: template for @role mentions - accepts `%s` + - `everyoneTemplate`: template for @here & @everyone mentions - accepts `%s` + - `channelTemplate`: template for @here & @everyone mentions - accepts `%s` - `roles`: `minecraft` configurations that are for a specific Discord role You can find some example configurations in `examples` folders. @@ -111,6 +112,7 @@ You can find some example configurations in `examples` folders. |---------|-----------| | `discordbridge.mention.name`
`discordbridge.mention.name.` | Allows `@username`/`@nickname` mentions to be sent from Minecraft | | `discordbridge.mention.role`
`discordbridge.mention.role.` | Allows `@role` mentions - the role must have "Allow anyone to @mention" set | +| `discordbridge.mention.channel`
`discordbridge.mention.channel.` | Allows `#channel` mention | | `discordbridge.mention.here` | Allows the `@here` mention1 | | `discordbridge.mention.everyone` | Allows the `@everyone` mention1 | > 1 The bot must have permission to "Mention Everyone" in order to use `@here` & `@everyone`. diff --git a/examples/config-multiple.json b/examples/config-multiple.json index c854c5b..922e75a 100644 --- a/examples/config-multiple.json +++ b/examples/config-multiple.json @@ -24,9 +24,10 @@ "allowLink": false }, "mention": { - "userTemplate": "&6@&e%a", - "roleTemplate": "&6@&a%r", - "everyoneTemplate": "&6@&3%a" + "userTemplate": "@%s", + "roleTemplate": "@%s", + "everyoneTemplate": "&6@%s", + "channelTemplate": "&9#%s" }, "roles": { "Admin": { diff --git a/examples/config-simple.json b/examples/config-simple.json index 81f3d5d..c28a392 100644 --- a/examples/config-simple.json +++ b/examples/config-simple.json @@ -28,9 +28,10 @@ "allowLink": true }, "mention": { - "userTemplate": "&6@&e%a", - "roleTemplate": "&6@&a%r", - "everyoneTemplate": "&6@&3%a" + "userTemplate": "@%s", + "roleTemplate": "@%s", + "everyoneTemplate": "&6@%s", + "channelTemplate": "&9#%s" } } } diff --git a/src/main/java/com/nguyenquyhy/discordbridge/utils/DiscordUtil.java b/src/main/java/com/nguyenquyhy/discordbridge/utils/DiscordUtil.java index 28d5270..7b67b90 100644 --- a/src/main/java/com/nguyenquyhy/discordbridge/utils/DiscordUtil.java +++ b/src/main/java/com/nguyenquyhy/discordbridge/utils/DiscordUtil.java @@ -2,6 +2,7 @@ import com.google.common.collect.Lists; import com.nguyenquyhy.discordbridge.models.ChannelMinecraftMentionConfig; +import de.btobastian.javacord.entities.Channel; import de.btobastian.javacord.entities.Server; import de.btobastian.javacord.entities.User; import de.btobastian.javacord.entities.permissions.Role; @@ -44,4 +45,18 @@ static Optional getRoleByName(String name, Server server) { } return Optional.empty(); } + + /** + * @param name The name to search the server for valid a Channel + * @param server The server to search through Roles + * @return The Channel, if any, that matches the name supplied + */ + static Optional getChannelByName(String name, Server server) { + for (Channel channel : server.getChannels()) { + if (channel.getName().equalsIgnoreCase(name)) { + return Optional.of(channel); + } + } + return Optional.empty(); + } } diff --git a/src/main/java/com/nguyenquyhy/discordbridge/utils/TextUtil.java b/src/main/java/com/nguyenquyhy/discordbridge/utils/TextUtil.java index e46acd0..fd067f3 100644 --- a/src/main/java/com/nguyenquyhy/discordbridge/utils/TextUtil.java +++ b/src/main/java/com/nguyenquyhy/discordbridge/utils/TextUtil.java @@ -11,6 +11,7 @@ import de.btobastian.javacord.entities.message.Message; import de.btobastian.javacord.entities.permissions.Role; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.action.HoverAction; @@ -38,7 +39,7 @@ public class TextUtil { private static final Pattern urlPattern = Pattern.compile("(?(^|\\s))(?(&[0-9a-flmnork])+)?(?(http(s)?://)?([A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,}\\S*)", Pattern.CASE_INSENSITIVE); private static final Pattern mentionPattern = - Pattern.compile("(@\\S*)"); + Pattern.compile("([@#]\\S*)"); public static final StyleTuple EMPTY = new StyleTuple(TextColors.NONE, TextStyles.NONE); @@ -65,27 +66,40 @@ public static String formatMinecraftMessage(String message) { */ public static String formatMinecraftMention(String message, Server server, Player player, boolean isBot) { Matcher m = mentionPattern.matcher(message); + Logger logger = DiscordBridge.getInstance().getLogger(); + while (m.find()) { String mention = m.group(); - String mentionName = mention.replace("@", ""); - if ((mentionName.equalsIgnoreCase("here") && isBot && !player.hasPermission("discordbridge.mention.here")) || - (mentionName.equalsIgnoreCase("everyone") && isBot && !player.hasPermission("discordbridge.mention.everyone"))) { - message = message.replace(mention, mentionName); - continue; - } - if (!isBot || player.hasPermission("discordbridge.mention.name." + mentionName.toLowerCase())) { - Optional user = DiscordUtil.getUserByName(mentionName, server); - DiscordBridge.getInstance().getLogger().info(String.format("Found user %s: %s", mentionName, user.isPresent())); - if (user.isPresent()) { - message = message.replace(mention, "<@" + user.get().getId() + ">"); + if (mention.contains("@")) { + String mentionName = mention.replace("@", ""); + if ((mentionName.equalsIgnoreCase("here") && isBot && !player.hasPermission("discordbridge.mention.here")) || + (mentionName.equalsIgnoreCase("everyone") && isBot && !player.hasPermission("discordbridge.mention.everyone"))) { + message = message.replace(mention, mentionName); continue; } - } - if (!isBot || player.hasPermission("discordbridge.mention.role." + mentionName.toLowerCase())) { - Optional role = DiscordUtil.getRoleByName(mentionName, server); - DiscordBridge.getInstance().getLogger().info(String.format("Found role %s: %s", mentionName, role.isPresent())); - if (role.isPresent() && role.get().isMentionable()) { - message = message.replace(mention, "<@&" + role.get().getId() + ">"); + if (!isBot || player.hasPermission("discordbridge.mention.name." + mentionName.toLowerCase())) { + Optional user = DiscordUtil.getUserByName(mentionName, server); + logger.debug(String.format("Found user %s: %s", mentionName, user.isPresent())); + if (user.isPresent()) { + message = message.replace(mention, "<@" + user.get().getId() + ">"); + continue; + } + } + if (!isBot || player.hasPermission("discordbridge.mention.role." + mentionName.toLowerCase())) { + Optional role = DiscordUtil.getRoleByName(mentionName, server); + logger.debug(String.format("Found role %s: %s", mentionName, role.isPresent())); + if (role.isPresent() && role.get().isMentionable()) { + message = message.replace(mention, "<@&" + role.get().getId() + ">"); + } + } + } else if (mention.contains("#")) { + String mentionName = mention.replace("#", ""); + if (!isBot || player.hasPermission("discordbridge.mention.channel." + mentionName.toLowerCase())) { + Optional channel = DiscordUtil.getChannelByName(mentionName, server); + logger.debug(String.format("Found channel %s: %s", mentionName, channel.isPresent())); + if (channel.isPresent()) { + message = message.replace(mention, "<#" + channel.get().getId() + ">"); + } } } }