Added command to update total votes manually
Moved code to update total votes to util class Removed the channel sub command option from all poll subcommands other than the add subcommand
This commit is contained in:
parent
977ae78e39
commit
bb60924a63
|
|
@ -8,6 +8,7 @@ import com.alttd.contextMenuManager.ContextMenuManager;
|
||||||
import com.alttd.database.Database;
|
import com.alttd.database.Database;
|
||||||
import com.alttd.listeners.LockedChannel;
|
import com.alttd.listeners.LockedChannel;
|
||||||
import com.alttd.modalManager.ModalManager;
|
import com.alttd.modalManager.ModalManager;
|
||||||
|
import com.alttd.schedulers.PollTimerTask;
|
||||||
import com.alttd.selectMenuManager.SelectMenuManager;
|
import com.alttd.selectMenuManager.SelectMenuManager;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import com.alttd.buttonManager.ButtonManager;
|
||||||
import com.alttd.commandManager.CommandManager;
|
import com.alttd.commandManager.CommandManager;
|
||||||
import com.alttd.commandManager.DiscordCommand;
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
import com.alttd.commandManager.SubOption;
|
import com.alttd.commandManager.SubOption;
|
||||||
|
import com.alttd.schedulers.PollTimerTask;
|
||||||
import com.alttd.util.Logger;
|
import com.alttd.util.Logger;
|
||||||
import com.alttd.util.Util;
|
import com.alttd.util.Util;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
|
|
@ -30,31 +31,26 @@ public class CommandPoll extends DiscordCommand {
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll should go into", true)
|
.addOption(OptionType.CHANNEL, "channel", "Channel this poll should go into", true)
|
||||||
.addOption(OptionType.STRING, "title", "Title of the embed (max 256 characters)", true),
|
.addOption(OptionType.STRING, "title", "Title of the embed (max 256 characters)", true),
|
||||||
new SubcommandData("edit_title", "Edit the title of a poll")
|
new SubcommandData("edit_title", "Edit the title of a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're editing", true)
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're editing", true)
|
||||||
.addOption(OptionType.STRING, "title", "The new title for the poll (max 256 characters)", true),
|
.addOption(OptionType.STRING, "title", "The new title for the poll (max 256 characters)", true),
|
||||||
new SubcommandData("edit_description", "Edit the description of a poll")
|
new SubcommandData("edit_description", "Edit the description of a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're editing", true)
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're editing", true)
|
||||||
.addOption(OptionType.STRING, "description", "The new description for the poll (max 2048 characters)", true),
|
.addOption(OptionType.STRING, "description", "The new description for the poll (max 2048 characters)", true),
|
||||||
new SubcommandData("add_button", "Add a button to a poll")
|
new SubcommandData("add_button", "Add a button to a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're adding a button to", true)
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're adding a button to", true)
|
||||||
.addOption(OptionType.INTEGER, "button_row", "Row the button should go in (1-5)", true)
|
.addOption(OptionType.INTEGER, "button_row", "Row the button should go in (1-5)", true)
|
||||||
.addOption(OptionType.STRING, "button_name", "Name of the button you're adding"),
|
.addOption(OptionType.STRING, "button_name", "Name of the button you're adding"),
|
||||||
new SubcommandData("remove_button", "Remove a button from a poll")
|
new SubcommandData("remove_button", "Remove a button from a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're removing a button from", true)
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're removing a button from", true)
|
||||||
.addOption(OptionType.STRING, "button_name", "Name of the button you're removing"),
|
.addOption(OptionType.STRING, "button_name", "Name of the button you're removing"),
|
||||||
new SubcommandData("open", "Open a poll")
|
new SubcommandData("open", "Open a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're opening", true),
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're opening", true),
|
||||||
new SubcommandData("close", "Close a poll")
|
new SubcommandData("close", "Close a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you're closing", true),
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you're closing", true),
|
||||||
new SubcommandData("results", "Get the results for a poll")
|
new SubcommandData("results", "Get the results for a poll")
|
||||||
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true)
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you want the results for", true),
|
||||||
.addOption(OptionType.STRING, "message_id", "Id of the poll you want the results for", true))
|
new SubcommandData("update_total_votes", "Update the total vote count incase it's out of sync")
|
||||||
|
.addOption(OptionType.STRING, "message_id", "Id of the poll you want to update the total vote count for", true))
|
||||||
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
|
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR))
|
||||||
.setGuildOnly(true);
|
.setGuildOnly(true);
|
||||||
Util.registerSubOptions(subOptionsMap,
|
Util.registerSubOptions(subOptionsMap,
|
||||||
|
|
@ -65,7 +61,8 @@ public class CommandPoll extends DiscordCommand {
|
||||||
new SubCommandEditTitle(null, this),
|
new SubCommandEditTitle(null, this),
|
||||||
new SubCommandOpen(null, this, buttonManager),
|
new SubCommandOpen(null, this, buttonManager),
|
||||||
new SubCommandRemoveButton(null, this),
|
new SubCommandRemoveButton(null, this),
|
||||||
new SubCommandResults(null,this));
|
new SubCommandResults(null,this),
|
||||||
|
new SubCommandUpdateTotalVotes(null, this, jda));
|
||||||
Util.registerCommand(commandManager, jda, commandData, getName());
|
Util.registerCommand(commandManager, jda, commandData, getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,15 @@
|
||||||
package com.alttd.commandManager.commands.PollCommand;
|
package com.alttd.commandManager.commands.PollCommand;
|
||||||
|
|
||||||
|
import com.alttd.AltitudeLogs;
|
||||||
import com.alttd.database.queries.Poll.Poll;
|
import com.alttd.database.queries.Poll.Poll;
|
||||||
import com.alttd.util.OptionMappingParsing;
|
import com.alttd.util.OptionMappingParsing;
|
||||||
import com.alttd.util.Util;
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||||
|
|
@ -77,5 +82,37 @@ public class PollUtil {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void updatePoll(JDA jda, AltitudeLogs logger, Poll poll) {
|
||||||
|
Guild guild = jda.getGuildById(poll.getGuildId());
|
||||||
|
if (guild == null) {
|
||||||
|
logger.warning("Unable to retrieve guild for poll: " + poll.getPollId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TextChannel textChannel = guild.getTextChannelById(poll.getChannelId());
|
||||||
|
if (textChannel == null) {
|
||||||
|
logger.warning("Unable to retrieve text channel for poll: " + poll.getPollId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
textChannel.retrieveMessageById(poll.getPollId()).queue(message -> updatePoll(logger, message, poll));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updatePoll(AltitudeLogs logger, Message message, Poll poll) {
|
||||||
|
List<MessageEmbed> embeds = new ArrayList<>(message.getEmbeds());
|
||||||
|
if (embeds.isEmpty()) {
|
||||||
|
logger.warning("Unable to retrieve embeds for poll " + poll.getPollId());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageEmbed messageEmbed = embeds.get(0);
|
||||||
|
EmbedBuilder embedBuilder = new EmbedBuilder(messageEmbed);
|
||||||
|
|
||||||
|
embedBuilder.setFooter("Counted " + poll.getTotalVotes() + " total votes!");
|
||||||
|
embeds.set(0, embedBuilder.build());
|
||||||
|
|
||||||
|
message.editMessageEmbeds(embeds).queue();
|
||||||
|
poll.setUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.alttd.commandManager.commands.PollCommand;
|
||||||
|
|
||||||
|
import com.alttd.commandManager.DiscordCommand;
|
||||||
|
import com.alttd.commandManager.SubCommand;
|
||||||
|
import com.alttd.commandManager.SubCommandGroup;
|
||||||
|
import com.alttd.util.Logger;
|
||||||
|
import com.alttd.util.Util;
|
||||||
|
import net.dv8tion.jda.api.JDA;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||||
|
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||||
|
|
||||||
|
public class SubCommandUpdateTotalVotes extends SubCommand {
|
||||||
|
private final JDA jda;
|
||||||
|
protected SubCommandUpdateTotalVotes(SubCommandGroup parentGroup, DiscordCommand parent, JDA jda) {
|
||||||
|
super(parentGroup, parent);
|
||||||
|
this.jda = jda;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "update_total_votes";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(SlashCommandInteractionEvent event) {
|
||||||
|
PollChannel pollChannel = PollUtil.getPollHandleErrors(event, getName());
|
||||||
|
if (pollChannel == null)
|
||||||
|
return;
|
||||||
|
PollUtil.updatePoll(jda, Logger.altitudeLogs, pollChannel.poll());
|
||||||
|
event.replyEmbeds(Util.genericSuccessEmbed("Success", "Updated poll [" + pollChannel.poll().getTitle() + "]")).setEphemeral(true).queue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||||
|
PollUtil.handleSuggestMessageId(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getHelpMessage() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -132,6 +132,6 @@ public class Poll {
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getTotalVotes() {
|
public long getTotalVotes() {
|
||||||
return buttons.stream().map(PollButton::getVotes).count();
|
return buttons.stream().map(PollButton::getVotes).mapToInt(Integer::intValue).sum();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,11 @@
|
||||||
package com.alttd.schedulers;
|
package com.alttd.schedulers;
|
||||||
|
|
||||||
import com.alttd.AltitudeLogs;
|
import com.alttd.AltitudeLogs;
|
||||||
|
import com.alttd.commandManager.commands.PollCommand.PollUtil;
|
||||||
import com.alttd.database.queries.Poll.Poll;
|
import com.alttd.database.queries.Poll.Poll;
|
||||||
import net.dv8tion.jda.api.EmbedBuilder;
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
|
||||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
public class PollTimerTask extends TimerTask {
|
public class PollTimerTask extends TimerTask {
|
||||||
|
|
@ -29,41 +23,8 @@ public class PollTimerTask extends TimerTask {
|
||||||
Collection<Poll> polls = Poll.getAllPolls();
|
Collection<Poll> polls = Poll.getAllPolls();
|
||||||
for (Poll poll : polls) {
|
for (Poll poll : polls) {
|
||||||
if (poll.needsUpdate()) {
|
if (poll.needsUpdate()) {
|
||||||
updatePoll(poll);
|
PollUtil.updatePoll(jda, logger, poll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updatePoll(Poll poll) {
|
|
||||||
Guild guild = jda.getGuildById(poll.getGuildId());
|
|
||||||
if (guild == null) {
|
|
||||||
logger.warning("Unable to retrieve guild for poll: " + poll.getPollId());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextChannel textChannel = guild.getTextChannelById(poll.getChannelId());
|
|
||||||
if (textChannel == null) {
|
|
||||||
logger.warning("Unable to retrieve text channel for poll: " + poll.getPollId());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
textChannel.retrieveMessageById(poll.getPollId()).queue(message -> updatePoll(message, poll));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePoll(Message message, Poll poll) {
|
|
||||||
List<MessageEmbed> embeds = new ArrayList<>(message.getEmbeds());
|
|
||||||
if (embeds.isEmpty()) {
|
|
||||||
logger.warning("Unable to retrieve embeds for poll " + poll.getPollId());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MessageEmbed messageEmbed = embeds.get(0);
|
|
||||||
EmbedBuilder embedBuilder = new EmbedBuilder(messageEmbed);
|
|
||||||
|
|
||||||
embedBuilder.setFooter("Counted " + poll.getTotalVotes() + " total votes!");
|
|
||||||
embeds.set(0, embedBuilder.build());
|
|
||||||
|
|
||||||
message.editMessageEmbeds(embeds).queue();
|
|
||||||
poll.setUpdated();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user