From 23c2dfa78e402b5737cbc4b2c8ca9faa0fc74ed3 Mon Sep 17 00:00:00 2001 From: Michaili K Date: Sat, 2 Mar 2024 01:26:58 +0100 Subject: [PATCH] Make JMusicBot error & shut down if ran in an unsupported manner (#1486) * Make JMusicBot error & shut down if ran in an unsupported manner * add info about message content intent * five second rule --------- Co-authored-by: unknown --- .../java/com/jagrosh/jmusicbot/JMusicBot.java | 20 +++++++++++++++++++ .../jagrosh/jmusicbot/utils/OtherUtil.java | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java index 7879b9244..f4dea7121 100644 --- a/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java +++ b/src/main/java/com/jagrosh/jmusicbot/JMusicBot.java @@ -189,6 +189,26 @@ else if(config.getGame().getName().equalsIgnoreCase("none")) .setBulkDeleteSplittingEnabled(true) .build(); bot.setJDA(jda); + + // check if something about the current startup is not supported + String unsupportedReason = OtherUtil.getUnsupportedBotReason(jda); + if (unsupportedReason != null) + { + prompt.alert(Prompt.Level.ERROR, "JMusicBot", "JMusicBot cannot be run on this Discord bot: " + unsupportedReason); + try{ Thread.sleep(5000);}catch(InterruptedException ignored){} // this is awful but until we have a better way... + jda.shutdown(); + System.exit(1); + } + + // other check that will just be a warning now but may be required in the future + // check if the user has changed the prefix and provide info about the + // message content intent + if(!"@mention".equals(config.getPrefix())) + { + prompt.alert(Prompt.Level.INFO, "JMusicBot", "You currently have a custom prefix set. " + + "If your prefix is not working, make sure that the 'MESSAGE CONTENT INTENT' is Enabled " + + "on https://discord.com/developers/applications/" + jda.getSelfUser().getId() + "/bot"); + } } catch (LoginException ex) { diff --git a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java index 67abe4ee8..63c39ce4b 100644 --- a/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java +++ b/src/main/java/com/jagrosh/jmusicbot/utils/OtherUtil.java @@ -23,8 +23,12 @@ import java.net.URLConnection; import java.nio.file.Path; import java.nio.file.Paths; + +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.ApplicationInfo; +import net.dv8tion.jda.api.entities.User; import okhttp3.*; import org.json.JSONException; import org.json.JSONObject; @@ -207,4 +211,20 @@ public static String getLatestVersion() return null; } } + + /** + * Checks if the bot JMusicBot is being run on is supported & returns the reason if it is not. + * @return A string with the reason, or null if it is supported. + */ + public static String getUnsupportedBotReason(JDA jda) + { + if (jda.getSelfUser().getFlags().contains(User.UserFlag.VERIFIED_BOT)) + return "The bot is verified. Using JMusicBot in a verified bot is not supported."; + + ApplicationInfo info = jda.retrieveApplicationInfo().complete(); + if (info.isBotPublic()) + return "\"Public Bot\" is enabled. Using JMusicBot as a public bot is not supported. Please disable it in the Developer Dashboard."; + + return null; + } }