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...
discordLoggingInDone=Successfully logged in as {0}
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
disposalCommandDescription=Opens a portable disposal menu.
disposalCommandUsage=/<command>

View File

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

View File

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

View File

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