Skip to content

Commit

Permalink
Mentioning channels from MC
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Jan 15, 2017
1 parent 8d88fd5 commit 4a32b6b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -111,6 +112,7 @@ You can find some example configurations in `examples` folders.
|---------|-----------|
| `discordbridge.mention.name` <br> `discordbridge.mention.name.<name>` | Allows `@username`/`@nickname` mentions to be sent from Minecraft |
| `discordbridge.mention.role` <br> `discordbridge.mention.role.<role>` | Allows `@role` mentions - the role must have "Allow anyone to @mention" set |
| `discordbridge.mention.channel` <br> `discordbridge.mention.channel.<channel>` | Allows `#channel` mention |
| `discordbridge.mention.here` | Allows the `@here` mention<sup>1</sup> |
| `discordbridge.mention.everyone` | Allows the `@everyone` mention<sup>1</sup> |
> <sup>1</sup> The bot must have permission to "Mention Everyone" in order to use `@here` & `@everyone`.
Expand Down
7 changes: 4 additions & 3 deletions examples/config-multiple.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 4 additions & 3 deletions examples/config-simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/nguyenquyhy/discordbridge/utils/DiscordUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,4 +45,18 @@ static Optional<Role> 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<Channel> getChannelByName(String name, Server server) {
for (Channel channel : server.getChannels()) {
if (channel.getName().equalsIgnoreCase(name)) {
return Optional.of(channel);
}
}
return Optional.empty();
}
}
50 changes: 32 additions & 18 deletions src/main/java/com/nguyenquyhy/discordbridge/utils/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class TextUtil {
private static final Pattern urlPattern =
Pattern.compile("(?<first>(^|\\s))(?<colour>(&[0-9a-flmnork])+)?(?<url>(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);

Expand All @@ -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> 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> 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> 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> 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> 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() + ">");
}
}
}
}
Expand Down

0 comments on commit 4a32b6b

Please sign in to comment.