From 17a345f8ad15008e87ba3c804225e4036df341ad Mon Sep 17 00:00:00 2001 From: HeyBanditoz Date: Thu, 7 May 2020 15:16:46 -0600 Subject: [PATCH] Move to new JDABuilder methods and rewrite Discord mention fetching. --- README.md | 5 +++++ src/main/java/io/banditoz/gmecord/Bot.java | 3 ++- .../io/banditoz/gmecord/util/BuildMentionables.java | 13 +++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9adc2f2..efc58f5 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,11 @@ can use it! * Mentioning the bridge from Discord will mention the last user to send a message on the Groupme side. +## NOTE ON GATEWAY INTENTS +By default, the bot will assume you have the server members intent +checked. If not, it will fail to start (for now.) It is required for +mentionables to function. + ## Configuration The configuration of the bot must be in the same directory as the jar file, and must be called `Config.json`. A file will automatically be diff --git a/src/main/java/io/banditoz/gmecord/Bot.java b/src/main/java/io/banditoz/gmecord/Bot.java index a093a10..cb73b0e 100644 --- a/src/main/java/io/banditoz/gmecord/Bot.java +++ b/src/main/java/io/banditoz/gmecord/Bot.java @@ -6,6 +6,7 @@ import io.banditoz.gmecord.web.WebServer; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import org.slf4j.Logger; @@ -27,7 +28,7 @@ public class Bot { public Bot() { Settings settings = SettingsManager.getInstance().getSettings(); try { - jda = new JDABuilder(settings.getDiscordToken()).build(); + jda = JDABuilder.createLight(settings.getDiscordToken()).enableIntents(GatewayIntent.GUILD_MEMBERS).build(); jda.awaitReady(); } catch (Exception ex) { logger.error("Could not login! Exiting safely...", ex); diff --git a/src/main/java/io/banditoz/gmecord/util/BuildMentionables.java b/src/main/java/io/banditoz/gmecord/util/BuildMentionables.java index 8a95302..d38d1d0 100644 --- a/src/main/java/io/banditoz/gmecord/util/BuildMentionables.java +++ b/src/main/java/io/banditoz/gmecord/util/BuildMentionables.java @@ -4,15 +4,21 @@ import io.banditoz.gmecord.SettingsManager; import io.banditoz.gmecord.api.Member; import io.banditoz.gmecord.api.Response; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.User; import okhttp3.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Objects; public class BuildMentionables { + private static final Logger LOGGER = LoggerFactory.getLogger(BuildMentionables.class); + public static HashMap buildGroupmeMentionables() throws IOException { HashMap mentionables = new HashMap<>(); String initialUrl = "https://api.groupme.com/v3/groups/" + SettingsManager.getInstance().getSettings().getGroupID() + "?token=" + SettingsManager.getInstance().getSettings().getGroupMeToken(); @@ -24,16 +30,19 @@ public static HashMap buildGroupmeMentionables() throws IOExcept for (Member m : r.getResponse().getMembers()) { mentionables.put(m.getNickname(), m.getUserId()); } + LOGGER.info("We have " + mentionables.size() + " Groupme members."); return mentionables; } public static HashMap buildDiscordMentionables() { - List members = Bot.getJda().getTextChannelById(SettingsManager.getInstance().getSettings().getChannel()).getGuild().getMembers(); + Guild g = Bot.getJda().getTextChannelById(SettingsManager.getInstance().getSettings().getChannel()).getGuild(); + g.retrieveMembers().join(); // ..you MUST NOT use join()... https://i.kym-cdn.com/entries/icons/mobile/000/024/196/sign.jpg HashMap mentionables = new HashMap<>(); - for (net.dv8tion.jda.api.entities.Member member : members) { + for (net.dv8tion.jda.api.entities.Member member : g.getMembers()) { User u = member.getUser(); mentionables.put(u.getName(), u.getId()); } + LOGGER.info("We have " + mentionables.size() + " Discord members."); return mentionables; } }