diff --git a/commands/search/searchAnime.js b/commands/search/searchAnime.js index fc74ba6..283d978 100644 --- a/commands/search/searchAnime.js +++ b/commands/search/searchAnime.js @@ -15,37 +15,93 @@ class anime extends Command{ }) } async run(message, args){ + if(!args[0]) return new ErrorMessage(this.client).send(ErrorType.Arguments, message) 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() - .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'}) .setColor(0x003284) - .setDescription(data.description.replaceAll('
',' ')) + .setURL('https://www.rem.wordfights.com') .setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' }) - .setImage(data.coverImage) - .setThumbnail(data.coverImage) + .setImage(data[0].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 => - { - message.delete(); - m.message.delete(); - }); + {name:data[0].title.romaji+' ['+data[0].title.english+']', value:'1', inline:true}, + {name:data[1].title.romaji+' ['+data[0].title.english+']', value:'2', inline:true}, + {name:data[2].title.romaji+' ['+data[0].title.english+']', value:'3', inline:true}, + {name:data[3].title.romaji+' ['+data[0].title.english+']', value:'4', inline:true}, + ); + var embeds = [embed] + embeds.push(new EmbedBuilder().setImage(data[1].coverImage).setURL('https://www.rem.wordfights.com')); + embeds.push(new EmbedBuilder().setImage(data[2].coverImage).setURL('https://www.rem.wordfights.com')); + embeds.push(new EmbedBuilder().setImage(data[3].coverImage).setURL('https://www.rem.wordfights.com')); + + const randomID = Random(); + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(randomID+1) + .setLabel('1') + .setStyle(ButtonStyle.Success), + 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('
',' ')) + .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; diff --git a/lib.js b/lib.js index d1ba6a5..9a5d9e5 100644 --- a/lib.js +++ b/lib.js @@ -261,6 +261,7 @@ const ErrorType = NoArguments: "No Arguments Given", NotOnTheList: "Unknown Command", OldMessages: "Can't delete this messages." + } module.exports.ErrorType = ErrorType; @@ -443,13 +444,75 @@ class aniListCli { */ async searchAnime(ss) { - var variables = { - ss + ss, + "page": 0, + "perPage": 4 }; - var query = `query ($ss: String) { - Media(search:$ss,type: ANIME) { + var query = `query ($ss: String, $page: Int, $perPage: Int) { + 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 + idMal title { romaji english @@ -467,7 +530,7 @@ class aniListCli { } } }`; - var url = ``+'https://graphql.anilist.co', + var url = 'https://graphql.anilist.co', options = { method: 'POST', headers: { @@ -495,6 +558,7 @@ class aniListCli { function handleData(data) { return { title:data.data.Media.title, + malLink:'https://myanimelist.net/anime/'+data.data.Media.idMal, status:data.data.Media.status, episodes:data.data.Media.episodes, trailer:data.data.Media.trailer,