Ensure no JDA code is called during invalid shutdown

This commit is contained in:
Josh Roy 2021-07-03 14:17:32 -04:00
parent 37e3730e9d
commit d244956b4b
4 changed files with 30 additions and 10 deletions

View File

@ -253,6 +253,7 @@ discordErrorWebhook=An error occurred while sending messages to your console cha
discordLoggingIn=Attempting to login to Discord... discordLoggingIn=Attempting to login to Discord...
discordLoggingInDone=Successfully logged in as {0} discordLoggingInDone=Successfully logged in as {0}
discordNoSendPermission=Cannot send message in channel: #{0} Please ensure the bot has "Send Messages" permission in that channel\! discordNoSendPermission=Cannot send message in channel: #{0} Please ensure the bot has "Send Messages" permission in that channel\!
discordReloadInvalid=Tried to reload EssentialsX Discord config while the plugin is in an invalid state! If you've modified your config, restart your server.
disposal=Disposal disposal=Disposal
disposalCommandDescription=Opens a portable disposal menu. disposalCommandDescription=Opens a portable disposal menu.
disposalCommandUsage=/<command> disposalCommandUsage=/<command>

View File

@ -18,6 +18,8 @@ import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
import static com.earth2me.essentials.I18n.tl;
public class DiscordSettings implements IConf { public class DiscordSettings implements IConf {
private final EssentialsConfiguration config; private final EssentialsConfiguration config;
private final EssentialsDiscord plugin; private final EssentialsDiscord plugin;
@ -306,6 +308,11 @@ public class DiscordSettings implements IConf {
@Override @Override
public void reloadConfig() { public void reloadConfig() {
if (plugin.isInvalidStartup()) {
plugin.getLogger().warning(tl("discordReloadInvalid"));
return;
}
config.load(); config.load();
// Build channel maps // Build channel maps

View File

@ -36,6 +36,10 @@ public class EssentialsDiscord extends JavaPlugin implements IEssentialsModule {
settings = new DiscordSettings(this); settings = new DiscordSettings(this);
ess.addReloadListener(settings); ess.addReloadListener(settings);
if (metrics == null) {
metrics = new MetricsWrapper(this, 9824, false);
}
if (jda == null) { if (jda == null) {
jda = new JDADiscordService(this); jda = new JDADiscordService(this);
try { try {
@ -46,18 +50,13 @@ public class EssentialsDiscord extends JavaPlugin implements IEssentialsModule {
if (ess.getSettings().isDebug()) { if (ess.getSettings().isDebug()) {
e.printStackTrace(); e.printStackTrace();
} }
setEnabled(false); jda.shutdown();
return;
} }
} }
if (metrics == null) {
metrics = new MetricsWrapper(this, 9824, false);
}
} }
public void onReload() { public void onReload() {
if (jda != null) { if (jda != null && !jda.isInvalidStartup()) {
jda.updatePresence(); jda.updatePresence();
jda.updatePrimaryChannel(); jda.updatePrimaryChannel();
jda.updateConsoleRelay(); jda.updateConsoleRelay();
@ -65,6 +64,10 @@ public class EssentialsDiscord extends JavaPlugin implements IEssentialsModule {
} }
} }
public boolean isInvalidStartup() {
return jda != null && jda.isInvalidStartup();
}
public IEssentials getEss() { public IEssentials getEss() {
return ess; return ess;
} }
@ -79,7 +82,7 @@ public class EssentialsDiscord extends JavaPlugin implements IEssentialsModule {
@Override @Override
public void onDisable() { public void onDisable() {
if (jda != null) { if (jda != null && !jda.isInvalidStartup()) {
jda.shutdown(); jda.shutdown();
} }
} }

View File

@ -63,6 +63,7 @@ public class JDADiscordService implements DiscordService {
private ConsoleInjector injector; private ConsoleInjector injector;
private DiscordCommandDispatcher commandDispatcher; private DiscordCommandDispatcher commandDispatcher;
private InteractionControllerImpl interactionController; private InteractionControllerImpl interactionController;
private boolean invalidStartup = false;
public JDADiscordService(EssentialsDiscord plugin) { public JDADiscordService(EssentialsDiscord plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -148,11 +149,13 @@ public class JDADiscordService implements DiscordService {
logger.log(Level.INFO, tl("discordLoggingInDone", jda.getSelfUser().getAsTag())); logger.log(Level.INFO, tl("discordLoggingInDone", jda.getSelfUser().getAsTag()));
if (jda.getGuilds().isEmpty()) { if (jda.getGuilds().isEmpty()) {
invalidStartup = true;
throw new IllegalArgumentException(tl("discordErrorNoGuildSize")); throw new IllegalArgumentException(tl("discordErrorNoGuildSize"));
} }
guild = jda.getGuildById(plugin.getSettings().getGuildId()); guild = jda.getGuildById(plugin.getSettings().getGuildId());
if (guild == null) { if (guild == null) {
invalidStartup = true;
throw new IllegalArgumentException(tl("discordErrorNoGuild")); throw new IllegalArgumentException(tl("discordErrorNoGuild"));
} }
@ -348,8 +351,10 @@ public class JDADiscordService implements DiscordService {
} }
if (jda != null) { if (jda != null) {
sendMessage(MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true); if (!invalidStartup) {
DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true, null, null, null); sendMessage(MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true);
DiscordUtil.dispatchDiscordMessage(JDADiscordService.this, MessageType.DefaultTypes.SERVER_STOP, getSettings().getStopMessage(), true, null, null, null);
}
shutdownConsoleRelay(true); shutdownConsoleRelay(true);
@ -410,6 +415,10 @@ public class JDADiscordService implements DiscordService {
return consoleWebhook; return consoleWebhook;
} }
public boolean isInvalidStartup() {
return invalidStartup;
}
public boolean isDebug() { public boolean isDebug() {
return plugin.getEss().getSettings().isDebug(); return plugin.getEss().getSettings().isDebug();
} }