Skip to content

Commit

Permalink
[New] Added whatanime command that takes an image link as input and r…
Browse files Browse the repository at this point in the history
…eturns the anime name after scanning
  • Loading branch information
naseif committed Sep 3, 2021
1 parent 21b1cff commit 9ab51a0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
51 changes: 51 additions & 0 deletions commands/anime/whatAnime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const { getAnimeByImage } = require("./../../modules/getAnimeByImage");

module.exports = {
data: new SlashCommandBuilder()
.setName("whatanime")
.setDescription("Scans an Image to get the anime name! <whatanime 'link'>")
.addStringOption((option) =>
option.setName("image").setDescription("Image Link").setRequired(true)
),
async execute(interaction, client) {
await interaction.deferReply();
const imageLink = interaction.options.getString("image");
try {
const res = await getAnimeByImage(imageLink);
const resultArray = res.result.slice(0, 3).map((info, index) => {
return (
`${index + 1}: ` +
"`" +
`${
info.anilist.title.english
? info.anilist.title.english
: `Not Available`
}` +
"`" +
" **AKA** " +
"`" +
`${info.anilist.title.romaji}` +
"`" +
" **Similarity** " +
`${info.similarity.toFixed(2) * 100}%`
);
});
const infoEmbed = {
color: "#9dcc37",
title: "Search Result",
description: `${resultArray.join("\n")}`,
timestamp: new Date(),
footer: {
text: `Requested by ${interaction.user.username}`,
icon_url: `${interaction.user.avatarURL()}`,
},
};
await interaction.followUp({ embeds: [infoEmbed] });
} catch (error) {
await interaction.followUp(
`${interaction.user.toString()}, There was an error processing this image!`
);
}
},
};
15 changes: 15 additions & 0 deletions modules/getAnimeByImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const fetch = require("node-fetch");

module.exports.getAnimeByImage = async (imageLink) => {
try {
const processImage = await fetch(
`https://api.trace.moe/search?anilistInfo&url=${encodeURIComponent(
imageLink
)}`
);
const handleResponse = await processImage.json();
return handleResponse;
} catch (err) {
throw err;
}
};

0 comments on commit 9ab51a0

Please sign in to comment.