[FEAT] Allow Discord Mentions to be configurable

This commit is contained in:
2023-07-02 16:30:20 +02:00
parent a88a6826dd
commit a30970572a
4 changed files with 132 additions and 1 deletions

View File

@@ -89,4 +89,7 @@ public class ChatSettingsConfig {
@SpecComment("Commands that should not be broadcasted to discord") @SpecComment("Commands that should not be broadcasted to discord")
public List<String> ignoredCommands = new ArrayList<String>() {{ add("particle"); add("login"); add("execute"); }}; public List<String> ignoredCommands = new ArrayList<String>() {{ add("particle"); add("login"); add("execute"); }};
@Path("allowMentionsFromChat")
@SpecComment("Allow mentioning discord roles, users and channels from Minecraft Chat")
public boolean allowMentionsFromChat = false;
} }

View File

@@ -10,11 +10,18 @@ import com.hypherionmc.sdlink.core.discord.commands.slash.general.ServerStatusSl
import com.hypherionmc.sdlink.core.discord.hooks.BotReadyHooks; import com.hypherionmc.sdlink.core.discord.hooks.BotReadyHooks;
import com.hypherionmc.sdlink.core.discord.hooks.DiscordMessageHooks; import com.hypherionmc.sdlink.core.discord.hooks.DiscordMessageHooks;
import com.hypherionmc.sdlink.core.events.SDLinkReadyEvent; import com.hypherionmc.sdlink.core.events.SDLinkReadyEvent;
import com.hypherionmc.sdlink.core.managers.CacheManager;
import com.hypherionmc.sdlink.core.managers.ChannelManager; import com.hypherionmc.sdlink.core.managers.ChannelManager;
import com.hypherionmc.sdlink.core.managers.PermissionChecker; import com.hypherionmc.sdlink.core.managers.PermissionChecker;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.role.RoleCreateEvent;
import net.dv8tion.jda.api.events.role.RoleDeleteEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent; import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@@ -57,6 +64,7 @@ public class DiscordEventHandler extends ListenerAdapter {
BotReadyHooks.startActivityUpdates(event); BotReadyHooks.startActivityUpdates(event);
BotReadyHooks.startTopicUpdates(); BotReadyHooks.startTopicUpdates();
CraterEventBus.INSTANCE.postEvent(new SDLinkReadyEvent()); CraterEventBus.INSTANCE.postEvent(new SDLinkReadyEvent());
CacheManager.loadCache();
} }
} }
@@ -70,4 +78,46 @@ public class DiscordEventHandler extends ListenerAdapter {
event.reply("Success!").setEphemeral(true).queue(); event.reply("Success!").setEphemeral(true).queue();
} }
} }
@Override
public void onGuildJoin(@NotNull GuildJoinEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadUserCache();
}
}
@Override
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadUserCache();
}
}
@Override
public void onRoleCreate(@NotNull RoleCreateEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadRoleCache();
}
}
@Override
public void onRoleDelete(@NotNull RoleDeleteEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadRoleCache();
}
}
@Override
public void onChannelCreate(@NotNull ChannelCreateEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadChannelCache();
}
}
@Override
public void onChannelDelete(@NotNull ChannelDeleteEvent event) {
if (event.getJDA().getStatus() == JDA.Status.CONNECTED) {
CacheManager.loadChannelCache();
}
}
} }

View File

@@ -0,0 +1,75 @@
package com.hypherionmc.sdlink.core.managers;
import com.hypherionmc.sdlink.core.discord.BotController;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import java.util.HashMap;
public class CacheManager {
private static final HashMap<String, String> serverChannels = new HashMap<>();
private static final HashMap<String, String> serverRoles = new HashMap<>();
private static final HashMap<String, String> userCache = new HashMap<>();
public static void loadCache() {
loadChannelCache();
loadRoleCache();
loadUserCache();
}
public static void loadChannelCache() {
serverChannels.clear();
JDA jda = BotController.INSTANCE.getJDA();
if (jda.getGuilds().isEmpty())
return;
jda.getGuilds().get(0).getChannels(false).forEach(c -> {
if (c.getType() != ChannelType.CATEGORY) {
serverChannels.put("#" + c.getName(), c.getAsMention());
}
});
}
public static void loadRoleCache() {
serverRoles.clear();
JDA jda = BotController.INSTANCE.getJDA();
if (jda.getGuilds().isEmpty())
return;
jda.getGuilds().get(0).getRoles().forEach(r -> {
if (r.isMentionable() && !r.isManaged()) {
serverRoles.put("@" + r.getName(), r.getAsMention());
}
});
}
public static void loadUserCache() {
userCache.clear();
JDA jda = BotController.INSTANCE.getJDA();
if (jda.getGuilds().isEmpty())
return;
jda.getGuilds().get(0).getMembers().forEach(r -> {
userCache.put("@" + r.getEffectiveName(), r.getAsMention());
});
}
public static HashMap<String, String> getServerChannels() {
return serverChannels;
}
public static HashMap<String, String> getServerRoles() {
return serverRoles;
}
public static HashMap<String, String> getUserCache() {
return userCache;
}
}

View File

@@ -48,9 +48,12 @@ public final class DiscordMessageBuilder {
*/ */
public DiscordMessageBuilder message(String message) { public DiscordMessageBuilder message(String message) {
if (this.messageType == MessageType.CHAT) { if (this.messageType == MessageType.CHAT) {
message = message.replace("<@", "");
message = message.replace("@everyone", ""); message = message.replace("@everyone", "");
message = message.replace("@here", ""); message = message.replace("@here", "");
if (!SDLinkConfig.INSTANCE.chatConfig.allowMentionsFromChat) {
message = message.replace("<@", "");
}
} }
if (SDLinkConfig.INSTANCE.ignoreConfig.enabled) { if (SDLinkConfig.INSTANCE.ignoreConfig.enabled) {