Added evidence command

This commit is contained in:
Teriuihi 2022-09-15 22:03:36 +02:00
parent 4961b70f28
commit a28e70b443
5 changed files with 151 additions and 11 deletions

View File

@ -1,10 +1,7 @@
package com.alttd.commandManager;
import com.alttd.commandManager.commands.AddCommand.CommandManage;
import com.alttd.commandManager.commands.CommandHelp;
import com.alttd.commandManager.commands.CommandSetOutputChannel;
import com.alttd.commandManager.commands.CommandSuggestion;
import com.alttd.commandManager.commands.CommandUpdateCommands;
import com.alttd.commandManager.commands.*;
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
import com.alttd.database.Database;
import com.alttd.modalManager.ModalManager;
@ -42,7 +39,8 @@ public class CommandManager extends ListenerAdapter {
new CommandPoll(jda, this),
new CommandSuggestion(jda, modalManager, this),
new CommandSetOutputChannel(jda, this),
new CommandUpdateCommands(jda, this));
new CommandUpdateCommands(jda, this),
new CommandEvidence(jda, modalManager, this));
}
@Override

View File

@ -1,11 +1,32 @@
package com.alttd.commandManager.commands;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.config.MessagesConfig;
import com.alttd.modalManager.ModalManager;
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;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.components.Modal;
import net.dv8tion.jda.api.requests.RestAction;
import java.util.Collections;
public class CommandEvidence extends DiscordCommand {
private final CommandData commandData;
private final ModalManager modalManager;
public CommandEvidence(JDA jda, ModalManager modalManager, CommandManager commandManager) {
this.modalManager = modalManager;
commandData = Commands.slash(getName(), "Open suggestion form.");
Util.registerCommand(commandManager, jda, commandData, getName());
}
@Override
public String getName() {
return "evidence";
@ -13,21 +34,29 @@ public class CommandEvidence extends DiscordCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
Modal modal = modalManager.getModalFor("evidence");
if (modal == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error",
"Unable to find evidence modal."))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
event.replyModal(modal).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
}
@Override
public void suggest(CommandAutoCompleteInteractionEvent event) {
event.replyChoices(Collections.emptyList())
.queue(RestAction.getDefaultSuccess(), Util::handleFailure);
}
@Override
public String getHelpMessage() {
return null;
return MessagesConfig.HELP_SUGGESTION;
}
@Override
public CommandData getCommandData() {
return null;
return commandData;
}
}

View File

@ -3,5 +3,6 @@ package com.alttd.database.queries.commandOutputChannels;
public enum OutputType {
SUGGESTION,
SUGGESTION_REVIEW,
MOD_LOG
MOD_LOG,
EVIDENCE
}

View File

@ -1,6 +1,7 @@
package com.alttd.modalManager;
import com.alttd.buttonManager.ButtonManager;
import com.alttd.modalManager.modals.ModalEvidence;
import com.alttd.modalManager.modals.ModalSuggestion;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
@ -21,7 +22,8 @@ public class ModalManager extends ListenerAdapter {
public ModalManager(ButtonManager buttonManager) {
modals = List.of(
new ModalSuggestion(buttonManager));
new ModalSuggestion(buttonManager),
new ModalEvidence());
}
@Override

View File

@ -0,0 +1,110 @@
package com.alttd.modalManager.modals;
import com.alttd.database.queries.commandOutputChannels.CommandOutputChannels;
import com.alttd.database.queries.commandOutputChannels.OutputType;
import com.alttd.modalManager.DiscordModal;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Modal;
import net.dv8tion.jda.api.interactions.components.text.TextInput;
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle;
import net.dv8tion.jda.api.interactions.modals.ModalMapping;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
import java.util.List;
public class ModalEvidence extends DiscordModal {
@Override
public String getModalId() {
return "evidence";
}
@Override
public void execute(ModalInteractionEvent event) {
List<ModalMapping> modalMappings = event.getValues();
if (modalMappings.size() != 4) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Found the wrong number of fields in your form input"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
String user = modalMappings.get(0).getAsString();
String punishmentType = modalMappings.get(1).getAsString();
String reason = modalMappings.get(2).getAsString();
String evidence = modalMappings.get(3).getAsString();
Guild guild = event.getGuild();
if (guild == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "Couldn't find this guild"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
GuildMessageChannel channel = guild.getChannelById(GuildMessageChannel.class, CommandOutputChannels.getOutputChannel(guild.getIdLong(), OutputType.EVIDENCE));
if (channel == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "This guild does not have a suggestion review channel or it's not the right channel type"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
Member member = event.getMember();
if (member == null) {
event.replyEmbeds(Util.genericErrorEmbed("Error", "This command can only be executed from a guild"))
.setEphemeral(true).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
return;
}
MessageEmbed evidenceEmbed = new EmbedBuilder()
.setAuthor(member.getEffectiveName(), null, member.getAvatarUrl())
.setTitle("Evidence by " + member.getEffectiveName())
.addField("`" + user + "`", "", false)
.addField(punishmentType, reason, false)
.setDescription(evidence)
.setFooter(member.getIdLong() + "")
.build();
ReplyCallbackAction replyCallbackAction = event.deferReply(true);
channel.sendMessageEmbeds(evidenceEmbed)
.queue(success -> replyCallbackAction.setEmbeds(Util.genericSuccessEmbed("Success", "Your evidence was submitted to the evidence channel!"), evidenceEmbed),
Util::handleFailure);
}
@Override
public Modal getModal() {
TextInput user = TextInput.create("user", "User", TextInputStyle.SHORT)
.setPlaceholder("username/id")
.setMinLength(1)
.setRequired(true)
.build();
TextInput punishmentType = TextInput.create("punishment-type", "Punishment Type", TextInputStyle.SHORT)
.setPlaceholder("punishment type")
.setMinLength(3)
.setRequired(true)
.build();
TextInput reason = TextInput.create("reason", "Reason", TextInputStyle.SHORT)
.setPlaceholder("punishment reason")
.setMinLength(10)
.setRequired(true)
.build();
TextInput evidence = TextInput.create("evidence", "Evidence", TextInputStyle.PARAGRAPH)
.setPlaceholder("evidence")
.setRequiredRange(10, 1000)
.setRequired(true)
.build();
return Modal.create(getModalId(), "Evidence")
.addActionRows(ActionRow.of(user), ActionRow.of(punishmentType), ActionRow.of(reason), ActionRow.of(evidence))
.build();
}
}