[FEAT] Allow usage of thread and forum channels for messaging. Not supported in Webhooks yet

This commit is contained in:
2024-03-17 13:39:58 +02:00
parent b7d5cb9325
commit 4e2f037c2f
4 changed files with 18 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ public class SetChannelCommand extends SDLinkSlashCommand {
choices.add(new Command.Choice("Console", "console"));
List<OptionData> optionData = new ArrayList<>();
optionData.add(new OptionData(OptionType.CHANNEL, "channel", "The channel to set").setChannelTypes(ChannelType.TEXT).setRequired(true));
optionData.add(new OptionData(OptionType.CHANNEL, "channel", "The channel to set").setChannelTypes(ChannelType.TEXT, ChannelType.FORUM, ChannelType.GUILD_PUBLIC_THREAD, ChannelType.GUILD_PRIVATE_THREAD).setRequired(true));
optionData.add(new OptionData(OptionType.STRING, "type", "The type of channel to assign this channel to").addChoices(choices).setRequired(true));
optionData.add(new OptionData(OptionType.BOOLEAN, "webhook", "Create a webhook instead of using the channel").setRequired(true));

View File

@@ -12,6 +12,7 @@ import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
import com.hypherionmc.sdlink.core.util.SystemUtils;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel;
import net.dv8tion.jda.api.events.session.ReadyEvent;
@@ -65,13 +66,13 @@ public class BotReadyHooks {
BotController.taskManager.scheduleAtFixedRate(() -> {
try {
if (BotController.INSTANCE.isBotReady() && (SDLinkConfig.INSTANCE.botConfig.channelTopic.channelTopic != null && !SDLinkConfig.INSTANCE.botConfig.channelTopic.channelTopic.isEmpty())) {
StandardGuildMessageChannel channel = ChannelManager.getDestinationChannel(MessageDestination.CHAT);
if (channel != null) {
MessageChannel channel = ChannelManager.getDestinationChannel(MessageDestination.CHAT);
if (channel instanceof StandardGuildMessageChannel mc) {
String topic = SDLinkConfig.INSTANCE.botConfig.channelTopic.channelTopic
.replace("%players%", String.valueOf(SDLinkPlatform.minecraftHelper.getPlayerCounts().getLeft()))
.replace("%maxplayers%", String.valueOf(SDLinkPlatform.minecraftHelper.getPlayerCounts().getRight()))
.replace("%uptime%", SystemUtils.secondsToTimestamp(SDLinkPlatform.minecraftHelper.getServerUptime()));
channel.getManager().setTopic(topic).queue();
mc.getManager().setTopic(topic).queue();
}
}
} catch (Exception e) {

View File

@@ -9,6 +9,8 @@ import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.messaging.MessageDestination;
import lombok.Getter;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel;
import org.apache.commons.lang3.tuple.Pair;
@@ -20,9 +22,9 @@ import java.util.HashMap;
*/
public class ChannelManager {
private static final HashMap<MessageDestination, Pair<StandardGuildMessageChannel, Boolean>> channelMap = new HashMap<>();
private static final HashMap<MessageDestination, Pair<MessageChannel, Boolean>> channelMap = new HashMap<>();
@Getter
private static StandardGuildMessageChannel consoleChannel;
private static MessageChannel consoleChannel;
/**
* Load configured channel, while always defaulting back to ChatChannel for channels that aren't configured
@@ -32,9 +34,9 @@ public class ChannelManager {
JDA jda = BotController.INSTANCE.getJDA();
StandardGuildMessageChannel chatChannel = jda.getChannelById(StandardGuildMessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.chatChannelID);
StandardGuildMessageChannel eventChannel = jda.getChannelById(StandardGuildMessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.eventsChannelID);
consoleChannel = jda.getChannelById(StandardGuildMessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.consoleChannelID);
MessageChannel chatChannel = jda.getChannelById(MessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.chatChannelID);
MessageChannel eventChannel = jda.getChannelById(MessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.eventsChannelID);
consoleChannel = jda.getChannelById(MessageChannel.class, SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.consoleChannelID);
if (chatChannel != null) {
channelMap.put(MessageDestination.CHAT, Pair.of(chatChannel, false));
@@ -44,7 +46,7 @@ public class ChannelManager {
channelMap.put(MessageDestination.CONSOLE, consoleChannel != null ? Pair.of(consoleChannel, true) : Pair.of(chatChannel, false));
}
public static StandardGuildMessageChannel getDestinationChannel(MessageDestination destination) {
public static MessageChannel getDestinationChannel(MessageDestination destination) {
return channelMap.get(destination).getLeft();
}
}

View File

@@ -19,6 +19,8 @@ import com.hypherionmc.sdlink.core.messaging.MessageType;
import com.hypherionmc.sdlink.core.util.SDLinkUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.DataObject;
@@ -80,7 +82,7 @@ public final class DiscordMessage {
* Send a Non Console relay message to discord
*/
private void sendNormalMessage() {
Triple<StandardGuildMessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> channel = resolveDestination();
Triple<MessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> channel = resolveDestination();
// Check if a webhook is configured, and use that instead
if (channel.getMiddle() != null && SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.enabled) {
@@ -132,7 +134,7 @@ public final class DiscordMessage {
if (!BotController.INSTANCE.isBotReady() || !SDLinkConfig.INSTANCE.chatConfig.sendConsoleMessages)
return;
StandardGuildMessageChannel channel = ChannelManager.getConsoleChannel();
MessageChannel channel = ChannelManager.getConsoleChannel();
if (channel != null) {
channel.sendMessage(this.message).queue();
}
@@ -184,7 +186,7 @@ public final class DiscordMessage {
/**
* Figure out where the message must be delivered to, based on the config values
*/
private Triple<StandardGuildMessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> resolveDestination() {
private Triple<MessageChannel, WebhookClient, MessageChannelConfig.DestinationObject> resolveDestination() {
switch (messageType) {
case CHAT -> {
MessageChannelConfig.DestinationObject chat = SDLinkConfig.INSTANCE.messageDestinations.chat;