add cat and dog command

This commit is contained in:
Lee 2024-06-27 14:45:08 +01:00
parent 576f7b156d
commit 175c8eba9f
4 changed files with 126 additions and 0 deletions

@ -0,0 +1,38 @@
package cc.fascinated.bat.command.impl.fun;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.model.token.thecatapi.CatImageToken;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
/**
* @author Fascinated (fascinated7)
*/
@Component
public class CatCommand extends BatCommand {
public CatCommand() {
super("cat", "Get a random cat image");
}
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
CatImageToken[] responseEntity = WebRequest.getAsEntity("https://api.thecatapi.com/v1/images/search", CatImageToken[].class);
if (responseEntity == null || responseEntity.length == 0) {
interaction.reply("Failed to get a cat image!").queue();
return;
}
interaction.replyEmbeds(EmbedUtils.genericEmbed()
.setAuthor("Here's a random cat image!")
.setImage(responseEntity[0].getUrl())
.build()).queue();
}
}

@ -0,0 +1,38 @@
package cc.fascinated.bat.command.impl.fun;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.model.token.dogceo.RandomImage;
import cc.fascinated.bat.model.token.thecatapi.CatImageToken;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.stereotype.Component;
/**
* @author Fascinated (fascinated7)
*/
@Component
public class DogCommand extends BatCommand {
public DogCommand() {
super("dog", "Get a random dog image");
}
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
RandomImage responseEntity = WebRequest.getAsEntity("https://dog.ceo/api/breeds/image/random", RandomImage.class);
if (responseEntity == null) {
interaction.reply("Failed to get a dog image!").queue();
return;
}
interaction.replyEmbeds(EmbedUtils.genericEmbed()
.setAuthor("Here's a random dog image!")
.setImage(responseEntity.getMessage())
.build()).queue();
}
}

@ -0,0 +1,20 @@
package cc.fascinated.bat.model.token.dogceo;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author Fascinated (fascinated7)
*/
@Getter @AllArgsConstructor
public class RandomImage {
/**
* The URL of the image.
*/
private String message;
/**
* The status of the request.
*/
private String status;
}

@ -0,0 +1,30 @@
package cc.fascinated.bat.model.token.thecatapi;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author Fascinated (fascinated7)
*/
@Getter @AllArgsConstructor
public class CatImageToken {
/**
* The ID of the image.
*/
private String id;
/**
* The URL of the image.
*/
private String url;
/**
* The width of the image.
*/
private int width;
/**
* The height of the image.
*/
private int height;
}