|
| 1 | +package technobot.commands.fun; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 4 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 5 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 6 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 7 | +import okhttp3.Call; |
| 8 | +import okhttp3.Callback; |
| 9 | +import okhttp3.Request; |
| 10 | +import okhttp3.Response; |
| 11 | +import technobot.TechnoBot; |
| 12 | +import technobot.commands.Category; |
| 13 | +import technobot.commands.Command; |
| 14 | +import technobot.data.json.Emote; |
| 15 | +import technobot.util.embeds.EmbedColor; |
| 16 | +import technobot.util.embeds.EmbedUtils; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +/** |
| 21 | + * Command that generates an image to match an emotion with another user |
| 22 | + * |
| 23 | + * @author TechnoVision |
| 24 | + */ |
| 25 | +public class ActionCommand extends Command { |
| 26 | + |
| 27 | + public ActionCommand(TechnoBot bot) { |
| 28 | + super(bot); |
| 29 | + this.name = "action"; |
| 30 | + this.description = "Express your emotions on others."; |
| 31 | + this.category = Category.FUN; |
| 32 | + this.args.add(new OptionData(OptionType.STRING, "action", "The action to perform.", true) |
| 33 | + .addChoice("bite", "bite") |
| 34 | + .addChoice("brofist", "brofist") |
| 35 | + .addChoice("cuddle", "cuddle") |
| 36 | + .addChoice("handhold", "handhold") |
| 37 | + .addChoice("hug", "hug") |
| 38 | + .addChoice("kiss", "kiss") |
| 39 | + .addChoice("lick", "lick") |
| 40 | + .addChoice("pat", "pat") |
| 41 | + .addChoice("pinch", "pinch") |
| 42 | + .addChoice("poke", "poke") |
| 43 | + .addChoice("punch", "punch") |
| 44 | + .addChoice("slap", "slap") |
| 45 | + .addChoice("smack", "smack") |
| 46 | + .addChoice("sorry", "sorry") |
| 47 | + .addChoice("stare", "stare") |
| 48 | + .addChoice("thumbsup", "thumbsup") |
| 49 | + .addChoice("tickle", "tickle") |
| 50 | + .addChoice("wave", "wave") |
| 51 | + .addChoice("wink", "wink")); |
| 52 | + this.args.add(new OptionData(OptionType.USER, "user", "The user to direct this action towards.", true)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void execute(SlashCommandInteractionEvent event) { |
| 57 | + String url = "https://api.otakugifs.xyz/gif?reaction="; |
| 58 | + String emote = event.getOption("action").getAsString(); |
| 59 | + url += emote; |
| 60 | + |
| 61 | + String target = event.getOption("user").getAsUser().getName(); |
| 62 | + String text = event.getUser().getName() + " "; |
| 63 | + switch (emote) { |
| 64 | + case "bite" -> text += "takes a bite out of "+target+"."; |
| 65 | + case "brofist" -> text += "and "+target+" brofist!"; |
| 66 | + case "cuddle" -> text += "cuddles with "+target+"."; |
| 67 | + case "handhold" -> text += "and "+target+" hold hands. How sweet <3"; |
| 68 | + case "hug" -> text += "gives "+target+" a big hug!"; |
| 69 | + case "kiss" -> text += "kisses "+target+"."; |
| 70 | + case "lick" -> text += "licks "+target+"... gross!"; |
| 71 | + case "pat" -> text += "gives "+target+" a little pat on the head"; |
| 72 | + case "pinch" -> text += "pinches "+target+". Ouch!"; |
| 73 | + case "poke" -> text += "gives "+target+" a little poke."; |
| 74 | + case "punch" -> text += "punches "+target+" right in the face!"; |
| 75 | + case "slap" -> text += "slaps "+target+". They deserved it!"; |
| 76 | + case "smack" -> text += "gives "+target+" a smack they will remember."; |
| 77 | + case "sorry" -> text += "apologizes to "+target+"."; |
| 78 | + case "stare" -> text += "won't stop starting at "+target+"..."; |
| 79 | + case "thumbsup" -> text += "gives "+target+" two thumbs up!"; |
| 80 | + case "tickle" -> text += "tickles "+target+"."; |
| 81 | + case "wave" -> text += "waves at "+target+"."; |
| 82 | + case "wink" -> text += "winks at "+target+"."; |
| 83 | + } |
| 84 | + |
| 85 | + // Asynchronous API call |
| 86 | + Request request = new Request.Builder().url(url).build(); |
| 87 | + String finalText = text; |
| 88 | + bot.httpClient.newCall(request).enqueue(new Callback() { |
| 89 | + @Override |
| 90 | + public void onFailure(Call call, IOException e) { |
| 91 | + String text = "I was unable to fetch that emote!"; |
| 92 | + event.replyEmbeds(EmbedUtils.createError(text)).setEphemeral(true).queue(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onResponse(Call call, final Response response) throws IOException { |
| 97 | + if (!response.isSuccessful()) throw new IOException(); |
| 98 | + Emote entity = bot.gson.fromJson(response.body().string(), Emote.class); |
| 99 | + |
| 100 | + EmbedBuilder embed = new EmbedBuilder() |
| 101 | + .setColor(EmbedColor.DEFAULT.color) |
| 102 | + .setAuthor(finalText, null, event.getUser().getEffectiveAvatarUrl()) |
| 103 | + .setImage(entity.getUrl()); |
| 104 | + event.replyEmbeds(embed.build()).queue(); |
| 105 | + } |
| 106 | + }); |
| 107 | + } |
| 108 | +} |
0 commit comments