Skip to content

Commit

Permalink
Move to new JDABuilder methods and rewrite Discord mention fetching.
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyBanditoz committed May 7, 2020
1 parent a6ff611 commit 17a345f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/banditoz/gmecord/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/io/banditoz/gmecord/util/BuildMentionables.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> buildGroupmeMentionables() throws IOException {
HashMap<String, String> mentionables = new HashMap<>();
String initialUrl = "https://api.groupme.com/v3/groups/" + SettingsManager.getInstance().getSettings().getGroupID() + "?token=" + SettingsManager.getInstance().getSettings().getGroupMeToken();
Expand All @@ -24,16 +30,19 @@ public static HashMap<String, String> 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<String, String> buildDiscordMentionables() {
List<net.dv8tion.jda.api.entities.Member> 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<String, String> 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;
}
}

0 comments on commit 17a345f

Please sign in to comment.