Hacky-Quizbot/src/main/java/linux/general/hackyquizbot/QuizHandler.java

58 lines
2.4 KiB
Java
Raw Normal View History

2022-02-20 18:41:38 +01:00
package linux.general.hackyquizbot;
2022-02-22 19:45:04 +01:00
import java.util.concurrent.ExecutionException;
import org.javacord.api.entity.message.Message;
2022-02-20 18:41:38 +01:00
import org.javacord.api.entity.user.User;
import com.vdurmont.emoji.EmojiParser;
2022-02-22 18:50:34 +01:00
import linux.general.hackyquizbot.QuizBackend.Question;
2022-02-22 18:09:37 +01:00
public class QuizHandler {
2022-02-20 18:41:38 +01:00
private User user;
2022-02-22 18:50:34 +01:00
private Question lastQuestion;
public QuizHandler(User user) {
2022-02-22 18:09:37 +01:00
this.user = user;
2022-02-22 18:50:34 +01:00
if (QuizBackend.shouldGetMoreQuestions(user.getIdAsString())) {
2022-02-22 19:45:04 +01:00
try {
Message message = user.sendMessage("Hi! I'm Hacky.\n"
+ "I will be sending you a bunch of multiple-choice questions. "
+ "You can answer by selecting one or more of the reactions on the message and confirm your selection by selecting the :white_check_mark: reaction."
+ " Please contact @BodgeMaster#0344 if there are any issues.\n\n"
2022-02-22 19:45:04 +01:00
+ "Select the :white_check_mark: reaction to continue.").get();
message.addReaction(EmojiParser.parseToUnicode(":white_check_mark:"));
2022-02-22 19:45:04 +01:00
message.addReactionAddListener(event -> {
// HACK: Testing for an exception to find out whether the event was caused by a
// user or by the bot itself because reactions added by the bot allow to check
// for user where reactions added by actual users dont.
try {
if (!event.getUser().get().isYourself() && event.getReaction().get().getEmoji()
.equalsEmoji(EmojiParser.parseToUnicode(":white_check_mark:"))) {
event.getMessage().get().getChannel().sendMessage(
"This should not work. If you ever see this message, please inform @BodgeMaster#0344 that there is more to this arbitrary limitation in the Discord API than expected.");
}
} catch (Exception e) {
System.err.println("Assuming the reaction event was caused by the user...");
System.err.println("User ID: "+this.user.getIdAsString()); //test if we can get the user ID that way
2022-02-22 19:45:04 +01:00
}
// TODO: trigger question dispatch
// TODO: remove ReactionAddListener
2022-02-22 19:45:04 +01:00
});
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
System.err.println("Something went wrong while trying to get the sent message object.");
e.printStackTrace();
}
2022-02-22 21:17:46 +01:00
// TODO: add white question mark reaction
// TODO: add reaction handler (preferably to channel?)
2022-02-22 18:50:34 +01:00
} else {
user.sendMessage(
"You have already had the maximum amount of questions that you can get with the current question set.");
2022-02-20 19:29:54 +01:00
}
}
2022-02-22 18:50:34 +01:00
2022-02-20 18:41:38 +01:00
}