Discord JDA - Invalid Member List
I assume you're using a development version of the 4.2.0 release (4.1.1_102 and above)
In these versions, the new factory methods have been introduced to make people aware of the new discord API design. In the future, bots will be limited to cache members who connected to voice channels by default.
If all you need is the count of members you can just use Guild#getMemberCount! Otherwise:
The createDefault
/createLight
will only cache members connected to voice channels or owners of guilds (on first sight). To cache more members, you will have to enable the GUILD_MEMBERS
intent in both the application dashboard for your bot and in JDA.
Now you can do something like this:
JDA api = JDABuilder.createDefault(token)
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.build();
The GUILD_MEMBERS
intent is needed because it enables the GUILD_MEMBER_REMOVE
dispatch to tell the library to remove a member from the cache when they are kicked/banned/leave.
This setup will perform lazy loading, which means it will start with only voice members and add more members to the cache once they become active.
To load all members on startup you have to additionally enable member chunking:
JDABuilder.createDefault(token)
.setChunkingFilter(ChunkingFilter.ALL) // enable member chunking for all guilds
.setMemberCachePolicy(MemberCachePolicy.ALL)
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.build();
You can also load them for individual guilds using Guild#loadMembers or Guild#findMembers.
I recommend to also read this JDA wiki article: Gateway Intents and Member Cache Policy.
JDA guild.getMembers() no longer getting all members, since few days ago
You will need to cache members to loop through them.
When using methods such as getMember()
or getMemberById()
unless its the owner of guild or the bot itself its null.
When using createLight()
or createDefault()
for building a JDA Member Caching and Member Chunking is disabled by default.
Things like User class can't be null, as it is provided by discord
For more info on caching all members read this
JDA getMembers() only returns users who wrote in the chat since the bot's startup?
The method you're using only returns the cached members, to get all the members you'll need to use Guild#loadMembers()
.
Read more about that here: https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/entities/Guild.html#loadMembers()
JDA Mentioned member - not working command
It seems like you didn't quite get the problems in your code.
final Member target = event.getMessage().getMentionedMembers().get(0);
This will throw an IndexOutOfBoundsException
if your message doesn't include a mentioned member. Because in this case the list of getMentionedMembers()
is empty. There is no object to access. get(0)
can't get anything.
In order to fix this you have to check first if the list is empty or if the length is zero as Minn already proposed. If it is you can display a message stating, that they need to add an @username.
Cleaning up your code and restructuring it accordingly would help a lot I suppose. Please go over it and make sure to always check, if the needed data exists in the first place and if you really need it. Some things are only needed for a small number of cases, calling them on every message / command is not good practice.
For example, at the moment your bot tries to get the first mentioned member from EVERY message that is sent. This isn't necessary hence you only need it in case it is the kick command.
By the way, the SelfMember
is the bot itself. In your code you are telling the user that they don't have the permission although the bot (and not necessarily the user) might lack it which could be confusing.
I didn't see it earlier: Your bot is receiving its own messages as well. You might want to check, if the author of a message is your / a bot before continuing.
In the end I would advice you to always try to understand the answer or advice that is given to you. Asking another question for the same problem isn't going to help you nor does it cast a good light on you and your attempt to learn.
Discord JDA getOnlineStatus() is not checking for online members on the server
You need to enable the GUILD_PRESENCES
intent to keep track of the online status. Add this to your builder: enableIntents(GatewayIntent.GUILD_PRESENCES)
and enable it in your application dashboard.
Related Topics
How to Draw a Tree Representing a Graph of Connected Nodes
Populate Jtable with Large Number of Rows
How to Compile a .Java with Support for Older Versions of Java
General Strategy to Resolve Java Memory Leak
Best Way to Build a Plugin System with Java
How to Express Dependency in Maven on Java Ee Features for Transition to Java 9
How to Draw an Image Over Another Image
Regex: How to Escape Backslashes and Special Characters
How Can My Java Program Store Files Inside of Its .Jar File
Performance of Stringtokenizer Class VS. String.Split Method in Java
How to Correctly Use Custom Renderers to Paint Specific Cells in a Jtable
Java: Get Current Date and Time from Server Not System Clock
How to Run a Java Program Without Main Method
Reverse a Given Sentence in Java
Including Images with an Executable Jar
How to Find the Console Width with Java
"The Public Type <<Classname>> Must Be Defined in Its Own File" Error in Eclipse