Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
files.add(new PasteUtil.PasteFile("dump.json", dump.toString()));

final Plugin essDiscord = Bukkit.getPluginManager().getPlugin("EssentialsDiscord");
final Plugin essDiscordLink = Bukkit.getPluginManager().getPlugin("EssentialsDiscordLink");
final Plugin essSpawn = Bukkit.getPluginManager().getPlugin("EssentialsSpawn");

final Map<String, Command> knownCommandsCopy = new HashMap<>(ess.getKnownCommandsProvider().getKnownCommands());
Expand Down Expand Up @@ -389,6 +390,15 @@ private void runDump(Server server, CommandSource sender, String commandLabel, S
} catch (IOException e) {
sender.sendMessage(tl("dumpErrorUpload", "discord-config.yml", e.getMessage()));
}

if (essDiscordLink != null) {
try {
files.add(new PasteUtil.PasteFile("discord-link-config.yml",
new String(Files.readAllBytes(essDiscordLink.getDataFolder().toPath().resolve("config.yml")), StandardCharsets.UTF_8)));
} catch (IOException e) {
sender.sendMessage(tl("dumpErrorUpload", "discord-link-config.yml", e.getMessage()));
}
}
}

if (kits) {
Expand Down
15 changes: 10 additions & 5 deletions EssentialsDiscord/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ plugins {

dependencies {
compileOnly project(':EssentialsX')
implementation('net.dv8tion:JDA:4.4.1_353') {
implementation('net.dv8tion:JDA:5.0.0-beta.12') {
exclude(module: 'opus-java')
}
implementation 'com.vdurmont:emoji-java:5.1.1'
implementation 'club.minnced:discord-webhooks:0.7.2'
compileOnly 'org.apache.logging.log4j:log4j-core:2.15.0'
implementation 'com.github.MinnDevelopment:emoji-java:v6.1.0'
implementation('club.minnced:discord-webhooks:0.8.2') {
exclude(module: 'okhttp')
}
compileOnly 'org.apache.logging.log4j:log4j-core:2.17.1'
compileOnly 'me.clip:placeholderapi:2.10.9'
}

Expand All @@ -20,15 +22,17 @@ shadowJar {
include(dependency('com.neovisionaries:nv-websocket-client'))
include(dependency('com.squareup.okhttp3:okhttp'))
include(dependency('com.squareup.okio:okio'))
include(dependency('com.squareup.okio:okio-jvm'))
include(dependency('org.apache.commons:commons-collections4'))
include(dependency('net.sf.trove4j:trove4j'))
include(dependency('com.fasterxml.jackson.core:jackson-databind'))
include(dependency('com.fasterxml.jackson.core:jackson-core'))
include(dependency('com.fasterxml.jackson.core:jackson-annotations'))
include(dependency('org.slf4j:slf4j-api'))
include(dependency('org.jetbrains.kotlin:kotlin-stdlib'))

// Emoji
include(dependency('com.vdurmont:emoji-java'))
include(dependency('com.github.MinnDevelopment:emoji-java'))
include(dependency('org.json:json'))

// discord-webhooks
Expand All @@ -47,6 +51,7 @@ shadowJar {
relocate 'com.fasterxml.jackson.core', 'net.essentialsx.dep.com.fasterxml.jackson.core'
relocate 'com.fasterxml.jackson.annotation', 'net.essentialsx.dep.com.fasterxml.jackson.annotation'
relocate 'gnu.trove', 'net.essentialsx.dep.gnu.trove'
relocate 'kotlin', 'net.essentialsx.dep.garbage.kotlin'

// Emoji
relocate 'com.vdurmont.emoji', 'net.essentialsx.dep.com.vdurmont.emoji'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ public List<Pattern> getConsoleFilters() {
return consoleFilter;
}

public int getConsoleSkipDelay() {
return config.getInt("console.skip-delay", 2);
}

public boolean isShowAvatar() {
return config.getBoolean("show-avatar", false);
}
Expand Down Expand Up @@ -472,7 +476,7 @@ public void reloadConfig() {
activityType = Activity.ActivityType.valueOf(activity);
}
} catch (IllegalArgumentException e) {
activityType = Activity.ActivityType.DEFAULT;
activityType = Activity.ActivityType.PLAYING;
}
if (activityType != null) {
statusActivity = Activity.of(activityType, config.getString("presence.message", "Minecraft"));
Expand Down
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}

Expand All @@ -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()
Expand Down Expand Up @@ -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();

updatePrimaryChannel();

Expand Down Expand Up @@ -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()));
}

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -588,7 +587,7 @@ public DiscordSettings getSettings() {
return plugin.getSettings();
}

public WebhookClient getConsoleWebhook() {
public WrappedWebhookClient getConsoleWebhook() {
return consoleWebhook;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.earth2me.essentials.commands.EssentialsCommand;
import com.earth2me.essentials.commands.NotEnoughArgumentsException;
import com.vdurmont.emoji.EmojiParser;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import net.essentialsx.discord.JDADiscordService;
import net.essentialsx.discord.util.DiscordUtil;
import net.essentialsx.discord.util.MessageUtil;
Expand Down Expand Up @@ -48,7 +48,7 @@ protected void run(Server server, CommandSource sender, String commandLabel, Str
}

channel.sendMessage(jda.parseMessageEmotes(message))
.allowedMentions(sender.isAuthorized("essentials.discordbroadcast.ping", ess) ? null : DiscordUtil.NO_GROUP_MENTIONS)
.setAllowedMentions(sender.isAuthorized("essentials.discordbroadcast.ping", ess) ? null : DiscordUtil.NO_GROUP_MENTIONS)
.queue();

sender.sendMessage(tl("discordbroadcastSent", "#" + EmojiParser.parseToAliases(channel.getName())));
Expand All @@ -65,12 +65,12 @@ protected List<String> getTabCompleteOptions(Server server, CommandSource sender
final String curArg = args[args.length - 1];
if (!curArg.isEmpty() && curArg.charAt(0) == ':' && (curArg.length() == 1 || curArg.charAt(curArg.length() - 1) != ':')) {
final JDADiscordService jda = (JDADiscordService) module;
if (jda.getGuild().getEmoteCache().isEmpty()) {
if (jda.getGuild().getEmojiCache().isEmpty()) {
return Collections.emptyList();
}

final List<String> completions = new ArrayList<>();
for (final Emote emote : jda.getGuild().getEmoteCache()) {
for (final RichCustomEmoji emote : jda.getGuild().getEmojiCache()) {
completions.add(":" + emote.getName() + ":");
}
return completions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.essentialsx.discord.interactions;

import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.essentialsx.api.v2.services.discord.InteractionChannel;

public class InteractionChannelImpl implements InteractionChannel {
private final GuildChannel channel;
private final GuildMessageChannel channel;

public InteractionChannelImpl(GuildChannel channel) {
public InteractionChannelImpl(GuildMessageChannel channel) {
this.channel = channel;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.essentialsx.discord.interactions;

import com.earth2me.essentials.utils.StringUtil;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.requests.ErrorResponse;
import net.essentialsx.api.v2.services.discord.InteractionCommand;
import net.essentialsx.api.v2.services.discord.InteractionCommandArgument;
Expand Down Expand Up @@ -42,7 +44,7 @@ public InteractionControllerImpl(JDADiscordService jda) {
}

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
if (event.getGuild() == null || event.getMember() == null || !commandMap.containsKey(event.getName())) {
return;
}
Expand Down Expand Up @@ -77,7 +79,7 @@ public void processBatchRegistration() {
for (final InteractionCommand command : batchRegistrationQueue.values()) {
// German is quite the language
final String description = StringUtil.abbreviate(command.getDescription(), 100);
final CommandData data = new CommandData(command.getName(), description);
final SlashCommandData data = Commands.slash(command.getName(), description);
if (command.getArguments() != null) {
for (final InteractionCommandArgument argument : command.getArguments()) {
// German doesn't support spaces between words
Expand Down Expand Up @@ -132,7 +134,7 @@ public void registerCommand(InteractionCommand command) throws InteractionExcept
return;
}

final CommandData data = new CommandData(command.getName(), command.getDescription());
final SlashCommandData data = Commands.slash(command.getName(), command.getDescription());
if (command.getArguments() != null) {
for (final InteractionCommandArgument argument : command.getArguments()) {
data.addOption(OptionType.valueOf(argument.getType().name()), argument.getName(), argument.getDescription(), argument.isRequired());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import com.earth2me.essentials.utils.FormatUtil;
import com.google.common.base.Joiner;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.utils.messages.MessageEditBuilder;
import net.essentialsx.api.v2.services.discord.InteractionChannel;
import net.essentialsx.api.v2.services.discord.InteractionEvent;
import net.essentialsx.api.v2.services.discord.InteractionMember;
Expand All @@ -23,11 +23,11 @@
*/
public class InteractionEventImpl implements InteractionEvent {
private final static Logger logger = EssentialsDiscord.getWrappedLogger();
private final SlashCommandEvent event;
private final SlashCommandInteractionEvent event;
private final InteractionMember member;
private final List<String> replyBuffer = new ArrayList<>();

public InteractionEventImpl(final SlashCommandEvent jdaEvent) {
public InteractionEventImpl(final SlashCommandInteractionEvent jdaEvent) {
this.event = jdaEvent;
this.member = new InteractionMemberImpl(jdaEvent.getMember());
}
Expand All @@ -39,7 +39,7 @@ public void reply(String message) {
String reply = Joiner.on('\n').join(replyBuffer);
reply = reply.substring(0, Math.min(Message.MAX_CONTENT_LENGTH, reply.length()));
event.getHook().editOriginal(
new MessageBuilder()
new MessageEditBuilder()
.setContent(reply)
.setAllowedMentions(DiscordUtil.NO_GROUP_MENTIONS).build())
.queue(null, error -> logger.log(Level.SEVERE, "Error while editing command interaction response", error));
Expand Down Expand Up @@ -77,7 +77,7 @@ public InteractionMember getUserArgument(String key) {
@Override
public InteractionChannel getChannelArgument(String key) {
final OptionMapping mapping = event.getOption(key);
return mapping == null ? null : new InteractionChannelImpl(mapping.getAsGuildChannel());
return mapping == null ? null : new InteractionChannelImpl(mapping.getAsChannel().asGuildMessageChannel());
}

@Override
Expand Down
Loading