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:
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();
}
}
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.