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 <Wild1145@users.noreply.github.com>
This commit is contained in:
Allink 2022-10-28 13:46:16 +01:00 committed by Paul Reilly
parent 7f0eeab09c
commit 19db8a14dc
6 changed files with 42 additions and 26 deletions

View File

@ -7,11 +7,8 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.SplittableRandom;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import com.google.common.collect.ImmutableList; 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.api.utils.MemberCachePolicy;
import net.dv8tion.jda.internal.utils.concurrent.CountingThreadFactory; import net.dv8tion.jda.internal.utils.concurrent.CountingThreadFactory;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils; import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.bukkit.GameRule; import org.bukkit.GameRule;
@ -66,7 +61,6 @@ public class Discord extends FreedomService
public List<CompletableFuture<Message>> sentMessages = new ArrayList<>(); public List<CompletableFuture<Message>> sentMessages = new ArrayList<>();
public Boolean enabled = false; public Boolean enabled = false;
private static final ImmutableList<String> DISCORD_SUBDOMAINS = ImmutableList.of("discordapp.com", "discord.com", "discord.gg"); private static final ImmutableList<String> 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) public static String getCode(PlayerData playerData)
{ {
@ -209,7 +203,7 @@ public class Discord extends FreedomService
catch (NoClassDefFoundError e) catch (NoClassDefFoundError e)
{ {
FLog.warning("The JDA plugin is not installed, therefore the discord bot cannot start."); 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) 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())) 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())) 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()); 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()); 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(); final EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Report for " + reported.getName() + " (offline)"); embedBuilder.setTitle("Report for " + reported.getName() + " (offline)");
embedBuilder.setDescription(reason); embedBuilder.setDescription(reason);
@ -456,7 +463,7 @@ public class Discord extends FreedomService
} }
} }
MessageEmbed embed = embedBuilder.build(); 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()) 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()); 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()); 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(); final EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Report for " + reported.getName()); embedBuilder.setTitle("Report for " + reported.getName());
embedBuilder.setDescription(reason); embedBuilder.setDescription(reason);
@ -496,7 +516,7 @@ public class Discord extends FreedomService
} }
MessageEmbed embed = embedBuilder.build(); 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()) if (!ConfigEntry.DISCORD_REPORT_ARCHIVE_CHANNEL_ID.getString().isEmpty())
{ {

View File

@ -2,7 +2,6 @@ package me.totalfreedom.totalfreedommod.discord;
import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandManager;
import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.rank.Title; import me.totalfreedom.totalfreedommod.rank.Title;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;

View File

@ -7,7 +7,7 @@ public abstract class DiscordCommandImpl implements DiscordCommand
@Override @Override
public boolean canExecute(Member member) 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(); return !isAdmin();
} }
} }

View File

@ -81,6 +81,6 @@ public class HelpCommand extends DiscordCommandImpl
embedBuilder.addField(category, fieldValue.toString().trim(), false); embedBuilder.addField(category, fieldValue.toString().trim(), false);
} }
return new MessageBuilder().setEmbed(embedBuilder.build()); return new MessageBuilder().setEmbeds(embedBuilder.build());
} }
} }

View File

@ -3,16 +3,14 @@ package me.totalfreedom.totalfreedommod.discord.commands;
import me.totalfreedom.totalfreedommod.TotalFreedomMod; import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.admin.AdminList; import me.totalfreedom.totalfreedommod.admin.AdminList;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; 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.discord.command.DiscordCommandImpl;
import me.totalfreedom.totalfreedommod.rank.Displayable; import me.totalfreedom.totalfreedommod.rank.Displayable;
import me.totalfreedom.totalfreedommod.rank.RankManager; import me.totalfreedom.totalfreedommod.rank.RankManager;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder; 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.Member;
import net.dv8tion.jda.api.entities.User;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -82,13 +80,14 @@ public class ListCommand extends DiscordCommandImpl
Displayable displayable = rankManager.getDisplay(onlinePlayer); Displayable displayable = rankManager.getDisplay(onlinePlayer);
final String name = Discord.deformat(onlinePlayer.getName());
if (displayables.containsKey(displayable)) if (displayables.containsKey(displayable))
{ {
displayables.get(displayable).add(onlinePlayer.getName()); displayables.get(displayable).add(name);
} } else
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); String.join(", ", players), false);
} }
return new MessageBuilder().setEmbed(embedBuilder.build()); return new MessageBuilder().setEmbeds(embedBuilder.build());
} }
} }

View File

@ -1,13 +1,11 @@
package me.totalfreedom.totalfreedommod.discord.commands; package me.totalfreedom.totalfreedommod.discord.commands;
import me.totalfreedom.totalfreedommod.discord.command.DiscordCommand;
import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandImpl; import me.totalfreedom.totalfreedommod.discord.command.DiscordCommandImpl;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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("Allocated Memory", Math.ceil(FUtil.getTotalMem()) + " MB", false);
builder.addField("Free Memory", Math.ceil(FUtil.getFreeMem()) + " MB", false); builder.addField("Free Memory", Math.ceil(FUtil.getFreeMem()) + " MB", false);
return new MessageBuilder().setEmbed(builder.build()); return new MessageBuilder().setEmbeds(builder.build());
} }
} }