mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-02 22:47:41 +01:00
Update Discord Module to JDA 5
This commit is contained in:
parent
a1fa1e38f8
commit
697128bcf2
@ -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'
|
||||
}
|
||||
|
||||
@ -28,7 +30,7 @@ shadowJar {
|
||||
include(dependency('org.slf4j:slf4j-api'))
|
||||
|
||||
// Emoji
|
||||
include(dependency('com.vdurmont:emoji-java'))
|
||||
include(dependency('com.github.MinnDevelopment:emoji-java'))
|
||||
include(dependency('org.json:json'))
|
||||
|
||||
// discord-webhooks
|
||||
|
@ -472,7 +472,7 @@ public class DiscordSettings implements IConf {
|
||||
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"));
|
||||
|
@ -11,14 +11,15 @@ import com.earth2me.essentials.utils.NumberUtil;
|
||||
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.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.EventListener;
|
||||
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;
|
||||
@ -154,7 +155,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
return;
|
||||
}
|
||||
channel.sendMessage(strippedContent)
|
||||
.allowedMentions(groupMentions ? null : DiscordUtil.NO_GROUP_MENTIONS)
|
||||
.setAllowedMentions(groupMentions ? null : DiscordUtil.NO_GROUP_MENTIONS)
|
||||
.queue();
|
||||
}
|
||||
|
||||
@ -169,7 +170,8 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
|
||||
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 +207,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
}
|
||||
|
||||
// Load emotes into cache, JDA will handle updates from here on out.
|
||||
guild.retrieveEmotes().queue();
|
||||
guild.retrieveEmojis().queue();
|
||||
|
||||
updatePrimaryChannel();
|
||||
|
||||
@ -323,10 +325,10 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
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 +339,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
}
|
||||
|
||||
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;
|
||||
@ -483,7 +485,7 @@ public class JDADiscordService implements DiscordService, IEssentialsModule {
|
||||
|
||||
shutdownConsoleRelay(true);
|
||||
|
||||
for (WebhookClient webhook : channelIdToWebhook.values()) {
|
||||
for (WrappedWebhookClient webhook : channelIdToWebhook.values()) {
|
||||
webhook.close();
|
||||
}
|
||||
|
||||
|
@ -4,8 +4,8 @@ import com.earth2me.essentials.CommandSource;
|
||||
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;
|
||||
@ -48,7 +48,7 @@ public class Commanddiscordbroadcast extends EssentialsCommand {
|
||||
}
|
||||
|
||||
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())));
|
||||
@ -65,12 +65,12 @@ public class Commanddiscordbroadcast extends EssentialsCommand {
|
||||
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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
@ -42,7 +44,7 @@ public class InteractionControllerImpl extends ListenerAdapter implements Intera
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
@ -77,7 +79,7 @@ public class InteractionControllerImpl extends ListenerAdapter implements Intera
|
||||
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
|
||||
@ -132,7 +134,7 @@ public class InteractionControllerImpl extends ListenerAdapter implements Intera
|
||||
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());
|
||||
|
@ -2,10 +2,10 @@ package net.essentialsx.discord.interactions;
|
||||
|
||||
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;
|
||||
@ -23,11 +23,11 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
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());
|
||||
}
|
||||
@ -39,7 +39,7 @@ public class InteractionEventImpl implements InteractionEvent {
|
||||
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));
|
||||
@ -77,7 +77,7 @@ public class InteractionEventImpl implements InteractionEvent {
|
||||
@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
|
||||
|
@ -2,8 +2,8 @@ package net.essentialsx.discord.interactions;
|
||||
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.PrivateChannel;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
|
||||
import net.essentialsx.api.v2.services.discord.InteractionMember;
|
||||
import net.essentialsx.api.v2.services.discord.InteractionRole;
|
||||
import net.essentialsx.discord.util.DiscordUtil;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.essentialsx.discord.listeners;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.entities.channel.ChannelType;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.essentialsx.discord.JDADiscordService;
|
||||
import net.essentialsx.discord.util.DiscordCommandSender;
|
||||
@ -18,7 +19,11 @@ public class DiscordCommandDispatcher extends ListenerAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) {
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
|
||||
if (event.getMessage().getChannelType() != ChannelType.TEXT) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (jda.getConsoleWebhook() != null && event.getChannel().getId().equals(channelId)) {
|
||||
if ((event.isWebhookMessage() || event.getAuthor().isBot()) && (!jda.getSettings().isConsoleBotCommandRelay() || DiscordUtil.ACTIVE_WEBHOOKS.contains(event.getAuthor().getId()) || event.getAuthor().getId().equals(event.getGuild().getSelfMember().getId()))) {
|
||||
return;
|
||||
|
@ -6,7 +6,8 @@ import com.vdurmont.emoji.EmojiParser;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.entities.channel.ChannelType;
|
||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.ess3.api.IUser;
|
||||
import net.essentialsx.api.v2.events.discord.DiscordRelayEvent;
|
||||
@ -36,7 +37,11 @@ public class DiscordListener extends ListenerAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(@NotNull GuildMessageReceivedEvent event) {
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
|
||||
if (event.getMessage().getChannelType() != ChannelType.TEXT) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getAuthor().isBot() && !event.isWebhookMessage() && (!plugin.getSettings().isShowBotMessages() || event.getAuthor().getId().equals(plugin.getJda().getSelfUser().getId()))) {
|
||||
return;
|
||||
}
|
||||
@ -120,7 +125,7 @@ public class DiscordListener extends ListenerAdapter {
|
||||
// Do not create the event specific objects if there are no listeners
|
||||
if (DiscordRelayEvent.getHandlerList().getRegisteredListeners().length != 0) {
|
||||
final DiscordRelayEvent relayEvent = new DiscordRelayEvent(
|
||||
new InteractionMemberImpl(member), new InteractionChannelImpl(event.getChannel()),
|
||||
new InteractionMemberImpl(member), new InteractionChannelImpl(event.getGuildChannel()),
|
||||
Collections.unmodifiableList(keys), event.getMessage().getContentRaw(), formattedMessage, viewers);
|
||||
Bukkit.getPluginManager().callEvent(relayEvent);
|
||||
if (relayEvent.isCancelled()) {
|
||||
|
@ -13,8 +13,8 @@ import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
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.entities.channel.concrete.TextChannel;
|
||||
import net.essentialsx.api.v2.events.discord.DiscordMessageEvent;
|
||||
import net.essentialsx.api.v2.services.discord.MessageType;
|
||||
import net.essentialsx.discord.JDADiscordService;
|
||||
@ -39,7 +39,7 @@ public final class DiscordUtil {
|
||||
final ImmutableList.Builder<Message.MentionType> types = new ImmutableList.Builder<>();
|
||||
types.add(Message.MentionType.USER);
|
||||
types.add(Message.MentionType.CHANNEL);
|
||||
types.add(Message.MentionType.EMOTE);
|
||||
types.add(Message.MentionType.EMOJI);
|
||||
NO_GROUP_MENTIONS = types.build();
|
||||
}
|
||||
|
||||
|
@ -4,13 +4,11 @@ dependencyResolutionManagement {
|
||||
maven("https://papermc.io/repo/repository/maven-public/")
|
||||
maven("https://jitpack.io") {
|
||||
content { includeGroup("com.github.milkbowl") }
|
||||
content { includeGroup("com.github.MinnDevelopment") }
|
||||
}
|
||||
maven("https://repo.codemc.org/repository/maven-public") {
|
||||
content { includeGroup("org.bstats") }
|
||||
}
|
||||
maven("https://m2.dv8tion.net/releases/") {
|
||||
content { includeGroup("net.dv8tion") }
|
||||
}
|
||||
maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") {
|
||||
content { includeGroup("me.clip") }
|
||||
}
|
||||
@ -18,6 +16,7 @@ dependencyResolutionManagement {
|
||||
content { includeGroup("com.mojang") }
|
||||
}
|
||||
mavenCentral {
|
||||
content { includeGroup("net.dv8tion") }
|
||||
content { includeGroup("net.kyori") }
|
||||
content { includeGroup("org.apache.logging.log4j") }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user