|
| 1 | +package technobot.commands.greetings; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.Permission; |
| 4 | +import net.dv8tion.jda.api.entities.ChannelType; |
| 5 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 6 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 8 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; |
| 10 | +import technobot.TechnoBot; |
| 11 | +import technobot.commands.Category; |
| 12 | +import technobot.commands.Command; |
| 13 | +import technobot.data.GuildData; |
| 14 | +import technobot.data.cache.Greetings; |
| 15 | +import technobot.handlers.GreetingHandler; |
| 16 | +import technobot.util.embeds.EmbedUtils; |
| 17 | + |
| 18 | +/** |
| 19 | + * Command that displays and modifies greetings config. |
| 20 | + * |
| 21 | + * @author TechnoVision |
| 22 | + */ |
| 23 | +public class GreetingsCommand extends Command { |
| 24 | + |
| 25 | + public GreetingsCommand(TechnoBot bot) { |
| 26 | + super(bot); |
| 27 | + this.name = "greetings"; |
| 28 | + this.description = "Modify this server's greetings config."; |
| 29 | + this.category = Category.GREETINGS; |
| 30 | + this.permission = Permission.MANAGE_SERVER; |
| 31 | + this.subCommands.add(new SubcommandData("channel", "Sets a channel to send welcome messages to.") |
| 32 | + .addOptions(new OptionData(OptionType.CHANNEL, "channel", "The channel to send welcome messages to") |
| 33 | + .setChannelTypes(ChannelType.TEXT, ChannelType.NEWS))); |
| 34 | + this.subCommands.add(new SubcommandData("config", "Displays the greetings config for this server.")); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void execute(SlashCommandInteractionEvent event) { |
| 39 | + event.deferReply().queue(); |
| 40 | + GreetingHandler greetingHandler = GuildData.get(event.getGuild()).greetingHandler; |
| 41 | + |
| 42 | + String text = ""; |
| 43 | + switch(event.getSubcommandName()) { |
| 44 | + case "channel" -> { |
| 45 | + OptionMapping channelOption = event.getOption("channel"); |
| 46 | + if (channelOption == null) { |
| 47 | + // Remove welcome channel if not specified |
| 48 | + greetingHandler.removeChannel(); |
| 49 | + text = EmbedUtils.BLUE_X + " Welcome channel successfully removed!"; |
| 50 | + } else { |
| 51 | + // Set welcome channel |
| 52 | + Long channelID = channelOption.getAsGuildChannel().getIdLong(); |
| 53 | + greetingHandler.setChannel(channelID); |
| 54 | + text = EmbedUtils.BLUE_X + " Welcome channel set to <#" + channelID + ">"; |
| 55 | + } |
| 56 | + } |
| 57 | + case "config" -> { |
| 58 | + text = configToString(greetingHandler.getConfig()); |
| 59 | + event.getHook().sendMessage(text).queue(); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | + event.getHook().sendMessageEmbeds(EmbedUtils.createDefault(text)).queue(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Converts the greetings config into a readable string |
| 68 | + * |
| 69 | + * @param greetings an instance of the guild greetings config. |
| 70 | + * @return Stringified config (greetings only). |
| 71 | + */ |
| 72 | + private String configToString(Greetings greetings) { |
| 73 | + String text = ""; |
| 74 | + if (greetings.getWelcomeChannel() == null) { |
| 75 | + text += "**Welcome Channel:** none\n"; |
| 76 | + } else { |
| 77 | + text += "**Welcome Channel:** <#" + greetings.getWelcomeChannel() + ">\n"; |
| 78 | + } |
| 79 | + if (greetings.getGreeting() == null) { |
| 80 | + text += "**Greeting:** none\n"; |
| 81 | + } else { |
| 82 | + text += "**Greeting:** '" + greetings.getGreeting() + "'\n"; |
| 83 | + } |
| 84 | + if (greetings.getFarewell() == null) { |
| 85 | + text += "**Farewell:** none\n"; |
| 86 | + } else { |
| 87 | + text += "**Farewell:** " + greetings.getFarewell() + "\n"; |
| 88 | + } |
| 89 | + if (greetings.getJoinDM() == null) { |
| 90 | + text += "**Join DM:** none\n"; |
| 91 | + } else { |
| 92 | + text += "**Join DM:** " + greetings.getJoinDM() + "\n"; |
| 93 | + } |
| 94 | + return text; |
| 95 | + } |
| 96 | +} |
0 commit comments