How can I find out who invited the user using JDA?

68 views Asked by At

I was trying to find out who invited a user to my discord server, but JDA is not correctly displaying the list of who invited.

I was trying to find out who invited the user using:

List<Invite> invites = event.getGuild().retrieveInvites().complete();
            for (Invite invite : invites) {
               System.out.println(invite.getInviter);
            }

and get the invitee's id in the same way:

            for (Invite invite : invites) {
               System.out.println("Inviter: " + invite.getInviter + "User: " + event.getUser.getId());
            }

But this code shows all invites.

2

There are 2 answers

0
michel929 On

its not possible right now... You can't get a User from the InviteCode or reverse.

At least not in an easy way - you have to program it yourself via logging. You can find the complete explanation here:

Discord JDA - How to check how many users a user invited

0
BETA On

Hey here is a way to get inviter as rest action

    private RestAction<User> getInviter(Guild guild, User user) {
        return guild.retrieveAuditLogs().type(ActionType.INVITE_CREATE).map(auditLogs -> {
            for (AuditLogEntry entry : auditLogs) {
                if (entry.getTargetId().equals(user.getId())) {
                    return entry.getUser(); // Returns the inviter
                }
            }
    
            // AuditLogEntry not found
            return null;
        });
    }

some examples of using it:

// Sync
User user = getInviter(guild, user).complete();
if (user == null) {
  // Do something
  return;
}

// Do something

// Async
getInviter(guild, user).queue(user -> {
  if (user == null) {
    // Do something
    return;
  }
  
  // Do something
});

the getInviter method is to find the user who invited another user to a specific Discord server (guild).

How it Works:

  1. Retrieving Audit Logs: The method starts by retrieving the audit logs of the specified guild using guild.retrieveAuditLogs(). Audit logs contain records of various actions performed within the guild, including user invitations.

  2. Filtering Audit Logs: It filters the audit logs to include only entries of type INVITE_CREATE using .type(ActionType.INVITE_CREATE). This step ensures that only actions related to creating invites are considered.

  3. Iterating through Audit Log Entries: It iterates through each AuditLogEntry within the filtered audit logs. An AuditLogEntry represents a specific action that occurred in the guild, such as creating an invite.

  4. Finding the Inviter: For each entry, it checks if the target ID of the action matches the ID of the user for whom the inviter is being sought (user.getId()). If a match is found, it retrieves the user associated with that audit log entry (entry.getUser()), which represents the inviter. If no matching entry is found, it returns null, indicating that the inviter could not be determined from the audit logs.

Synchronous usage

  • In synchronous usage, the Inviter retrieval operation is completed immediately, and the retrieved user (or null if not found) is returned.

  • This approach is suitable for scenarios where you need the inviter information synchronously and can afford to block the current thread until the operation completes.

Asynchronous

  • In asynchronous usage, a callback (Consumer) is provided to handle the retrieved inviter once the operation completes.

  • This approach is preferable in event-driven or non-blocking scenarios where you don't want to block the thread and instead handle the result asynchronously.