mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-21 14:04:38 +02:00
Fix anime score once and for all
This commit is contained in:
@@ -55,10 +55,15 @@ module.exports = class AnimeScoreCommand extends Command {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.totalAnime = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg) {
|
async run(msg) {
|
||||||
const anime = await this.getRandomAnime(msg.channel.nsfw);
|
const anime = await this.getRandomAnime();
|
||||||
|
if (anime === 'ratelimited') {
|
||||||
|
return msg.reply('Sorry, looks like the command needs to cool down. Try again later.');
|
||||||
|
}
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor(0x02A9FF)
|
.setColor(0x02A9FF)
|
||||||
.setAuthor({ name: 'AniList', iconURL: logos.anilist, url: 'https://anilist.co/' })
|
.setAuthor({ name: 'AniList', iconURL: logos.anilist, url: 'https://anilist.co/' })
|
||||||
@@ -84,28 +89,43 @@ module.exports = class AnimeScoreCommand extends Command {
|
|||||||
return msg.reply(`Nice job! It was **${anime.averageScore}%**!`);
|
return msg.reply(`Nice job! It was **${anime.averageScore}%**!`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRandomAnime(nsfw) {
|
async getRandomAnime() {
|
||||||
const { body: initialBody } = await request
|
try {
|
||||||
|
if (!this.totalAnime) await this.getTotalAnime();
|
||||||
|
const selectedAnime = Math.floor(Math.random() * this.totalAnime);
|
||||||
|
const { body } = await request
|
||||||
|
.post('https://graphql.anilist.co/')
|
||||||
|
.send({
|
||||||
|
variables: {
|
||||||
|
page: Math.ceil(selectedAnime / 50),
|
||||||
|
type: 'ANIME',
|
||||||
|
isAdult: false
|
||||||
|
},
|
||||||
|
query: searchGraphQL
|
||||||
|
});
|
||||||
|
const selected = body.data.Page.media[selectedAnime % 50];
|
||||||
|
if (!selected) return this.getRandomAnime();
|
||||||
|
return selected;
|
||||||
|
} catch (err) {
|
||||||
|
if (err.status === 429) return 'ratelimited';
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getTotalAnime() {
|
||||||
|
if (this.totalAnime) return this.totalAnime;
|
||||||
|
const { body } = await request
|
||||||
.post('https://graphql.anilist.co/')
|
.post('https://graphql.anilist.co/')
|
||||||
.send({
|
.send({
|
||||||
variables: {
|
variables: {
|
||||||
page: 0,
|
page: 0,
|
||||||
type: 'ANIME',
|
type: 'ANIME',
|
||||||
isAdult: Boolean(nsfw)
|
isAdult: false
|
||||||
},
|
},
|
||||||
query: searchGraphQL
|
query: searchGraphQL
|
||||||
});
|
});
|
||||||
const selectedAnime = Math.floor(Math.random() * initialBody.data.Page.pageInfo.total);
|
this.totalAnime = body.data.Page.pageInfo.total;
|
||||||
const { body } = await request
|
setTimeout(() => { this.totalAnime = null; }, 2.16e+7);
|
||||||
.post('https://graphql.anilist.co/')
|
return this.totalAnime;
|
||||||
.send({
|
|
||||||
variables: {
|
|
||||||
page: Math.ceil(selectedAnime / 50),
|
|
||||||
type: 'ANIME',
|
|
||||||
isAdult: Boolean(nsfw)
|
|
||||||
},
|
|
||||||
query: searchGraphQL
|
|
||||||
});
|
|
||||||
return body.data.Page.media[selectedAnime % 50];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user