Discord Bot Java API (net.dv8tion) Permission Problems

Viewed 62

i'm trying to make a discord bot that can add/delete roles of users but sadly i got some permission problems.

API can be found: https://github.com/DV8FromTheWorld/JDA/

I would love to hear of someone who has an idea of what to do. I have not found anyone with the same problem.

The "exiting" part of the error looks like the following:

net.dv8tion.jda.api.exceptions.HierarchyException: Can't modify a role with higher or equal highest role than yourself! Role: R:unverified(1014845515776671764)
at net.dv8tion.jda.internal.entities.GuildImpl.checkPosition(GuildImpl.java:1866)
at net.dv8tion.jda.internal.entities.GuildImpl.addRoleToMember(GuildImpl.java:1535)
at Verify.onMessageReceived(Verify.java:29)

What i've already checked:

  • in the https://discord.com/developers/applications - bot section i marked the Privileged Gateway Intents stuff
  • in the invite for my bot on the server i checked the administrator permissions
  • in the invite i also checked the guild-related scopes in addition to bot and application.commands scopes (but if i just check all of the scopes the invite link doesnt work so i concentrated on the ones i think are important ... whatever)
  • in the source code i enabled the GatewayIntents (see source code below) but i think it's not needed since i set them already in the previous points
  • EDIT: i also tried to do the addRoleThingy with a new Account that has no roles at all on the server -> received the exact same error
  • EDIT: when i want to kick the other account with the bot it works as intended even though you need some higher permission for that...

Main.java:

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import javax.security.auth.login.LoginException;


public class Main extends ListenerAdapter {
    public static Channel verifyingChannel = null;
    public static Role unverified = null;
    public static Role verified = null;

    public static void main(String[] args) throws LoginException {

        String token = "MTAxNDc5NDM3NTc5ODIwNjU2NA.GbfY1_.xBGsGKK5Ox1jzFVYwIp_cMU3kMcOvNnq2dKOQM";
        JDABuilder builder = JDABuilder.createDefault(token);

        builder.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT,GatewayIntent.GUILD_PRESENCES);
        builder.setStatus(OnlineStatus.ONLINE);
        builder.setActivity(Activity.listening("the sound of life"));

        builder.setMemberCachePolicy(MemberCachePolicy.ALL);
        builder.setChunkingFilter(ChunkingFilter.ALL);
        builder.enableCache(CacheFlag.ROLE_TAGS);
        JDA bot = builder.build().awaitReady();
        bot.addEventListener(new Verify());

        // don't worry about the next one, it adds some commands that just work already
        bot.addEventListener(new Commander(bot));

        System.out.println("Bot is online!");

    }

}

Verify.java

import net.dv8tion.jda.api.entities.Channel;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

public class Verify extends ListenerAdapter {

    public Verify(){}

    @Override
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        // later: implement the addRoleToMember funtion here
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event){
        // The Role Main.unverified is first set with a setup command in the Commander-Class, that part is not the problem
        event.getGuild().addRoleToMember(event.getMember(),Main.unverified).queue();
    }

}
2 Answers

You simply cannot modify a user with higher or equal permissions as shown in the error log.

I assume you are an admin on your server where you test the bot.
Therefore, you and the bot have the same/equal permissions which goes against the fact that ...

..."You simply cannot modify a user with higher or equal permissions as shown in the error log"



I would recommend that you invite another user to the server and give him lower permissions. If you do so, the bot should be able to modify the new user with no issues.

So, to finally fix my problem i simply rearranged the roles on the server. In the server properties the bot role had to be above the role i wanted to apply.

Related