Search anime now gives options

master
Cristiano Pires 2 years ago
parent d36508a059
commit 686c73857a

@ -15,37 +15,93 @@ class anime extends Command{
}) })
} }
async run(message, args){ async run(message, args){
if(!args[0]) return new ErrorMessage(this.client).send(ErrorType.Arguments, message)
const data = await new aniList().searchAnime(args.join(' ')); const data = await new aniList().searchAnime(args.join(' '));
console.log(data) if(data=='Error') return new ErrorMessage(this.client).send(ErrorType.Arguments, message, [`Couldn't find any anime with the search argument you provided.`])
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setTitle(`${data.title.romaji} (${data.title.native} / ${data.title.english})`) .setTitle('First 4 results of search:')
.setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'}) .setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'})
.setColor(0x003284) .setColor(0x003284)
.setDescription(data.description.replaceAll('<br>',' ')) .setURL('https://www.rem.wordfights.com')
.setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' }) .setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' })
.setImage(data.coverImage) .setImage(data[0].coverImage)
.setThumbnail(data.coverImage)
.setTimestamp() .setTimestamp()
.addFields( .addFields(
{name:'Status:', value:data.status, inline:true}, {name:data[0].title.romaji+' ['+data[0].title.english+']', value:'1', inline:true},
{name:'Episodes:', value:data.episodes?data.episodes.toString():'N/A', inline:true}, {name:data[1].title.romaji+' ['+data[0].title.english+']', value:'2', inline:true},
{name:'Trailer:', value:data.trailer?(data.trailer.site=='yotube'?`https://www.youtube.com/watch?v=${data.trailer.id}`:`https://www.dailymotion.com/video/${data.trailer.id}`):'N/A'}); {name:data[2].title.romaji+' ['+data[0].title.english+']', value:'3', inline:true},
const randomID = Random(); {name:data[3].title.romaji+' ['+data[0].title.english+']', value:'4', inline:true},
const row = new ActionRowBuilder() );
.addComponents( var embeds = [embed]
new ButtonBuilder() embeds.push(new EmbedBuilder().setImage(data[1].coverImage).setURL('https://www.rem.wordfights.com'));
.setCustomId(randomID) embeds.push(new EmbedBuilder().setImage(data[2].coverImage).setURL('https://www.rem.wordfights.com'));
.setLabel('Remove this.') embeds.push(new EmbedBuilder().setImage(data[3].coverImage).setURL('https://www.rem.wordfights.com'));
.setStyle(ButtonStyle.Primary),
); const randomID = Random();
const filter = i => i.customId === randomID; const row = new ActionRowBuilder()
await message.channel.send({ephemeral: true, embeds: [embed], components: [row] }); .addComponents(
const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 }); new ButtonBuilder()
collector.on('collect', async m => .setCustomId(randomID+1)
{ .setLabel('1')
message.delete(); .setStyle(ButtonStyle.Success),
m.message.delete(); new ButtonBuilder()
}); .setCustomId(randomID+2)
.setLabel('2')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(randomID+3)
.setLabel('3')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId(randomID+4)
.setLabel('4')
.setStyle(ButtonStyle.Success),
);
const filter = i => i.customId.slice(0,-1) === randomID;
await message.channel.send({ephemeral: true, embeds, components: [row] });
const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
collector.on('collect', async m =>
{
message.delete();
m.message.delete();
sendMessage(data[m.customId[m.customId.length-1]-1].id);
});
/**
* @param {Number} id
*/
async function sendMessage(id)
{
const data = await new aniList().getAnimeInfo(id);
const embed = new EmbedBuilder()
.setTitle(`${data.title.romaji} (${data.title.native} / ${data.title.english})`)
.setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'})
.setColor(0x003284)
.setDescription(data.description.replaceAll('<br>',' '))
.setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' })
.setImage(data.coverImage)
.setThumbnail(data.coverImage)
.setTimestamp()
.addFields(
{name:'Status:', value:data.status, inline:true},
{name:'Episodes:', value:data.episodes?data.episodes.toString():'N/A', inline:true},
{name:'Trailer:', value:data.trailer?(data.trailer.site=='yotube'?`https://www.youtube.com/watch?v=${data.trailer.id}`:`https://www.dailymotion.com/video/${data.trailer.id}`):'N/A'});
const randomID = Random();
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId(randomID)
.setLabel('Remove this.')
.setStyle(ButtonStyle.Primary),
);
const filter = i => i.customId === randomID;
await message.channel.send({ephemeral: true, embeds: [embed], components: [row] });
const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
collector.on('collect', async m =>
{
m.message.delete();
});
}
} }
}module.exports = anime; }module.exports = anime;

@ -261,6 +261,7 @@ const ErrorType =
NoArguments: "No Arguments Given", NoArguments: "No Arguments Given",
NotOnTheList: "Unknown Command", NotOnTheList: "Unknown Command",
OldMessages: "Can't delete this messages." OldMessages: "Can't delete this messages."
} }
module.exports.ErrorType = ErrorType; module.exports.ErrorType = ErrorType;
@ -443,13 +444,75 @@ class aniListCli {
*/ */
async searchAnime(ss) async searchAnime(ss)
{ {
var variables = { var variables = {
ss ss,
"page": 0,
"perPage": 4
}; };
var query = `query ($ss: String) { var query = `query ($ss: String, $page: Int, $perPage: Int) {
Media(search:$ss,type: ANIME) { Page(page: $page, perPage: $perPage) {
media(search: $ss, type: ANIME) {
id
title {
romaji
english
}
coverImage {
medium
}
}
}
}
`
var url = 'https://graphql.anilist.co',
options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query,
variables
})
};
return await fetch(url, options).then(handleResponse)
.then(handleData)
.catch(handleError);
function handleResponse(response)
{
return response.json().then(function (json)
{
return response.ok ? json : Promise.reject(json);
});
}
function handleError(error) {
return error;
}
function handleData(data) {
data = data.data.Page.media
if(!data[0]) return 'Error'
var aux = [];
for(var anime of data)
{
aux.push({
title:anime.title,
id:anime.id,
coverImage:anime.coverImage.medium
});
}
return aux;
}
}
async getAnimeInfo(id)
{
var variables = {
id
};
var query = `query ($id: Int) {
Media(id:$id) {
id id
idMal
title { title {
romaji romaji
english english
@ -467,7 +530,7 @@ class aniListCli {
} }
} }
}`; }`;
var url = ``+'https://graphql.anilist.co', var url = 'https://graphql.anilist.co',
options = { options = {
method: 'POST', method: 'POST',
headers: { headers: {
@ -495,6 +558,7 @@ class aniListCli {
function handleData(data) { function handleData(data) {
return { return {
title:data.data.Media.title, title:data.data.Media.title,
malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal,
status:data.data.Media.status, status:data.data.Media.status,
episodes:data.data.Media.episodes, episodes:data.data.Media.episodes,
trailer:data.data.Media.trailer, trailer:data.data.Media.trailer,

Loading…
Cancel
Save