Skip to content

Commit 142a960

Browse files
committed
Improve bot permission management
1 parent bba6d28 commit 142a960

File tree

7 files changed

+21
-19
lines changed

7 files changed

+21
-19
lines changed

src/main/java/net/clementraynaud/skoice/bot/Bot.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,7 @@ public void connect(CommandSender sender) {
126126
}
127127

128128
public boolean isAdministrator() {
129-
Guild guild = this.plugin.getBot().getGuild();
130-
if (guild == null) {
131-
return false;
132-
}
133-
134-
if (this.getGuild().getRequiredMFALevel() == Guild.MFALevel.TWO_FACTOR_AUTH
135-
&& !this.jda.getSelfUser().isMfaEnabled()) {
136-
return false;
137-
}
138-
139-
return guild.getSelfMember().hasPermission(Permission.ADMINISTRATOR);
129+
return this.status.ordinal() > BotStatus.MISSING_PERMISSION.ordinal();
140130
}
141131

142132
public void setDefaultAvatar() {
@@ -211,6 +201,7 @@ public void updateStatus() {
211201
if (!this.plugin.getConfigYamlFile().contains(ConfigField.TOKEN.toString())) {
212202
this.plugin.getLogger().warning(this.plugin.getLang().getMessage("logger.warning.no-token"));
213203
}
204+
214205
} else {
215206
if (this.guildId == null) {
216207
List<Guild> guilds = this.jda.getGuilds();
@@ -224,30 +215,36 @@ public void updateStatus() {
224215
this.status = BotStatus.MULTIPLE_GUILDS;
225216
this.plugin.getLogger().warning(this.plugin.getLang().getMessage("logger.warning.multiple-guilds"));
226217
}
218+
227219
} else if (this.getGuild().getRequiredMFALevel() == Guild.MFALevel.TWO_FACTOR_AUTH
228220
&& !this.jda.getSelfUser().isMfaEnabled()) {
229221
this.status = BotStatus.MFA_REQUIRED;
230222
this.plugin.getLogger().warning(this.plugin.getLang().getMessage("logger.warning.two-factor-authentication"));
231-
} else if (!this.isAdministrator()) {
223+
224+
} else if (!this.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
232225
this.status = BotStatus.MISSING_PERMISSION;
233226
this.jda.retrieveApplicationInfo().queue(applicationInfo -> {
234227
applicationInfo.setRequiredScopes("applications.commands");
235228
this.plugin.getLogger().severe(this.plugin.getLang().getMessage("logger.error.missing-permission", applicationInfo.getInviteUrl(Permission.ADMINISTRATOR)));
236229
});
230+
237231
} else if (!this.plugin.getConfigYamlFile().contains(ConfigField.VOICE_CHANNEL_ID.toString())) {
238232
this.status = BotStatus.NO_VOICE_CHANNEL;
239233
this.plugin.getLogger().warning(this.plugin.getLang().getMessage("logger.warning.no-voice-channel"));
234+
240235
} else if (!this.plugin.getConfigYamlFile().contains(ConfigField.HORIZONTAL_RADIUS.toString())
241236
|| !this.plugin.getConfigYamlFile().contains(ConfigField.VERTICAL_RADIUS.toString())) {
242237
this.status = BotStatus.NO_RADIUS;
243238
this.plugin.getLogger().warning(this.plugin.getLang().getMessage("logger.warning.no-radius"));
239+
244240
} else {
245241
this.status = BotStatus.READY;
246242
this.plugin.getServer().getScheduler().runTask(this.plugin, () -> {
247243
SystemReadyEvent event = new SystemReadyEvent();
248244
this.plugin.getServer().getPluginManager().callEvent(event);
249245
});
250246
}
247+
251248
this.updateActivity();
252249
}
253250
}

src/main/java/net/clementraynaud/skoice/bot/BotVoiceChannel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public VoiceChannel getVoiceChannel() {
5656
}
5757

5858
public void setStatus() {
59-
if (!this.plugin.getBot().isAdministrator()) {
59+
if (this.plugin.getBot().isAdministrator()) {
6060
return;
6161
}
6262
VoiceChannel voiceChannel = this.getVoiceChannel();

src/main/java/net/clementraynaud/skoice/listeners/channel/main/GenericChannelListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private void checkForValidVoiceChannel(GenericChannelEvent event) {
122122
this.plugin.getConfigYamlFile().remove(ConfigField.VOICE_CHANNEL_ID.toString());
123123
this.plugin.getListenerManager().update();
124124

125-
if (!event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
125+
if (!this.plugin.getBot().isAdministrator()) {
126126
return;
127127
}
128128
event.getGuild().retrieveAuditLogs()

src/main/java/net/clementraynaud/skoice/listeners/channel/network/GenericChannelListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void onChannelDelete(ChannelDeleteEvent event) {
5555

5656
ProximityChannels.remove(channel.getId());
5757

58-
if (!event.getGuild().getSelfMember().hasPermission(Permission.VIEW_AUDIT_LOGS)) {
58+
if (!this.plugin.getBot().isAdministrator()) {
5959
return;
6060
}
6161
event.getGuild().retrieveAuditLogs()

src/main/java/net/clementraynaud/skoice/listeners/guild/GuildJoinListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public GuildJoinListener(Skoice plugin) {
3434

3535
@Override
3636
public void onGuildJoin(GuildJoinEvent event) {
37-
if (event.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
37+
if (this.plugin.getBot().isAdministrator()) {
3838
event.getGuild().getPublicRole().getManager().givePermissions(Permission.USE_APPLICATION_COMMANDS).queue();
3939
}
4040
this.plugin.getBot().updateGuild();

src/main/java/net/clementraynaud/skoice/listeners/session/ReadyListener.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.dv8tion.jda.api.entities.Guild;
2828
import net.dv8tion.jda.api.events.session.ReadyEvent;
2929
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
30+
import net.dv8tion.jda.api.exceptions.PermissionException;
3031
import net.dv8tion.jda.api.hooks.ListenerAdapter;
3132
import net.dv8tion.jda.api.requests.ErrorResponse;
3233
import net.dv8tion.jda.api.requests.RestAction;
@@ -66,10 +67,15 @@ public void onReady(ReadyEvent event) {
6667
RestAction.setDefaultFailure(throwable -> {
6768
if (throwable instanceof ErrorResponseException) {
6869
ErrorResponseException error = (ErrorResponseException) throwable;
69-
if (error.getErrorCode() == ErrorResponse.MFA_NOT_ENABLED.getCode()) {
70+
if (error.getErrorCode() == ErrorResponse.MISSING_PERMISSIONS.getCode()
71+
|| error.getErrorCode() == ErrorResponse.MISSING_ACCESS.getCode()
72+
|| error.getErrorCode() == ErrorResponse.MFA_NOT_ENABLED.getCode()) {
7073
this.plugin.getListenerManager().update();
7174
return;
7275
}
76+
} else if (throwable instanceof PermissionException) {
77+
this.plugin.getListenerManager().update();
78+
return;
7379
}
7480

7581
defaultFailure.accept(throwable);

src/main/java/net/clementraynaud/skoice/tasks/UpdateVoiceStateTask.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ public void run() {
7575
if (isMainVoiceChannel) {
7676
if (!this.member.getVoiceState().isGuildMuted()
7777
&& this.member.hasPermission(this.channel, Permission.VOICE_SPEAK, Permission.VOICE_MUTE_OTHERS)
78-
&& this.channel.getGuild().getSelfMember().hasPermission(this.channel, Permission.VOICE_MUTE_OTHERS)
79-
&& this.channel.getGuild().getSelfMember().hasPermission(voiceChannel.getParentCategory(), Permission.VOICE_MOVE_OTHERS)) {
78+
&& this.plugin.getBot().isAdministrator()) {
8079
this.member.mute(true).queue(success -> {
8180
UpdateVoiceStateTask.mutedUsers.add(this.member.getId());
8281
this.plugin.getTempYamlFile().set(TempYamlFile.MUTED_USERS_ID_FIELD,

0 commit comments

Comments
 (0)