From 19db8a14dc88f5e9aa04b149729f1b72a15fc0ab Mon Sep 17 00:00:00 2001 From: Allink <44676012+allinkdev@users.noreply.github.com> Date: Fri, 28 Oct 2022 13:46:16 +0100 Subject: [PATCH] Remove unused imports, variables and other minor oversights in the Discord bridge (#280) * Fix player names not being deformatted * Remove unused imports and variables * Rectify usage of deprecated methods * Clarify comment * Update JDA plugin warning repository URL * Fix possible NPEs Co-authored-by: Ryan --- .../totalfreedommod/discord/Discord.java | 44 ++++++++++++++----- .../discord/DiscordToMinecraftListener.java | 1 - .../discord/command/DiscordCommandImpl.java | 2 +- .../discord/commands/HelpCommand.java | 2 +- .../discord/commands/ListCommand.java | 15 +++---- .../discord/commands/TPSCommand.java | 4 +- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java index 77d1779c..248d6090 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java @@ -7,11 +7,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Objects; -import java.util.SplittableRandom; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.security.auth.login.LoginException; import com.google.common.collect.ImmutableList; @@ -41,9 +38,7 @@ import net.dv8tion.jda.api.utils.ChunkingFilter; import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.dv8tion.jda.internal.utils.concurrent.CountingThreadFactory; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; -import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.WordUtils; import org.apache.commons.lang3.RandomStringUtils; import org.bukkit.GameRule; @@ -66,7 +61,6 @@ public class Discord extends FreedomService public List> sentMessages = new ArrayList<>(); public Boolean enabled = false; private static final ImmutableList DISCORD_SUBDOMAINS = ImmutableList.of("discordapp.com", "discord.com", "discord.gg"); - private final Pattern DISCORD_MENTION_PATTERN = Pattern.compile("(<@!?([0-9]{16,20})>)"); public static String getCode(PlayerData playerData) { @@ -209,7 +203,7 @@ public class Discord extends FreedomService catch (NoClassDefFoundError e) { FLog.warning("The JDA plugin is not installed, therefore the discord bot cannot start."); - FLog.warning("To resolve this error, please download the latest JDA from: https://github.com/TFPatches/Minecraft-JDA/releases"); + FLog.warning("To resolve this error, please download the latest JDA from: https://github.com/AtlasMediaGroup/Minecraft-JDA/releases"); } } @@ -277,7 +271,7 @@ public class Discord extends FreedomService if (deathMessage != null) { - messageChatChannel("**" + PlainTextComponentSerializer.plainText().serialize(deathMessage) + "**", true); + messageChatChannel("**" + deformat(PlainTextComponentSerializer.plainText().serialize(deathMessage)) + "**", true); } } @@ -292,7 +286,7 @@ public class Discord extends FreedomService { if (!plugin.al.isVanished(event.getPlayer().getName())) { - messageChatChannel("**" + event.getPlayer().getName() + " joined the server" + "**", true); + messageChatChannel("**" + deformat(event.getPlayer().getName()) + " joined the server" + "**", true); } } @@ -301,7 +295,7 @@ public class Discord extends FreedomService { if (!plugin.al.isVanished(event.getPlayer().getName())) { - messageChatChannel("**" + event.getPlayer().getName() + " left the server" + "**", true); + messageChatChannel("**" + deformat(event.getPlayer().getName()) + " left the server" + "**", true); } } @@ -437,8 +431,21 @@ public class Discord extends FreedomService } final Guild server = bot.getGuildById(ConfigEntry.DISCORD_SERVER_ID.getString()); + + if (server == null) + { + FLog.severe("The guild ID specified in the config is invalid."); + return false; + } + final TextChannel channel = server.getTextChannelById(ConfigEntry.DISCORD_REPORT_CHANNEL_ID.getString()); + if (channel == null) + { + FLog.severe("The report channel ID specified in the config is invalid."); + return false; + } + final EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setTitle("Report for " + reported.getName() + " (offline)"); embedBuilder.setDescription(reason); @@ -456,7 +463,7 @@ public class Discord extends FreedomService } } MessageEmbed embed = embedBuilder.build(); - Message message = channel.sendMessage(embed).complete(); + Message message = channel.sendMessageEmbeds(embed).complete(); if (!ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty()) { @@ -474,8 +481,21 @@ public class Discord extends FreedomService } final Guild server = bot.getGuildById(ConfigEntry.DISCORD_SERVER_ID.getString()); + + if (server == null) + { + FLog.severe("The guild ID specified in the config is invalid."); + return false; + } + final TextChannel channel = server.getTextChannelById(ConfigEntry.DISCORD_REPORT_CHANNEL_ID.getString()); + if (channel == null) + { + FLog.severe("The report channel ID specified in the config is invalid."); + return false; + } + final EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setTitle("Report for " + reported.getName()); embedBuilder.setDescription(reason); @@ -496,7 +516,7 @@ public class Discord extends FreedomService } MessageEmbed embed = embedBuilder.build(); - Message message = channel.sendMessage(embed).complete(); + Message message = channel.sendMessageEmbeds(embed).complete(); if (!ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty()) { diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java index 64c719bf..bd8d849d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/DiscordToMinecraftListener.java @@ -2,7 +2,6 @@ package me.totalfreedom.totalfreedommod.discord; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.config.ConfigEntry; -import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandManager; import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.rank.Title; import me.totalfreedom.totalfreedommod.util.FLog; diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/command/DiscordCommandImpl.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/command/DiscordCommandImpl.java index 8626be04..4726f1c2 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/command/DiscordCommandImpl.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/command/DiscordCommandImpl.java @@ -7,7 +7,7 @@ public abstract class DiscordCommandImpl implements DiscordCommand @Override public boolean canExecute(Member member) { - //return !isAdmin() || member.getRoles().stream().filter((role -> role.getName().toLowerCase().contains("admin") && !role.getName().toLowerCase().contains("discord"))).toList().size() > 0; + // TODO Implement administrator checks if/when any administrative Discord commands are added to the bot return !isAdmin(); } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/HelpCommand.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/HelpCommand.java index d9825083..ffa0a437 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/HelpCommand.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/HelpCommand.java @@ -81,6 +81,6 @@ public class HelpCommand extends DiscordCommandImpl embedBuilder.addField(category, fieldValue.toString().trim(), false); } - return new MessageBuilder().setEmbed(embedBuilder.build()); + return new MessageBuilder().setEmbeds(embedBuilder.build()); } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/ListCommand.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/ListCommand.java index d81d24ff..a9bd6783 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/ListCommand.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/ListCommand.java @@ -3,16 +3,14 @@ package me.totalfreedom.totalfreedommod.discord.commands; import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.admin.AdminList; import me.totalfreedom.totalfreedommod.config.ConfigEntry; -import me.totalfreedom.totalfreedommod.discord.command.DiscordCommand; +import me.totalfreedom.totalfreedommod.discord.Discord; import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandImpl; import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.rank.RankManager; import me.totalfreedom.totalfreedommod.util.FUtil; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; -import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; -import net.dv8tion.jda.api.entities.User; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -82,13 +80,14 @@ public class ListCommand extends DiscordCommandImpl Displayable displayable = rankManager.getDisplay(onlinePlayer); + final String name = Discord.deformat(onlinePlayer.getName()); + if (displayables.containsKey(displayable)) { - displayables.get(displayable).add(onlinePlayer.getName()); - } - else + displayables.get(displayable).add(name); + } else { - displayables.put(displayable, new ArrayList<>(List.of(onlinePlayer.getName()))); + displayables.put(displayable, new ArrayList<>(List.of(name))); } } @@ -101,6 +100,6 @@ public class ListCommand extends DiscordCommandImpl String.join(", ", players), false); } - return new MessageBuilder().setEmbed(embedBuilder.build()); + return new MessageBuilder().setEmbeds(embedBuilder.build()); } } diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/TPSCommand.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/TPSCommand.java index 0067af00..4ce0d008 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/TPSCommand.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/commands/TPSCommand.java @@ -1,13 +1,11 @@ package me.totalfreedom.totalfreedommod.discord.commands; -import me.totalfreedom.totalfreedommod.discord.command.DiscordCommand; import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandImpl; import me.totalfreedom.totalfreedommod.util.FUtil; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.Member; import org.bukkit.Bukkit; -import org.bukkit.Server; import java.util.Collections; import java.util.List; @@ -55,6 +53,6 @@ public class TPSCommand extends DiscordCommandImpl builder.addField("Allocated Memory", Math.ceil(FUtil.getTotalMem()) + " MB", false); builder.addField("Free Memory", Math.ceil(FUtil.getFreeMem()) + " MB", false); - return new MessageBuilder().setEmbed(builder.build()); + return new MessageBuilder().setEmbeds(builder.build()); } }