Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Suggestions. #129

Merged
merged 8 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/main/java/de/presti/ree6/commands/impl/mod/Suggestion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package de.presti.ree6.commands.impl.mod;

import de.presti.ree6.commands.Category;
import de.presti.ree6.commands.CommandEvent;
import de.presti.ree6.commands.interfaces.Command;
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.base.entities.SQLResponse;
import de.presti.ree6.sql.entities.Suggestions;
import de.presti.ree6.utils.data.Data;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;

import java.awt.*;

/**
* A command used to set up the Suggestion System.
*/
@Command(name = "suggestion", description = "Setup the Suggestion-System!", category = Category.MOD)
public class Suggestion implements ICommand {

/**
* @inheritDoc
*/
@Override
public void onPerform(CommandEvent commandEvent) {
if (!commandEvent.getGuild().getSelfMember().hasPermission(Permission.ADMINISTRATOR)) {
Main.getInstance().getCommandManager().sendMessage("It seems like I do not have the permissions to do that :/\nPlease re-invite me!", 5, commandEvent.getChannel(), commandEvent.getInteractionHook());
return;
}

if (commandEvent.isSlashCommand()) {
OptionMapping channel = commandEvent.getSlashCommandInteractionEvent().getOption("target");
OptionMapping messageChannel = commandEvent.getSlashCommandInteractionEvent().getOption("messagetarget");

if (channel == null || messageChannel == null) {
commandEvent.reply("Please provide a channel!");
return;
}

createSuggestions(commandEvent, channel.getAsChannel().asGuildMessageChannel(), messageChannel.getAsChannel().asGuildMessageChannel());
} else {
if (commandEvent.getMessage() != null && commandEvent.getMessage().getMentions().getChannels().size() == 2) {
createSuggestions(commandEvent, (MessageChannel) commandEvent.getMessage().getMentions().getChannels().get(0), (MessageChannel) commandEvent.getMessage().getMentions().getChannels().get(1));
} else {
commandEvent.reply("No channels provided!");
commandEvent.reply("Use " + Main.getInstance().getSqlConnector().getSqlWorker().getSetting(commandEvent.getGuild().getId(), "chatprefix").getStringValue() + "suggestion #suggestions-channel #message-channel");
}
}
}

/**
* Create all the entries needed.
* @param channel the Suggestion channel.
* @param messageChannel the Channel for the Message.
*/
public void createSuggestions(CommandEvent commandEvent, MessageChannel channel, MessageChannel messageChannel) {
MessageCreateBuilder messageCreateBuilder = new MessageCreateBuilder();
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Suggestion-System");
embedBuilder.setColor(Color.ORANGE);
embedBuilder.setDescription("Click on the button below to suggest something!");
embedBuilder.setFooter(commandEvent.getGuild().getName() + " - " + Data.ADVERTISEMENT, commandEvent.getGuild().getIconUrl());
messageCreateBuilder.setEmbeds(embedBuilder.build());
messageCreateBuilder.setActionRow(Button.primary("re_suggestion", "Suggest something!"));

Main.getInstance().getCommandManager().sendMessage(messageCreateBuilder.build(), messageChannel);

SQLResponse sqlResponse = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(Suggestions.class, "SELECT * FROM Suggestions WHERE guildId = ?", commandEvent.getGuild().getIdLong());

if (sqlResponse.isSuccess()) {
Suggestions suggestions = (Suggestions) sqlResponse.getEntity();
Suggestions newSuggestions = new Suggestions(suggestions.getGuildId(), channel.getIdLong());

Main.getInstance().getSqlConnector().getSqlWorker().updateEntity(suggestions, newSuggestions, true);
} else {
Suggestions suggestions = new Suggestions(commandEvent.getGuild().getIdLong(), channel.getIdLong());
Main.getInstance().getSqlConnector().getSqlWorker().saveEntity(suggestions);
}

commandEvent.reply("Successfully setup the Suggestion-System!", 5);
}

/**
* @inheritDoc
*/
@Override
public CommandData getCommandData() {
return new CommandDataImpl("suggestion", "Setup the Suggestion-System!")
.addOptions(new OptionData(OptionType.CHANNEL, "target", "The channel the suggestions should be shown in.").setRequired(true))
.addOptions(new OptionData(OptionType.CHANNEL, "messagetarget", "The channel the bot should send the suggestion message to.").setRequired(true))
.setDefaultPermissions(DefaultMemberPermissions.enabledFor(Permission.ADMINISTRATOR));
}

/**
* @inheritDoc
*/
@Override
public String[] getAlias() {
return new String[0];
}
}
40 changes: 40 additions & 0 deletions src/main/java/de/presti/ree6/events/OtherEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.presti.ree6.main.Main;
import de.presti.ree6.sql.base.entities.SQLResponse;
import de.presti.ree6.sql.base.utils.SQLUtil;
import de.presti.ree6.sql.entities.Suggestions;
import de.presti.ree6.sql.entities.TemporalVoicechannel;
import de.presti.ree6.sql.entities.level.ChatUserLevel;
import de.presti.ree6.sql.entities.level.VoiceUserLevel;
Expand Down Expand Up @@ -37,6 +38,7 @@
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.SelectMenuInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
Expand All @@ -54,6 +56,7 @@
import java.awt.*;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -367,6 +370,18 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
Main.getInstance().getCommandManager().perform(Objects.requireNonNull(event.getMember()), event.getGuild(), null, null, event.getChannel(), event);
}

@Override
public void onButtonInteraction(@NotNull ButtonInteractionEvent event) {
super.onButtonInteraction(event);

if (event.getComponentId().equals("re_suggestion")) {

Modal.Builder builder = Modal.create("re_suggestion_modal", "Suggestion");
builder.addActionRow(TextInput.create("re_suggestion_text", "Suggestion", TextInputStyle.PARAGRAPH).setRequired(true).setMaxLength(2042).setMinLength(16).build());
event.replyModal(builder.build()).queue();
}
}

/**
* @inheritDoc
*/
Expand All @@ -375,6 +390,31 @@ public void onModalInteraction(@NotNull ModalInteractionEvent event) {
super.onModalInteraction(event);

switch(event.getModalId()) {
case "re_suggestion_modal" -> {
SQLResponse sqlResponse = Main.getInstance().getSqlConnector().getSqlWorker().getEntity(Suggestions.class, "SELECT * FROM Suggestions WHERE guildId = ?", event.getGuild().getIdLong());

event.deferReply(true).queue();

if (sqlResponse.isSuccess()) {
Suggestions suggestions = (Suggestions) sqlResponse.getEntity();

MessageChannel messageChannel = (MessageChannel) event.getGuild().getGuildChannelById(suggestions.getChannelId());

if (messageChannel == null) return;

EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Suggestion");
embedBuilder.setColor(Color.ORANGE);
embedBuilder.setDescription("```" + event.getValue("re_suggestion_text").getAsString() + "```");
embedBuilder.setFooter("Suggestion by " + event.getUser().getAsTag(), event.getUser().getAvatarUrl());
embedBuilder.setTimestamp(Instant.now());
Main.getInstance().getCommandManager().sendMessage(embedBuilder, messageChannel);
Main.getInstance().getCommandManager().sendMessage("Suggestion sent!", null, event.getInteraction().getHook());
} else {
Main.getInstance().getCommandManager().sendMessage("Looks like the Suggestion-System is not set up right?", null,event.getInteraction().getHook());
}
}

case "statisticsSetupTwitchModal" -> {
ModalMapping modalMapping = event.getValue("twitchChannelName");

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/de/presti/ree6/sql/entities/Suggestions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package de.presti.ree6.sql.entities;

import de.presti.ree6.sql.base.annotations.Property;
import de.presti.ree6.sql.base.annotations.Table;
import de.presti.ree6.sql.base.entities.SQLEntity;

/**
* Class used to store information about the Suggestions.
*/
@Table(name = "Suggestions")
public class Suggestions extends SQLEntity {

/**
* The ID of the Guild.
*/
@Property(name = "guildId")
long guildId;

/**
* The ID of the Channel.
*/
@Property(name = "channelId", updateQuery = true)
long channelId;

/**
* Constructor.
*/
public Suggestions() {
}

/**
* Constructor for the Suggestions.
*
* @param guildId the ID of the Guild.
* @param channelId the ID of the Channel.
*/
public Suggestions(long guildId, long channelId) {
this.guildId = guildId;
this.channelId = channelId;
}

/**
* Get the ID of the Guild.
*
* @return the ID of the Guild.
*/
public long getGuildId() {
return guildId;
}

/**
* Get the ID of the Channel.
*
* @return the ID of the Channel.
*/
public long getChannelId() {
return channelId;
}
}