Skip to content

Commit

Permalink
feat(command): (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seb committed Jan 23, 2022
1 parent e877862 commit 512c6d4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 61 deletions.
147 changes: 88 additions & 59 deletions src/main/java/de/voidtech/gerald/commands/utils/SuggestCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package main.java.de.voidtech.gerald.commands.utils;

import java.awt.Color;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

import main.java.de.voidtech.gerald.annotations.Command;
import main.java.de.voidtech.gerald.commands.AbstractCommand;
import main.java.de.voidtech.gerald.commands.CommandCategory;
Expand All @@ -11,13 +19,7 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.MessageEmbed;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.*;
import java.util.List;
import net.dv8tion.jda.api.entities.Role;

@Command
public class SuggestCommand extends AbstractCommand {
Expand All @@ -31,41 +33,45 @@ public class SuggestCommand extends AbstractCommand {
private final static String CHECK = "U+2705";
private final static String CROSS = "U+274C";

private boolean suggestionChannelExists(long serverID) {
private SuggestionChannel getSuggestionChannel(long serverID) {
try (Session session = sessionFactory.openSession()) {
SuggestionChannel suggestionChannel = (SuggestionChannel) session.createQuery("FROM SuggestionChannel WHERE ServerID = :serverID")
.setParameter("serverID", serverID)
.uniqueResult();
return suggestionChannel != null;
return suggestionChannel;
}
}

private String getChannelID(long serverID) {
try (Session session = sessionFactory.openSession()) {
SuggestionChannel suggestionChannel = (SuggestionChannel) session.createQuery("FROM SuggestionChannel WHERE ServerID = :serverID")
.setParameter("serverID", serverID)
.uniqueResult();
return suggestionChannel.getSuggestionChannel();
}
private void postSuggestion(CommandContext context, List<String> args, SuggestionChannel config) {
MessageEmbed newSuggestionEmbed = getMessageEmbed(context, args);
context.getGuild().getTextChannelById(config.getSuggestionChannel()).sendMessageEmbeds(newSuggestionEmbed).queue(sentMessage -> {
sentMessage.addReaction(CHECK).queue();
sentMessage.addReaction(CROSS).queue();
context.reply("**Your suggestion has been posted!**");
});
}

private void addSuggestion(CommandContext context, List<String> args) {
Server server = serverService.getServer(context.getGuild().getId());

if (suggestionChannelExists(server.getId())) {
String channelID = getChannelID(server.getId());

//TODO (from: Franziska): We cannot supply the original message in case of SlashCommand. Needs some thought.
MessageEmbed newSuggestionEmbed = getMessageEmbed(context, args);

context.getGuild().getTextChannelById(channelID).sendMessageEmbeds(newSuggestionEmbed).queue(sentMessage -> {
sentMessage.addReaction(CHECK).queue();
sentMessage.addReaction(CROSS).queue();
context.reply("**Your suggestion has been posted!**");
});
SuggestionChannel config = getSuggestionChannel(server.getId());

if (config != null) {
if (config.voteRoleRequired()) {
Role role = context.getMember().getRoles().stream()
.filter(searchRole -> searchRole.getId().equals(config.getVoteRoleID()))
.findFirst()
.orElse(null);
if (role == null) {
context.reply("**You need the** `" + context.getGuild().getRoleById(config.getVoteRoleID()).getName() + "` **role to make suggestions!**");
} else {
postSuggestion(context, args, config);
}
} else {
postSuggestion(context, args, config);
}

} else {
context.reply("**This command has not been set up yet!**\n\n"
+ this.getUsage());
context.reply("**This command has not been set up yet!**\n\n" + this.getUsage());
}

}
Expand All @@ -76,7 +82,6 @@ private MessageEmbed getMessageEmbed(CommandContext context, List<String> args)
.setColor(Color.ORANGE)
.setTitle("New Suggestion!")
.addField("Suggestion", String.join(" ", args), false)
//-------->.addField("Original Message", "**[Click Here](" + context.getJumpUrl() + ")**", false)<--------
.setFooter("Suggested By " + context.getAuthor().getAsTag(), context.getAuthor().getAvatarUrl())
.build();
return newSuggestionEmbed;
Expand All @@ -85,6 +90,10 @@ private MessageEmbed getMessageEmbed(CommandContext context, List<String> args)
private boolean isGuildChannel(String channelID, CommandContext context) {
return context.getGuild().getTextChannelById(channelID) != null;
}

private boolean isRole(String roleID, CommandContext context) {
return context.getGuild().getRoleById(roleID) != null;
}

private void addNewSuggestionChannel(long serverID, String channelID) {
try (Session session = sessionFactory.openSession()) {
Expand All @@ -95,6 +104,14 @@ private void addNewSuggestionChannel(long serverID, String channelID) {
session.getTransaction().commit();
}
}

private void saveRoleConfig(SuggestionChannel config) {
try (Session session = sessionFactory.openSession()) {
session.getTransaction().begin();
session.saveOrUpdate(config);
session.getTransaction().commit();
}
}

private void updateSuggestionChannel(long serverID, String channelID) {
try (Session session = sessionFactory.openSession()) {
Expand All @@ -114,35 +131,27 @@ private void updateSuggestionChannel(long serverID, String channelID) {
private void validateInput(String channelID, Server server, CommandContext context) {
if (ParsingUtils.isInteger(channelID)) {
if (isGuildChannel(channelID, context)) {
if (suggestionChannelExists(server.getId())) {
if (getSuggestionChannel(server.getId()) != null) {
updateSuggestionChannel(server.getId(), channelID);
context.reply("**The suggestion channel has been updated!!**");
} else {
addNewSuggestionChannel(server.getId(), channelID);
context.reply("**The suggestion box has been set up!**");
context.reply("**The suggestion channel has been set up!**");
}
} else {
context.reply("**That is not a valid text channel!**");
}
} else {
context.reply("**That is not a valid channel!**");
}
} else context.reply("**That is not a valid text channel!**");
} else context.reply("**That is not a valid channel!**");
}

private void setChannel(CommandContext context, List<String> args) {
if (context.getMember().hasPermission(Permission.MANAGE_CHANNEL)) {
if (args.size() < 2) {
context.reply("**You need to specify a channel! Use a channel mention or its ID**");
} else {
if (args.size() < 2) context.reply("**You need to specify a channel! Use a channel mention or its ID**");
else {
String channelID = ParsingUtils.filterSnowflake(args.get(1));
Server server = serverService.getServer(context.getGuild().getId());

validateInput(channelID, server, context);
}
} else {
context.reply("**You do not have permission to do that!**");
}

} else context.reply("**You do not have permission to do that!**");
}

private void deleteSuggestionChannel(long guildID) {
Expand All @@ -158,19 +167,33 @@ private void deleteSuggestionChannel(long guildID) {
private void disableSuggestions(CommandContext context) {
if (context.getMember().hasPermission(Permission.MANAGE_CHANNEL)) {
Server server = serverService.getServer(context.getGuild().getId());

if (suggestionChannelExists(server.getId())) {
if (getSuggestionChannel(server.getId()) != null) {
deleteSuggestionChannel(server.getId());
context.reply("**The suggestion system has been disabled**");
} else {
context.reply("**The suggestion system has not yet been set up!**");
}
} else {
context.reply("**You do not have permission to do that!**");
}
} else context.reply("**The suggestion system has not yet been set up!**");
} else context.reply("**You do not have permission to do that!**");
}

private void validateRoleInput(CommandContext context, String roleID, Server server, SuggestionChannel config) {
if (ParsingUtils.isInteger(roleID)) {
if (isRole(roleID, context)) {
config.setVoteRole(roleID);
saveRoleConfig(config);
context.reply("**Suggestion role set to** `" + context.getGuild().getRoleById(roleID).getName() + "`");
} else context.reply("**That is not a valid role!**");
} else context.reply("**That is not a valid role!**");
}

private void setOrRemoveRole(CommandContext context, List<String> args) {
if (context.getMember().hasPermission(Permission.MANAGE_CHANNEL)) {
Server server = serverService.getServer(context.getGuild().getId());
SuggestionChannel config = getSuggestionChannel(server.getId());
if (config != null) validateRoleInput(context, ParsingUtils.filterSnowflake(args.get(1)), server, config);
else context.reply("**The suggestion system has not yet been set up!**");
} else context.reply("**You do not have permission to do that!**");
}

@Override
@Override
public void executeInternal(CommandContext context, List<String> args) {
switch (args.get(0)) {
case "channel":
Expand All @@ -179,22 +202,28 @@ public void executeInternal(CommandContext context, List<String> args) {
case "disable":
disableSuggestions(context);
break;
case "role":
setOrRemoveRole(context, args);
break;
default:
addSuggestion(context, args);
break;
}
}

@Override
@Override
public String getDescription() {
return "This command allows you to set up a suggestions box. Simply set the suggestion box channel and your users can start sending suggestions!";
//+ "If a suggestion is approved or disapproved, the red and green circle emotes can be used by members with the Manage Channel permission to change the embed color.";
return "This command allows you to set up a suggestions box. "
+ "Simply set the suggestion box channel and your users can start sending suggestions! "
+ "If you only want certain users to make suggestions, use the role subcommand to set a suggestion role. "
+ "All users will require this role to make suggestions. This uncludes admins!";
}

@Override
public String getUsage() {
return "suggest channel [channel]\n"
+ "suggest disable\n"
+ "suggest role [role ID or role mention / clear]"
+ "suggest [suggestion]";
}

Expand Down
18 changes: 16 additions & 2 deletions src/main/java/de/voidtech/gerald/entities/SuggestionChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

@Entity
@Table(name = "suggestionchannel")

public class SuggestionChannel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@Column
private String channelID;
private String channelID;

@Column
private String voteRole;

@Column
private long serverID;
Expand All @@ -32,6 +34,18 @@ public SuggestionChannel(long serverID, String channelID)
this.serverID = serverID;
}

public boolean voteRoleRequired() {
return this.voteRole != null;
}

public String getVoteRoleID() {
return this.voteRole;
}

public void setVoteRole(String roleID) {
this.voteRole = roleID;
}

public String getSuggestionChannel() {
return channelID;
}
Expand Down

0 comments on commit 512c6d4

Please sign in to comment.