First step to mentions! You can now mention discord users ingame if they have a one word name and there is not someone named the same thing! I plan to make this system much much better soon!

This commit is contained in:
DroppingAnvil 2019-09-29 13:29:23 -05:00
parent 90d9486165
commit 58b25c726f

View File

@ -11,6 +11,7 @@ import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Message; import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel; 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.entities.Webhook;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter; import net.dv8tion.jda.core.hooks.ListenerAdapter;
@ -21,6 +22,7 @@ import org.bukkit.ChatColor;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -45,6 +47,7 @@ public class FactionChatHandler extends ListenerAdapter {
public static void sendMessage(FactionsPlugin plugin, Faction faction, UUID uuid, String username, String message) { public static void sendMessage(FactionsPlugin plugin, Faction faction, UUID uuid, String username, String message) {
String factionsChatChannelId = faction.getFactionChatChannelId(); String factionsChatChannelId = faction.getFactionChatChannelId();
String messageWithMentions = null;
if (factionsChatChannelId == null || factionsChatChannelId.isEmpty()) { if (factionsChatChannelId == null || factionsChatChannelId.isEmpty()) {
return; return;
} }
@ -55,10 +58,10 @@ public class FactionChatHandler extends ListenerAdapter {
if (textChannel == null) { if (textChannel == null) {
return; return;
} }
if (!textChannel.getGuild().getSelfMember().hasPermission(textChannel, Permission.MANAGE_WEBHOOKS)) { //if (!textChannel.getGuild().getSelfMember().hasPermission(textChannel, Permission.MANAGE_WEBHOOKS)) {
textChannel.sendMessage("Missing `Manage Webhooks` permission in this channel").queue(); //textChannel.sendMessage("Missing `Manage Webhooks` permission in this channel").queue();
return; //return;
} //}
Webhook webhook = (textChannel.getWebhooks().complete()).stream().filter(w -> w.getName().equals(Conf.webhookName)).findAny().orElse(null); Webhook webhook = (textChannel.getWebhooks().complete()).stream().filter(w -> w.getName().equals(Conf.webhookName)).findAny().orElse(null);
WebhookClient webhookClient; WebhookClient webhookClient;
if (webhook != null) { if (webhook != null) {
@ -66,6 +69,28 @@ public class FactionChatHandler extends ListenerAdapter {
} else { } else {
webhookClient = textChannel.createWebhook(Conf.webhookName).complete().newClient().build(); 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(); 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.send(webhookMessage).join();
webhookClient.close(); webhookClient.close();