Skip to content

Commit

Permalink
refac(command): Changed Starboard setup to use event waiters (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElementalMP4 committed Jul 20, 2021
1 parent f45223d commit 62d1831
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import java.awt.Color;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Autowired;

import com.jagrosh.jdautilities.commons.waiter.EventWaiter;

import main.java.de.voidtech.gerald.annotations.Command;
import main.java.de.voidtech.gerald.commands.AbstractCommand;
import main.java.de.voidtech.gerald.commands.CommandCategory;
import main.java.de.voidtech.gerald.entities.Server;
import main.java.de.voidtech.gerald.entities.StarboardConfig;
import main.java.de.voidtech.gerald.service.ServerService;
import main.java.de.voidtech.gerald.service.StarboardService;
import main.java.de.voidtech.gerald.util.MRESameUserPredicate;
import main.java.de.voidtech.gerald.util.ParsingUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

@Command
public class StarboardCommand extends AbstractCommand {
Expand All @@ -27,26 +33,44 @@ public class StarboardCommand extends AbstractCommand {
@Autowired
private StarboardService starboardService;

@Autowired
EventWaiter waiter;

private void getAwaitedReply(Message message, String question, Consumer<String> result) {
message.getChannel().sendMessage(question).queue();
waiter.waitForEvent(MessageReceivedEvent.class,
new MRESameUserPredicate(message.getAuthor()),
event -> {
result.accept(event.getMessage().getContentRaw());
}, 30, TimeUnit.SECONDS,
() -> message.getChannel().sendMessage(String.format("Request timed out.")).queue());
}

private void setupStarboard(Message message, List<String> args, Server server) {
if (starboardService.getStarboardConfig(server.getId()) != null)
message.getChannel().sendMessage("**A Starboard has already been set up here! Did you mean to use one of these?**\n\n" + this.getUsage()).queue();
else if (args.size() < 3)
message.getChannel().sendMessage("**You need more arguments than that!**\n\n" + this.getUsage()).queue();
else {
String channelID = ParsingUtils.filterSnowflake(args.get(1));
if (!ParsingUtils.isSnowflake(channelID))
message.getChannel().sendMessage("**The channel you provided is not valid!**").queue();
else if (!message.getGuild().getChannels().contains(message.getJDA().getGuildChannelById(channelID)))
message.getChannel().sendMessage("**The channel you provided is not in this server!**").queue();
else if (!ParsingUtils.isInteger(args.get(2)))
message.getChannel().sendMessage("**You need to specify a number for the star count!**").queue();
else if (Integer.parseInt(args.get(2)) < 1)
message.getChannel().sendMessage("**Your star count must be at least 1! We recommend 5**").queue();
else
starboardService.completeStarboardSetup(message, channelID, args.get(2), server);
getAwaitedReply(message, "**Please enter a channel ID or mention to be used for the starboard:**", response -> {
String channelID = ParsingUtils.filterSnowflake(response);
if (!ParsingUtils.isSnowflake(channelID))
message.getChannel().sendMessage("**The channel you provided is not valid!**").queue();
else if (!message.getGuild().getChannels().contains(message.getJDA().getGuildChannelById(channelID)))
message.getChannel().sendMessage("**The channel you provided is not in this server!**").queue();
else promptForStarCount(message, channelID, server);
});
}
}

private void promptForStarCount(Message message, String channelID, Server server) {
getAwaitedReply(message, "**Please enter the star count:**", response -> {
if (!ParsingUtils.isInteger(response))
message.getChannel().sendMessage("**You need to specify a number for the star count!**").queue();
else if (Integer.parseInt(response) < 1)
message.getChannel().sendMessage("**Your star count must be at least 1! We recommend 5**").queue();
else starboardService.completeStarboardSetup(message, channelID, response, server);
});
}

private void disableStarboard(Message message, Server server) {
if (starboardService.getStarboardConfig(server.getId()) != null)
starboardService.deleteStarboardConfig(message, server);
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/de/voidtech/gerald/service/StarboardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ public void completeStarboardSetup(Message message, String channelID, String sta
session.saveOrUpdate(config);
session.getTransaction().commit();

message.getChannel().sendMessage("**Starboard setup complete!**\n"
+ "Channel: <#" + channelID + ">\n"
+ "Stars required: " + starCount + "\n"
+ "Users must use the :star: emote!").queue();
message.getChannel().sendMessageEmbeds(createSetupEmbed(config)).queue();
}
}

private MessageEmbed createSetupEmbed(StarboardConfig config) {
EmbedBuilder setupEmbedBuilder = new EmbedBuilder()
.setColor(Color.ORANGE)
.setTitle("Setup complete!")
.setDescription("**Channel: <#" + config.getChannelID() + ">**\n"
+ "**Stars required:** " + config.getRequiredStarCount() + "\n"
+ "Users must react with the :star: emote to star a message!");
return setupEmbedBuilder.build();
}

//This method may look like something else, but it is used to check if the channel a star is added to is a starboard channel or not
public boolean reactionIsInStarboardChannel(String channelID, long serverID) {
try(Session session = sessionFactory.openSession())
Expand Down

0 comments on commit 62d1831

Please sign in to comment.