Skip to content

Commit

Permalink
Ensure users can use application commands
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCdry committed Sep 9, 2024
1 parent 5ea14c0 commit 91efb19
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/clementraynaud/skoice/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
List<Role> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(() -> {
Expand Down

0 comments on commit 91efb19

Please sign in to comment.