[FEAT] Initial preparations for custom embeds and custom message types

This commit is contained in:
2023-07-23 00:32:31 +02:00
parent 5e90d1ef38
commit 42e8adcfae
10 changed files with 68 additions and 36 deletions

View File

@@ -100,7 +100,8 @@ shadowJar {
relocate 'oshi', shade_group + 'oshi'
relocate 'kotlin', shade_group + 'kotlin'
relocate 'org.jasypt', shade_group + 'jasypt'
relocate 'com.google', shade_group + 'google'
relocate 'com.google.common', shade_group + 'google.common'
relocate 'com.google.thirdparty', shade_group + 'google.thirdparty'
relocate 'edu', shade_group + 'edu'
relocate 'io', shade_group + 'io'
relocate 'javassist', shade_group + 'javassist'

View File

@@ -1,6 +1,6 @@
version_major=0
version_minor=0
version_patch=10
version_patch=12
shade_group=com.hypherionmc.sdlink.shaded.

View File

@@ -10,9 +10,11 @@ import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.managers.RoleManager;
import com.hypherionmc.sdlink.core.messaging.Result;
import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
import com.hypherionmc.sdlink.core.util.SystemUtils;
import com.mojang.authlib.GameProfile;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.UserSnowflake;
import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONException;
import org.json.JSONObject;

View File

@@ -24,7 +24,7 @@ public class SDLinkConfig extends ModuleConfig {
// DO NOT REMOVE TRANSIENT HERE... OTHERWISE, THE STUPID CONFIG LIBRARY
// WILL TRY TO WRITE THESE TO THE CONFIG
public transient static SDLinkConfig INSTANCE;
public transient static int configVer = 3;
public transient static int configVer = 5;
@Path("general")
@SpecComment("General Mod Config")
@@ -67,7 +67,7 @@ public class SDLinkConfig extends ModuleConfig {
public MessageIgnoreConfig ignoreConfig = new MessageIgnoreConfig();
public SDLinkConfig() {
super("sdlink", "simple-discord-link");
super("sdlink", "simple-discord-link", "simple-discord-link");
registerAndSetup(this);
}

View File

@@ -16,27 +16,31 @@ public class MessageChannelConfig {
@Path("chat")
@SpecComment("Control where CHAT messages are delivered")
public DestinationObject chat = DestinationObject.of(MessageDestination.CHAT, false);
public DestinationObject chat = DestinationObject.of(MessageDestination.CHAT, false, "default");
@Path("startStop")
@SpecComment("Control where START/STOP messages are delivered")
public DestinationObject startStop = DestinationObject.of(MessageDestination.EVENT, false);
public DestinationObject startStop = DestinationObject.of(MessageDestination.EVENT, false, "default");
@Path("joinLeave")
@SpecComment("Control where JOIN/LEAVE messages are delivered")
public DestinationObject joinLeave = DestinationObject.of(MessageDestination.EVENT, false);
public DestinationObject joinLeave = DestinationObject.of(MessageDestination.EVENT, false, "default");
@Path("advancements")
@SpecComment("Control where ADVANCEMENT messages are delivered")
public DestinationObject advancements = DestinationObject.of(MessageDestination.EVENT, false);
public DestinationObject advancements = DestinationObject.of(MessageDestination.EVENT, false, "default");
@Path("death")
@SpecComment("Control where DEATH messages are delivered")
public DestinationObject death = DestinationObject.of(MessageDestination.EVENT, false);
public DestinationObject death = DestinationObject.of(MessageDestination.EVENT, false, "default");
@Path("commands")
@SpecComment("Control where COMMAND messages are delivered")
public DestinationObject commands = DestinationObject.of(MessageDestination.EVENT, false);
public DestinationObject commands = DestinationObject.of(MessageDestination.EVENT, false, "default");
@Path("custom")
@SpecComment("Control where messages that match none of the above are delivered")
public DestinationObject custom = DestinationObject.of(MessageDestination.EVENT, false, "default");
public static class DestinationObject {
@Path("channel")
@@ -47,13 +51,18 @@ public class MessageChannelConfig {
@SpecComment("Should the message be sent using EMBED style messages")
public boolean useEmbed;
DestinationObject(MessageDestination destination, boolean useEmbed) {
@Path("embedLayout")
@SpecComment("Embed Layout to use")
public String embedLayout;
DestinationObject(MessageDestination destination, boolean useEmbed, String embedLayout) {
this.channel = destination;
this.useEmbed = useEmbed;
this.embedLayout = embedLayout;
}
public static DestinationObject of(MessageDestination destination, boolean useEmbed) {
return new DestinationObject(destination, useEmbed);
public static DestinationObject of(MessageDestination destination, boolean useEmbed, String embedLayout) {
return new DestinationObject(destination, useEmbed, embedLayout);
}
}
}

View File

@@ -20,8 +20,10 @@ import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@@ -62,8 +64,23 @@ public class BotController {
INSTANCE = this;
this.logger = logger;
File newConfigDir = new File("./config/simple-discord-link");
if (!newConfigDir.exists())
newConfigDir.mkdirs();
File oldConfig = new File("./config/simple-discord-link.toml");
if (oldConfig.exists()) {
try {
FileUtils.moveFile(oldConfig, new File(newConfigDir.getAbsolutePath() + File.separator + "simple-discord-link.toml"));
} catch (Exception e) {
logger.error("Failed to move config file to new location", e);
}
}
// Initialize Config
new SDLinkConfig();
// Initialize Account Storage
DatabaseManager.initialize();
// Initialize Webhook Clients

View File

@@ -15,5 +15,6 @@ public enum MessageType {
ADVANCEMENT,
DEATH,
COMMAND,
CONSOLE
CONSOLE,
CUSTOM
}

View File

@@ -68,7 +68,7 @@ public final class DiscordMessage {
* Send a Non Console relay message to discord
*/
private void sendNormalMessage() {
Triple<StandardGuildMessageChannel, WebhookClient, Boolean> channel = resolveDestination();
Triple<StandardGuildMessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> channel = resolveDestination();
// Check if a webhook is configured, and use that instead
if (channel.getMiddle() != null && SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.enabled) {
@@ -79,8 +79,8 @@ public final class DiscordMessage {
}
// Message must be an Embed
if (channel.getRight()) {
EmbedBuilder eb = buildEmbed(false);
if (channel.getRight().useEmbed) {
EmbedBuilder eb = buildEmbed(false, channel.getRight().embedLayout);
WebhookEmbed web = WebhookEmbedBuilder.fromJDA(eb.build()).build();
builder.addEmbeds(web);
} else {
@@ -93,8 +93,8 @@ public final class DiscordMessage {
});
} else {
// Use the configured channel instead
if (channel.getRight()) {
EmbedBuilder eb = buildEmbed(true);
if (channel.getRight().useEmbed) {
EmbedBuilder eb = buildEmbed(true, channel.getRight().embedLayout);
channel.getLeft().sendMessageEmbeds(eb.build()).queue(success -> {
if (afterSend != null)
afterSend.run();
@@ -138,7 +138,7 @@ public final class DiscordMessage {
* Build an embed with the supplied information
* @param withAuthor Should the author be appended to the embed. Not used for Webhooks
*/
private EmbedBuilder buildEmbed(boolean withAuthor) {
private EmbedBuilder buildEmbed(boolean withAuthor, String embedLayout) {
EmbedBuilder builder = new EmbedBuilder();
if (withAuthor) {
@@ -156,14 +156,14 @@ public final class DiscordMessage {
/**
* Figure out where the message must be delivered to, based on the config values
*/
private Triple<StandardGuildMessageChannel, WebhookClient, Boolean> resolveDestination() {
private Triple<StandardGuildMessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> resolveDestination() {
switch (messageType) {
case CHAT -> {
MessageChannelConfig.DestinationObject chat = SDLinkConfig.INSTANCE.messageDestinations.chat;
return Triple.of(
ChannelManager.getDestinationChannel(chat.channel),
WebhookManager.getWebhookClient(chat.channel),
chat.useEmbed
chat
);
}
case START_STOP -> {
@@ -171,7 +171,7 @@ public final class DiscordMessage {
return Triple.of(
ChannelManager.getDestinationChannel(startStop.channel),
WebhookManager.getWebhookClient(startStop.channel),
startStop.useEmbed
startStop
);
}
case JOIN_LEAVE -> {
@@ -179,7 +179,7 @@ public final class DiscordMessage {
return Triple.of(
ChannelManager.getDestinationChannel(joinLeave.channel),
WebhookManager.getWebhookClient(joinLeave.channel),
joinLeave.useEmbed
joinLeave
);
}
case ADVANCEMENT -> {
@@ -187,7 +187,7 @@ public final class DiscordMessage {
return Triple.of(
ChannelManager.getDestinationChannel(advancement.channel),
WebhookManager.getWebhookClient(advancement.channel),
advancement.useEmbed
advancement
);
}
case DEATH -> {
@@ -195,7 +195,7 @@ public final class DiscordMessage {
return Triple.of(
ChannelManager.getDestinationChannel(death.channel),
WebhookManager.getWebhookClient(death.channel),
death.useEmbed
death
);
}
case COMMAND -> {
@@ -203,13 +203,21 @@ public final class DiscordMessage {
return Triple.of(
ChannelManager.getDestinationChannel(command.channel),
WebhookManager.getWebhookClient(command.channel),
command.useEmbed
command
);
}
case CUSTOM -> {
MessageChannelConfig.DestinationObject custom = SDLinkConfig.INSTANCE.messageDestinations.custom;
return Triple.of(
ChannelManager.getDestinationChannel(custom.channel),
WebhookManager.getWebhookClient(custom.channel),
custom
);
}
}
// This code should never be reached, but it's added here as a fail-safe
MessageChannelConfig.DestinationObject chat = SDLinkConfig.INSTANCE.messageDestinations.chat;
return Triple.of(ChannelManager.getDestinationChannel(chat.channel), WebhookManager.getWebhookClient(chat.channel), chat.useEmbed);
return Triple.of(ChannelManager.getDestinationChannel(chat.channel), WebhookManager.getWebhookClient(chat.channel), chat);
}
}

View File

@@ -9,8 +9,6 @@ import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.config.impl.MessageIgnoreConfig;
import com.hypherionmc.sdlink.core.messaging.MessageType;
import java.util.Optional;
/**
* @author HypherionSA
* Used to construct a {@link DiscordMessage} to be sent back to discord

View File

@@ -4,10 +4,6 @@
*/
package com.hypherionmc.sdlink.core.util;
import com.hypherionmc.sdlink.core.managers.RoleManager;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Arrays;