2022-03-31 20:08:50 +00:00
|
|
|
package com.alttd.commandManager.commands.PollCommand;
|
|
|
|
|
|
2023-06-04 21:30:09 +00:00
|
|
|
import com.alttd.buttonManager.ButtonManager;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.commandManager.DiscordCommand;
|
|
|
|
|
import com.alttd.commandManager.SubCommand;
|
2022-04-15 19:44:26 +00:00
|
|
|
import com.alttd.commandManager.SubCommandGroup;
|
2023-06-04 21:30:09 +00:00
|
|
|
import com.alttd.database.queries.Poll.Poll;
|
|
|
|
|
import com.alttd.database.queries.Poll.PollQueries;
|
2022-09-10 03:20:24 +00:00
|
|
|
import com.alttd.util.Util;
|
2023-06-04 21:30:09 +00:00
|
|
|
import net.dv8tion.jda.api.entities.Message;
|
|
|
|
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
2022-04-15 19:44:26 +00:00
|
|
|
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
2022-03-31 20:08:50 +00:00
|
|
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
2023-06-04 21:30:09 +00:00
|
|
|
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
2022-03-31 20:08:50 +00:00
|
|
|
|
|
|
|
|
public class SubCommandClose extends SubCommand {
|
2023-06-04 21:30:09 +00:00
|
|
|
|
|
|
|
|
private final ButtonManager buttonManager;
|
|
|
|
|
protected SubCommandClose(SubCommandGroup parentGroup, DiscordCommand parent, ButtonManager buttonManager) {
|
2022-04-15 19:44:26 +00:00
|
|
|
super(parentGroup, parent);
|
2023-06-04 21:30:09 +00:00
|
|
|
this.buttonManager = buttonManager;
|
2022-03-31 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getName() {
|
|
|
|
|
return "close";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void execute(SlashCommandInteractionEvent event) {
|
2023-06-04 21:30:09 +00:00
|
|
|
PollChannel pollChannel = PollUtil.getPollHandleErrors(event, getName());
|
|
|
|
|
if (pollChannel == null)
|
2022-04-01 00:54:33 +00:00
|
|
|
return;
|
2023-06-04 21:30:09 +00:00
|
|
|
Poll poll = pollChannel.poll();
|
|
|
|
|
ReplyCallbackAction replyCallbackAction = event.deferReply(true);
|
|
|
|
|
pollChannel.textChannel().retrieveMessageById(poll.getPollId()).queue(message -> closePoll(message, poll, replyCallbackAction));
|
|
|
|
|
}
|
2022-09-10 03:20:24 +00:00
|
|
|
|
2023-06-04 21:30:09 +00:00
|
|
|
private void closePoll(Message message, Poll poll, ReplyCallbackAction replyCallbackAction) {
|
|
|
|
|
List<MessageEmbed> embeds = message.getEmbeds();
|
|
|
|
|
if (embeds.size() > 1) {
|
|
|
|
|
replyCallbackAction.setEmbeds(Util.genericErrorEmbed("Error", "This poll has already been closed!")).queue();
|
2022-04-01 00:54:33 +00:00
|
|
|
return;
|
2022-09-10 03:20:24 +00:00
|
|
|
}
|
2023-06-04 21:30:09 +00:00
|
|
|
message.editMessageEmbeds(embeds.get(0), poll.getVotesEmbed()).queue();
|
|
|
|
|
PollQueries.setPollStatus(poll.getPollId(), false, buttonManager);
|
|
|
|
|
replyCallbackAction.setEmbeds(Util.genericSuccessEmbed("Success", "This poll has been closed!")).queue();
|
2022-03-31 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-15 19:44:26 +00:00
|
|
|
@Override
|
|
|
|
|
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
2023-06-04 21:30:09 +00:00
|
|
|
PollUtil.handleSuggestMessageId(event);
|
2022-04-15 19:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 20:08:50 +00:00
|
|
|
@Override
|
|
|
|
|
public String getHelpMessage() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|