Skip to content

Commit

Permalink
[New] Added searchanime command that sends an embed containing all po…
Browse files Browse the repository at this point in the history
…ssible info about the anime
  • Loading branch information
naseif committed Oct 18, 2021
1 parent d304257 commit d796c37
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 667 deletions.
87 changes: 87 additions & 0 deletions commands/Anime/searchAnime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const { embedMessage } = require("../../modules/embedSimple");
const { getAnimeInfo } = require("../../modules/get-anime-details");
const { SlashCommandBuilder } = require("@discordjs/builders");
const { logger } = require("../../modules/logger");

module.exports = {
name: "searchanime",
args: true,
aliases: ["san"],
description: "Searches for an Anime on Myanimelist",
usage: "san || searchanime",
async run(message, args, client) {
const searchString = args.join(" ");
if (!searchString)
return await message.channel.send({
embeds: [embedMessage("#9dcc37", "You have to provide an anime name!")],
});
try {
const Anime = await getAnimeInfo(searchString);
const animeEmbed = {
color: "#9dcc37",
title: `${Anime.titlerom} AKA ${Anime.titleeng ?? `${Anime.titlerom}`}`,
url: `${`https://myanimelist.net/anime/${Anime.malid}`}`,
author: {
name: `${message.member.user.username}`,
icon_url: `${message.member.user.avatarURL()}`,
},
description: `${Anime.synopsis}`,
thumbnail: {
url: `${Anime.imageUrl}`,
},
fields: [
{
name: "Status",
value: `${Anime.status}`,
},
{
name: "Rating",
value: `${Anime.rating}`,
inline: true,
},
{
name: "Score/Rank",
value: `${Anime.score}/${Anime.rank}`,
},
{
name: "Premiered",
value: Anime.premiered,
inline: true,
},
{
name: "Episodes Number",
value: `${Anime.episodes ? Anime.episodes : "Unknown"}`,
inline: true,
},
{
name: "Aired",
value: `${Anime.aired ? Anime.aired : "Unknown"}`,
inline: true,
},
{
name: "Genres",
value: `${Anime.genres.join(" - ")}`,
inline: true,
},
],

timestamp: new Date(),
};

await message.channel.send({ embeds: [animeEmbed] });
} catch (err) {
await message.channel.send({
embeds: [embedMessage("#9dcc37", `Could not find this Anime, Sry!`)],
});
logger(err.message, "error");
console.error(err);
}
},
data: new SlashCommandBuilder()
.setName("searchanime")
.setDescription("Searches for an Anime")
.addStringOption((option) =>
option.setName("name").setRequired(true).setDescription("anime to search")
),
async execute() {},
};
39 changes: 39 additions & 0 deletions modules/get-anime-details.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const jikanjs = require("jikanjs");
const { logger } = require("./logger");
jikanjs.settings.setBaseURL("https://api.jikan.moe/v3", 3);

/**
* Searches for an Anime on the Jikan Database
* @param {String} animeName
* @param {String} type
* @returns Object
*/

module.exports.getAnimeInfo = async (animeName, type = "anime") => {
try {
const getId = await jikanjs.search(type, animeName, [1]);
const animeId = getId.results[0].mal_id;
if (!animeId) throw new Error("No anime ID was found!");
const getFullInfo = await jikanjs.loadAnime(animeId, "/");
const genresArray = getFullInfo.genres.map((element) => element.name);

return {
malid: getFullInfo.mal_id,
imageUrl: getFullInfo.image_url,
titlerom: getFullInfo.title,
titleeng: getFullInfo.title_english,
episodes: getFullInfo.episodes,
status: getFullInfo.status,
rating: getFullInfo.rating,
score: getFullInfo.score,
rank: getFullInfo.rank,
synopsis: getFullInfo.synopsis,
premiered: getFullInfo.premiered,
genres: genresArray,
aired: getFullInfo.aired.string,
};
} catch (err) {
logger(err.message, "error");
throw err;
}
};
Loading

0 comments on commit d796c37

Please sign in to comment.