Skip to content

Commit ca07b53

Browse files
authored
Add message when discord bot is missing permissions (#6228)
Closes #5450
1 parent e086064 commit ca07b53

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Essentials/src/main/resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ discordErrorLoggerNoPerms=Discord console logger has been disabled due to insuff
276276
discordErrorNoGuild=Invalid or missing server ID\! Please follow the tutorial in the config in order to setup the plugin.
277277
discordErrorNoGuildSize=Your bot is not in any servers\! Please follow the tutorial in the config in order to setup the plugin.
278278
discordErrorNoPerms=Your bot cannot see or talk in any channel\! Please make sure your bot has read and write permissions in all channels you wish to use.
279+
discordErrorInvalidPerms=Your bot was not set up with the required permissions\! Missing permissions are\: {0}.
279280
discordErrorNoPrimary=You did not define a primary channel or your defined primary channel is invalid. Falling back to the default channel\: \#{0}.
280281
discordErrorNoPrimaryPerms=Your bot cannot speak in your primary channel, \#{0}. Please make sure your bot has read and write permissions in all channels you wish to use.
281282
discordErrorNoToken=No token provided\! Please follow the tutorial in the config in order to setup the plugin.

EssentialsDiscord/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
dependencies {
66
compileOnly project(':EssentialsX')
7-
implementation('net.dv8tion:JDA:5.3.0') {
7+
implementation('net.dv8tion:JDA:5.6.1') {
88
exclude(module: 'opus-java')
99
}
1010
implementation 'com.github.MinnDevelopment:emoji-java:v6.1.0'

EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import com.earth2me.essentials.utils.FormatUtil;
99
import com.earth2me.essentials.utils.NumberUtil;
1010
import com.earth2me.essentials.utils.VersionUtil;
11+
import com.google.common.collect.ImmutableList;
1112
import com.neovisionaries.ws.client.ProxySettings;
1213
import com.neovisionaries.ws.client.WebSocketFactory;
1314
import net.dv8tion.jda.api.JDA;
1415
import net.dv8tion.jda.api.JDABuilder;
16+
import net.dv8tion.jda.api.Permission;
1517
import net.dv8tion.jda.api.entities.Guild;
1618
import net.dv8tion.jda.api.entities.Role;
1719
import net.dv8tion.jda.api.entities.Webhook;
@@ -200,6 +202,17 @@ public void startup() throws LoginException, InterruptedException {
200202
throw new IllegalArgumentException(tlLiteral("discordErrorNoGuild"));
201203
}
202204

205+
final Collection<Permission> requiredPermissions = ImmutableList.of(Permission.MANAGE_WEBHOOKS, Permission.MANAGE_ROLES, Permission.NICKNAME_MANAGE, Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND, Permission.MESSAGE_EMBED_LINKS);
206+
final String[] missingPermissions = requiredPermissions.stream()
207+
.filter(permission -> !guild.getSelfMember().hasPermission(permission))
208+
.map(Permission::getName)
209+
.toArray(String[]::new);
210+
211+
if (missingPermissions.length > 0) {
212+
invalidStartup = true;
213+
throw new IllegalArgumentException(tlLiteral("discordErrorInvalidPerms", String.join(", ", missingPermissions)));
214+
}
215+
203216
interactionController = new InteractionControllerImpl(this);
204217
// Each will throw an exception if disabled
205218
try {

0 commit comments

Comments
 (0)