From b081a9ef880d45c38eaf2e259423b677c8d68fa0 Mon Sep 17 00:00:00 2001 From: Stijn Date: Fri, 8 Apr 2022 21:57:38 +0200 Subject: [PATCH] Started work for adding buttons --- .../PollCommand/SubCommandAddButton.java | 68 +++++++++++++++++-- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/alttd/commandManager/commands/PollCommand/SubCommandAddButton.java b/src/main/java/com/alttd/commandManager/commands/PollCommand/SubCommandAddButton.java index 712c193..57d686b 100644 --- a/src/main/java/com/alttd/commandManager/commands/PollCommand/SubCommandAddButton.java +++ b/src/main/java/com/alttd/commandManager/commands/PollCommand/SubCommandAddButton.java @@ -2,9 +2,18 @@ package com.alttd.commandManager.commands.PollCommand; import com.alttd.commandManager.DiscordCommand; import com.alttd.commandManager.SubCommand; +import com.alttd.templates.Parser; +import com.alttd.templates.Template; +import com.alttd.util.Logger; import com.alttd.util.OptionMappingParsing; +import com.alttd.util.Util; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.entities.GuildMessageChannel; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.InteractionHook; public class SubCommandAddButton extends SubCommand { protected SubCommandAddButton(DiscordCommand parent) { @@ -19,18 +28,67 @@ public class SubCommandAddButton extends SubCommand { @Override public void execute(SlashCommandInteractionEvent event) { GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName()); - if (channel == null) + Member member = event.getMember(); + if (member == null) { + event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find valid guild member.")) + .setEphemeral(true) + .queue(); return; + } + if (!Util.validateGuildMessageChannel(event.getInteraction(), channel, ChannelType.TEXT, member)) + return; + Long messageId = OptionMappingParsing.getLong("message_id", event, getName()); - if (messageId == null) + if (messageId == null) { + event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve message id.")) + .setEphemeral(true) + .queue(); return; + } + //TODO verify that message id is in database + Long rowLong = OptionMappingParsing.getLong("button_row", event, getName()); - if (rowLong == null) + if (rowLong == null) { + event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve button row.")) + .setEphemeral(true) + .queue(); return; - int row = rowLong.intValue(); + } + + int rowId = rowLong.intValue(); + if (rowId < 1 || rowId > 5) { + event.replyEmbeds(Util.genericErrorEmbed("Error", + Parser.parse("Invalid row id ``, only 1-5 are valid.", + Template.of("id", String.valueOf(rowId))))) + .setEphemeral(true) + .queue(); + return; + } String buttonName = OptionMappingParsing.getString("button_name", event, getName()); - if (buttonName == null) + if (buttonName == null) { + event.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to retrieve button name.")) + .setEphemeral(true) + .queue(); return; + } + event.deferReply().queue(hook -> + channel.retrieveMessageById(messageId).queue( + message -> updatePoll(channel, rowId, buttonName, message, hook), + throwable -> failedToGetMessage(throwable, hook))); + } + + private void failedToGetMessage(Throwable throwable, InteractionHook hook) { + Logger.warning(throwable.getMessage()); + hook.editOriginalEmbeds(Util.genericErrorEmbed("Failed to get poll message", + "Please check if the poll still exists and the message id is correct.")) + .queue(); + } + + private void updatePoll(GuildMessageChannel channel, int rowId, String buttonName, Message message, + InteractionHook hook) { + //TODO add button, generate id, add a way to remove button, store id in database + //Maybe temporarily disable the poll and listen for someone to click the button, then delete that button + // and switch back to normal poll mode? } @Override