Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.1k
Migrate Discord module to JDA 5#4786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe944e8
Update Discord Module to JDA 5
JRoy 9db028b
Increase Discord module console relay message limit
JRoy 68a1150
Fix Discord module webhook closing incorrectly
JRoy f1dc6e9
Shorten logger names for Discord module console relay
JRoy e7ad980
Add backoff and skip for Discord console relay
JRoy fe613b9
Add link module to /ess dump
JRoy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10 Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 5 additions & 1 deletion
6 EssentialsDiscord/src/main/java/net/essentialsx/discord/DiscordSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 21 additions & 22 deletions
43 EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| package net.essentialsx.discord; | ||
| import club.minnced.discord.webhook.WebhookClient; | ||
| import club.minnced.discord.webhook.WebhookClientBuilder; | ||
| import club.minnced.discord.webhook.send.WebhookMessage; | ||
| import club.minnced.discord.webhook.send.WebhookMessageBuilder; | ||
| @@ -11,14 +10,14 @@ | ||
| import com.earth2me.essentials.utils.VersionUtil; | ||
| import net.dv8tion.jda.api.JDA; | ||
| import net.dv8tion.jda.api.JDABuilder; | ||
| import net.dv8tion.jda.api.entities.Emote; | ||
| import net.dv8tion.jda.api.entities.Guild; | ||
| import net.dv8tion.jda.api.entities.Role; | ||
| import net.dv8tion.jda.api.entities.TextChannel; | ||
| import net.dv8tion.jda.api.entities.Webhook; | ||
| import net.dv8tion.jda.api.events.ShutdownEvent; | ||
| import net.dv8tion.jda.api.hooks.EventListener; | ||
| import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
| import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji; | ||
| import net.dv8tion.jda.api.events.session.ShutdownEvent; | ||
| import net.dv8tion.jda.api.hooks.ListenerAdapter; | ||
| import net.dv8tion.jda.api.requests.GatewayIntent; | ||
| import net.dv8tion.jda.api.utils.cache.CacheFlag; | ||
| import net.ess3.nms.refl.providers.AchievementListenerProvider; | ||
| import net.ess3.nms.refl.providers.AdvancementListenerProvider; | ||
| @@ -46,6 +45,7 @@ | ||
| import net.essentialsx.discord.util.ConsoleInjector; | ||
| import net.essentialsx.discord.util.DiscordUtil; | ||
| import net.essentialsx.discord.util.MessageUtil; | ||
| import net.essentialsx.discord.util.WrappedWebhookClient; | ||
| import org.bukkit.Bukkit; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.HandlerList; | ||
| @@ -79,11 +79,11 @@ public class JDADiscordService implements DiscordService, IEssentialsModule { | ||
| private JDA jda; | ||
| private Guild guild; | ||
| private TextChannel primaryChannel; | ||
| private WebhookClient consoleWebhook; | ||
| private WrappedWebhookClient consoleWebhook; | ||
| private String lastConsoleId; | ||
| private final Map<String, MessageType> registeredTypes = new HashMap<>(); | ||
| private final Map<MessageType, String> typeToChannelId = new HashMap<>(); | ||
| private final Map<String, WebhookClient> channelIdToWebhook = new HashMap<>(); | ||
| private final Map<String, WrappedWebhookClient> channelIdToWebhook = new HashMap<>(); | ||
| private ConsoleInjector injector; | ||
| private DiscordCommandDispatcher commandDispatcher; | ||
| private InteractionControllerImpl interactionController; | ||
| @@ -140,7 +140,7 @@ public void sendMessage(DiscordMessageEvent event, String message, boolean group | ||
| final String webhookChannelId = typeToChannelId.get(event.getType()); | ||
| if (webhookChannelId != null) { | ||
| final WebhookClient client = channelIdToWebhook.get(webhookChannelId); | ||
| final WrappedWebhookClient client = channelIdToWebhook.get(webhookChannelId); | ||
| if (client != null) { | ||
| final String avatarUrl = event.getAvatarUrl() != null ? event.getAvatarUrl() : jda.getSelfUser().getAvatarUrl(); | ||
| final String name = event.getName() != null ? event.getName() : guild.getSelfMember().getEffectiveName(); | ||
| @@ -154,7 +154,7 @@ public void sendMessage(DiscordMessageEvent event, String message, boolean group | ||
| return; | ||
| } | ||
| channel.sendMessage(strippedContent) | ||
| .allowedMentions(groupMentions ? null : DiscordUtil.NO_GROUP_MENTIONS) | ||
| .setAllowedMentions(groupMentions ? null : DiscordUtil.NO_GROUP_MENTIONS) | ||
| .queue(); | ||
| } | ||
| @@ -169,7 +169,8 @@ public void startup() throws LoginException, InterruptedException { | ||
| jda = JDABuilder.createDefault(plugin.getSettings().getBotToken()) | ||
| .addEventListeners(new DiscordListener(this)) | ||
| .enableCache(CacheFlag.EMOTE) | ||
| .enableIntents(GatewayIntent.MESSAGE_CONTENT) | ||
| .enableCache(CacheFlag.EMOJI) | ||
| .disableCache(CacheFlag.MEMBER_OVERRIDES, CacheFlag.VOICE_STATE) | ||
| .setContextEnabled(false) | ||
| .build() | ||
| @@ -205,7 +206,7 @@ public void startup() throws LoginException, InterruptedException { | ||
| } | ||
| // Load emotes into cache, JDA will handle updates from here on out. | ||
| guild.retrieveEmotes().queue(); | ||
| guild.retrieveEmojis().queue(); | ||
JRoy marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| updatePrimaryChannel(); | ||
| @@ -323,10 +324,10 @@ public InteractionController getInteractionController() { | ||
| public void updatePrimaryChannel() { | ||
| TextChannel channel = guild.getTextChannelById(plugin.getSettings().getPrimaryChannelId()); | ||
| if (channel == null) { | ||
| channel = guild.getDefaultChannel(); | ||
| if (channel == null) { | ||
| if (!(guild.getDefaultChannel() instanceof TextChannel)) { | ||
| throw new RuntimeException(tl("discordErrorNoPerms")); | ||
| } | ||
| channel = (TextChannel) guild.getDefaultChannel(); | ||
| logger.warning(tl("discordErrorNoPrimary", channel.getName())); | ||
| } | ||
| @@ -337,7 +338,7 @@ public void updatePrimaryChannel() { | ||
| } | ||
| public String parseMessageEmotes(String message) { | ||
| for (final Emote emote : guild.getEmoteCache()) { | ||
| for (final RichCustomEmoji emote : guild.getEmojiCache()) { | ||
| message = message.replaceAll(":" + Pattern.quote(emote.getName()) + ":", emote.getAsMention()); | ||
| } | ||
| return message; | ||
| @@ -362,7 +363,7 @@ public void updatePresence() { | ||
| public void updateTypesRelay() { | ||
| if (!getSettings().isShowAvatar() && !getSettings().isShowName() && !getSettings().isShowDisplayName()) { | ||
| for (WebhookClient webhook : channelIdToWebhook.values()) { | ||
| for (WrappedWebhookClient webhook : channelIdToWebhook.values()) { | ||
| webhook.close(); | ||
| } | ||
| typeToChannelId.clear(); | ||
| @@ -382,7 +383,7 @@ public void updateTypesRelay() { | ||
| final Webhook webhook = DiscordUtil.getOrCreateWebhook(channel, DiscordUtil.ADVANCED_RELAY_NAME).join(); | ||
| if (webhook == null) { | ||
| final WebhookClient current = channelIdToWebhook.get(channel.getId()); | ||
| final WrappedWebhookClient current = channelIdToWebhook.get(channel.getId()); | ||
| if (current != null) { | ||
| current.close(); | ||
| } | ||
| @@ -483,15 +484,13 @@ public void shutdown() { | ||
| shutdownConsoleRelay(true); | ||
| for (WebhookClient webhook : channelIdToWebhook.values()) { | ||
| for (WrappedWebhookClient webhook : channelIdToWebhook.values()) { | ||
| webhook.close(); | ||
| } | ||
| // Unregister leftover jda listeners | ||
| for (Object obj : jda.getRegisteredListeners()) { | ||
| if (!(obj instanceof EventListener)) { // Yeah bro I wish I knew too :/ | ||
| jda.removeEventListener(obj); | ||
| } | ||
| jda.removeEventListener(obj); | ||
| } | ||
| // Unregister Bukkit Events | ||
| @@ -512,7 +511,7 @@ public void onShutdown(@NotNull ShutdownEvent event) { | ||
| // Wait for JDA to wrap it up | ||
| future.get(5, TimeUnit.SECONDS); | ||
| } catch (InterruptedException | ExecutionException | TimeoutException e) { | ||
| logger.warning("JDA took longer than expected to shutdown, this may have caused some problems."); | ||
| logger.log(Level.WARNING, "JDA took longer than expected to shutdown, this may have caused some problems.", e); | ||
| } finally { | ||
| jda = null; | ||
| } | ||
| @@ -588,7 +587,7 @@ public DiscordSettings getSettings() { | ||
| return plugin.getSettings(); | ||
| } | ||
| public WebhookClient getConsoleWebhook() { | ||
| public WrappedWebhookClient getConsoleWebhook() { | ||
| return consoleWebhook; | ||
| } | ||
10 changes: 5 additions & 5 deletions
10 ...ntialsDiscord/src/main/java/net/essentialsx/discord/commands/Commanddiscordbroadcast.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7 ...alsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionChannelImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10 ...Discord/src/main/java/net/essentialsx/discord/interactions/InteractionControllerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12 ...tialsDiscord/src/main/java/net/essentialsx/discord/interactions/InteractionEventImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.