Merge pull request #19 from DroppingAnvil/1.6.x

You can now mention discord users in game!
This commit is contained in:
Driftay 2019-09-29 18:45:15 -04:00 committed by GitHub
commit 8a4478764f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -11,6 +11,7 @@ import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.Webhook;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
@ -21,6 +22,7 @@ import org.bukkit.ChatColor;
import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@ -48,6 +50,7 @@ public class FactionChatHandler extends ListenerAdapter {
public static void sendMessage(FactionsPlugin plugin, Faction faction, UUID uuid, String username, String message) {
String factionsChatChannelId = faction.getFactionChatChannelId();
String messageWithMentions = null;
if (factionsChatChannelId == null || factionsChatChannelId.isEmpty()) {
return;
}
@ -69,6 +72,28 @@ public class FactionChatHandler extends ListenerAdapter {
} else {
webhookClient = textChannel.createWebhook(Conf.webhookName).complete().newClient().build();
}
if (message.contains("@")) {
List<String> x = new ArrayList<>(Arrays.asList(message.split(" ")));
for (String y : x) {
if (y.contains("@")) {
if (!jda.getUsersByName(y.replace("@", ""), false).isEmpty() && jda.getUsersByName(y.replace("@", ""), false).size() < 2) {
x.set(x.indexOf(y), jda.getUsersByName(y.replace("@", ""), false).get(0).getAsMention());
}
}
}
StringBuilder sB = new StringBuilder();
for (String s : x) {
sB.append(s);
sB.append(" ");
}
messageWithMentions = sB.toString();
}
if (messageWithMentions != null) {
WebhookMessage webhookMessage = new WebhookMessageBuilder().setUsername(ChatColor.stripColor(username)).setAvatarUrl(Conf.avatarUrl.replace("%uuid%", uuid.toString())).setContent(ChatColor.stripColor(messageWithMentions)).build();
webhookClient.send(webhookMessage).join();
webhookClient.close();
return;
}
WebhookMessage webhookMessage = new WebhookMessageBuilder().setUsername(ChatColor.stripColor(username)).setAvatarUrl(Conf.avatarUrl.replace("%uuid%", uuid.toString())).setContent(ChatColor.stripColor(message)).build();
webhookClient.send(webhookMessage).join();
webhookClient.close();