diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java index 35d6231..4780677 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java @@ -7,6 +7,7 @@ package com.hypherionmc.sdlink.core.discord.commands; import com.hypherionmc.sdlink.core.discord.commands.slash.general.HelpSlashCommand; import com.hypherionmc.sdlink.core.discord.commands.slash.general.PlayerListSlashCommand; import com.hypherionmc.sdlink.core.discord.commands.slash.general.ServerStatusSlashCommand; +import com.hypherionmc.sdlink.core.discord.commands.slash.setup.SetChannelCommand; import com.hypherionmc.sdlink.core.discord.commands.slash.verification.*; import com.jagrosh.jdautilities.command.CommandClient; import com.jagrosh.jdautilities.command.SlashCommand; @@ -29,6 +30,7 @@ public class CommandManager { } private void addCommands() { + // Access Control Commands commands.add(new VerifyAccountCommand()); commands.add(new UnverifyAccountSlashCommand()); commands.add(new StaffUnverifyCommand()); @@ -43,6 +45,9 @@ public class CommandManager { // Enable the Help command commands.add(new HelpSlashCommand()); + + // SetChannel config Command + commands.add(new SetChannelCommand()); } /** diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java new file mode 100644 index 0000000..6a50cd9 --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/setup/SetChannelCommand.java @@ -0,0 +1,114 @@ +package com.hypherionmc.sdlink.core.discord.commands.slash.setup; + +import com.hypherionmc.sdlink.core.config.SDLinkConfig; +import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.messaging.Result; +import com.hypherionmc.sdlink.core.util.EncryptionUtil; +import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildMessageChannel; +import net.dv8tion.jda.api.interactions.commands.Command; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.ArrayList; +import java.util.List; + +public class SetChannelCommand extends SDLinkSlashCommand { + + public SetChannelCommand() { + super(true); + this.name = "setchannel"; + this.help = "Configure a channel for the mod to use"; + this.guildOnly = true; + + List choices = new ArrayList<>(); + choices.add(new Command.Choice("Chat", "chat")); + choices.add(new Command.Choice("Events", "events")); + choices.add(new Command.Choice("Console", "console")); + + List optionData = new ArrayList<>(); + optionData.add(new OptionData(OptionType.CHANNEL, "channel", "The channel to set").setChannelTypes(ChannelType.TEXT).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)); + + this.options = optionData; + } + + @Override + protected void execute(SlashCommandEvent event) { + StandardGuildMessageChannel channel = event.getOption("channel").getAsChannel().asStandardGuildMessageChannel(); + String type = event.getOption("type").getAsString(); + boolean webhook = event.getOption("webhook").getAsBoolean(); + + if (!channel.canTalk()) { + event.reply("I do not have permission to send messages in " + channel.getAsMention()).setEphemeral(true).queue(); + return; + } + + Result result; + + if (webhook) { + result = setWebhook(channel, type); + } else { + result = setChannel(channel, type); + } + + event.reply(result.getMessage()).setEphemeral(true).queue(); + } + + private Result setChannel(StandardGuildMessageChannel channel, String type) { + try { + switch (type.toLowerCase()) { + case "chat": { + SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.chatChannelID = channel.getId(); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + } + case "event": { + SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.eventsChannelID = channel.getId(); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + } + case "console": { + SDLinkConfig.INSTANCE.channelsAndWebhooks.channels.consoleChannelID = channel.getId(); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + } + } + + return Result.success("Saved Channel to Config. Restart your server for the channel to become active"); + } catch (Exception e) { + e.printStackTrace(); + return Result.error("Failed to save config: " + e.getMessage()); + } + } + + private Result setWebhook(StandardGuildMessageChannel channel, String type) { + try { + switch (type.toLowerCase()) { + case "chat": { + channel.createWebhook("SDLink " + type).queue(s -> { + SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.chatWebhook = EncryptionUtil.INSTANCE.encrypt(s.getUrl()); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + }); + } + case "event": { + channel.createWebhook("SDLink " + type).queue(s -> { + SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.eventsWebhook = EncryptionUtil.INSTANCE.encrypt(s.getUrl()); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + }); + } + case "console": { + channel.createWebhook("SDLink " + type).queue(s -> { + SDLinkConfig.INSTANCE.channelsAndWebhooks.webhooks.consoleWebhook = EncryptionUtil.INSTANCE.encrypt(s.getUrl()); + SDLinkConfig.INSTANCE.saveConfig(SDLinkConfig.INSTANCE); + }); + } + } + + return Result.success("Saved Webhook to Config. Restart your server for the webhook to become active"); + } catch (Exception e) { + e.printStackTrace(); + return Result.error("Failed to save config: " + e.getMessage()); + } + } + +}