|
| 1 | +package technobot.commands.fun; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 5 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 6 | +import okhttp3.Call; |
| 7 | +import okhttp3.Callback; |
| 8 | +import okhttp3.Request; |
| 9 | +import okhttp3.Response; |
| 10 | +import technobot.TechnoBot; |
| 11 | +import technobot.commands.Category; |
| 12 | +import technobot.commands.Command; |
| 13 | +import technobot.util.embeds.EmbedColor; |
| 14 | +import technobot.util.embeds.EmbedUtils; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Command that creates a fun 'would you rather' poll using an API. |
| 20 | + * |
| 21 | + * @author TechnoVision |
| 22 | + */ |
| 23 | +public class WouldYouRatherCommand extends Command { |
| 24 | + |
| 25 | + public WouldYouRatherCommand(TechnoBot bot) { |
| 26 | + super(bot); |
| 27 | + this.name = "wouldyourather"; |
| 28 | + this.description = "Answer a \"would you rather\" question with your friends."; |
| 29 | + this.category = Category.FUN; |
| 30 | + } |
| 31 | + |
| 32 | + public void execute(SlashCommandInteractionEvent event) { |
| 33 | + event.deferReply().queue(); |
| 34 | + Request request = new Request.Builder() |
| 35 | + .url("https://would-you-rather-api--abaanshanid.repl.co/") |
| 36 | + .addHeader("Accept", "application/json") |
| 37 | + .build(); |
| 38 | + |
| 39 | + // Asynchronous API call |
| 40 | + bot.httpClient.newCall(request).enqueue(new Callback() { |
| 41 | + @Override |
| 42 | + public void onFailure(Call call, IOException e) { |
| 43 | + String text = "I was unable to fetch any questions!"; |
| 44 | + event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onResponse(Call call, final Response response) throws IOException { |
| 49 | + if (!response.isSuccessful()) { |
| 50 | + String text = "I was unable to fetch any questions!"; |
| 51 | + event.getHook().sendMessageEmbeds(EmbedUtils.createError(text)).queue(); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + // Build 'Would you Rathe' embed from API response |
| 56 | + String questions = bot.gson.fromJson(response.body().string(), JsonObject.class).get("data").getAsString(); |
| 57 | + String[] split = questions.split(" or "); |
| 58 | + String optionA = ":regional_indicator_a: "+split[0].substring(17); |
| 59 | + String optionB = ":regional_indicator_b: "+split[1].substring(0, split[1].length()-1); |
| 60 | + EmbedBuilder embed = new EmbedBuilder() |
| 61 | + .setTitle("Would you rather...") |
| 62 | + .setColor(EmbedColor.DEFAULT.color) |
| 63 | + .setDescription(optionA) |
| 64 | + .addField("or", optionB, false); |
| 65 | + |
| 66 | + // Send embed and add emoji reactions |
| 67 | + event.getHook().sendMessageEmbeds(embed.build()).queue(msg -> { |
| 68 | + msg.addReaction("\uD83C\uDDE6").queue(); |
| 69 | + msg.addReaction("\uD83C\uDDE7").queue(); |
| 70 | + }); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
0 commit comments