diff --git a/src/main/java/net/clementraynaud/skoice/bot/Bot.java b/src/main/java/net/clementraynaud/skoice/bot/Bot.java index 10379247..8fa85445 100644 --- a/src/main/java/net/clementraynaud/skoice/bot/Bot.java +++ b/src/main/java/net/clementraynaud/skoice/bot/Bot.java @@ -130,6 +130,15 @@ public boolean isAdministrator() { return this.status.ordinal() > BotStatus.MISSING_PERMISSION.ordinal(); } + public void allowApplicationCommands(Guild guild) { + if (guild.getSelfMember().hasPermission(Permission.ADMINISTRATOR) + && (guild.getRequiredMFALevel() != Guild.MFALevel.TWO_FACTOR_AUTH + || this.plugin.getBot().getJDA().getSelfUser().isMfaEnabled()) + && !guild.getPublicRole().hasPermission(Permission.USE_APPLICATION_COMMANDS)) { + guild.getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); + } + } + public void setDefaultAvatar() { if (this.jda.getSelfUser().getDefaultAvatarUrl().equals(this.jda.getSelfUser().getEffectiveAvatarUrl())) { this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> { diff --git a/src/main/java/net/clementraynaud/skoice/listeners/guild/GuildJoinListener.java b/src/main/java/net/clementraynaud/skoice/listeners/guild/GuildJoinListener.java index d1111be6..098528f1 100644 --- a/src/main/java/net/clementraynaud/skoice/listeners/guild/GuildJoinListener.java +++ b/src/main/java/net/clementraynaud/skoice/listeners/guild/GuildJoinListener.java @@ -20,7 +20,6 @@ package net.clementraynaud.skoice.listeners.guild; import net.clementraynaud.skoice.Skoice; -import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -34,9 +33,7 @@ public GuildJoinListener(Skoice plugin) { @Override public void onGuildJoin(GuildJoinEvent event) { - if (this.plugin.getBot().isAdministrator()) { - event.getGuild().getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); - } + this.plugin.getBot().allowApplicationCommands(event.getGuild()); this.plugin.getListenerManager().update(); } } diff --git a/src/main/java/net/clementraynaud/skoice/listeners/guild/member/GuildMemberRoleAddListener.java b/src/main/java/net/clementraynaud/skoice/listeners/guild/member/GuildMemberRoleAddListener.java index 8ae85446..4eab1b38 100644 --- a/src/main/java/net/clementraynaud/skoice/listeners/guild/member/GuildMemberRoleAddListener.java +++ b/src/main/java/net/clementraynaud/skoice/listeners/guild/member/GuildMemberRoleAddListener.java @@ -43,7 +43,7 @@ public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) { List rolesBeforeUpdate = new ArrayList<>(event.getMember().getRoles()); rolesBeforeUpdate.removeAll(event.getRoles()); if (rolesBeforeUpdate.stream().noneMatch(role -> role.hasPermission(Permission.ADMINISTRATOR))) { - event.getGuild().getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); + this.plugin.getBot().allowApplicationCommands(event.getGuild()); this.plugin.getListenerManager().update(); } } diff --git a/src/main/java/net/clementraynaud/skoice/listeners/role/update/RoleUpdatePermissionsListener.java b/src/main/java/net/clementraynaud/skoice/listeners/role/update/RoleUpdatePermissionsListener.java index f9746665..27ed5a86 100644 --- a/src/main/java/net/clementraynaud/skoice/listeners/role/update/RoleUpdatePermissionsListener.java +++ b/src/main/java/net/clementraynaud/skoice/listeners/role/update/RoleUpdatePermissionsListener.java @@ -34,18 +34,18 @@ public RoleUpdatePermissionsListener(Skoice plugin) { @Override public void onRoleUpdatePermissions(RoleUpdatePermissionsEvent event) { - if (event.getRole().isPublicRole() && !event.getRole().hasPermission(Permission.USE_APPLICATION_COMMANDS)) { - event.getGuild().getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); - } + this.plugin.getBot().allowApplicationCommands(event.getGuild()); + if (event.getGuild().getSelfMember().getRoles().contains(event.getRole())) { - if (event.getOldPermissions().contains(Permission.ADMINISTRATOR) + boolean lostAdministratorPermission = event.getOldPermissions().contains(Permission.ADMINISTRATOR) && !event.getNewPermissions().contains(Permission.ADMINISTRATOR) - && !event.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) { - this.plugin.getListenerManager().update(); - } else if (!event.getOldPermissions().contains(Permission.ADMINISTRATOR) + && !event.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR); + + boolean gainedAdministratorPermission = !event.getOldPermissions().contains(Permission.ADMINISTRATOR) && event.getNewPermissions().contains(Permission.ADMINISTRATOR) - && event.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) { - event.getGuild().getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); + && event.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR); + + if (lostAdministratorPermission || gainedAdministratorPermission) { this.plugin.getListenerManager().update(); } } diff --git a/src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java b/src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java index 2d9a6395..32dfae4a 100644 --- a/src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java +++ b/src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java @@ -24,7 +24,6 @@ import net.clementraynaud.skoice.bot.BotStatus; import net.clementraynaud.skoice.storage.config.ConfigField; import net.dv8tion.jda.api.Permission; -import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.events.session.ReadyEvent; import net.dv8tion.jda.api.exceptions.ErrorResponseException; import net.dv8tion.jda.api.exceptions.PermissionException; @@ -115,13 +114,8 @@ private void setup(Player tokenManager) { this.plugin.getBot().retrieveMutedUsers(); this.plugin.getBot().getMenuFactory().loadAll(this.plugin); - this.plugin.getBot().getJDA().getGuilds().forEach(guild -> { - if (guild.getSelfMember().hasPermission(Permission.ADMINISTRATOR) - && (guild.getRequiredMFALevel() != Guild.MFALevel.TWO_FACTOR_AUTH - || this.plugin.getBot().getJDA().getSelfUser().isMfaEnabled())) { - guild.getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue(); - } - }); + this.plugin.getBot().getJDA().getGuilds() + .forEach(guild -> this.plugin.getBot().allowApplicationCommands(guild)); this.plugin.getBot().getCommands().clearGuildCommands(); this.plugin.getBot().getCommands().register() .thenRun(() -> {