Use Playlist API in guess-song

This commit is contained in:
Dragon Fire
2021-05-01 15:41:33 -04:00
parent 4fe35aec8e
commit a9987f891b
3 changed files with 36 additions and 31 deletions
+6 -5
View File
@@ -1,5 +1,6 @@
[ {
"3DamFFqW32WihKkTVlwTYQ", "us": "37i9dQZEVXbLRQDuF5jeBp",
"7GhIk7Il098yCjg4BQjzvb", "uk": "37i9dQZEVXbLnolsZ8PSNw",
"3cfOd4CMv2snFaKAnMdnvK" "jp": "37i9dQZEVXbKXQ4mDTEBXq",
] "owlcity": "37i9dQZF1DZ06evO00faq4"
}
+29 -24
View File
@@ -1,10 +1,9 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const request = require('node-superfetch'); const request = require('node-superfetch');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const csvParse = require('csv-parse');
const { Readable } = require('stream'); const { Readable } = require('stream');
const { reactIfAble, base64, list } = require('../../util/Util'); const { reactIfAble, base64, list } = require('../../util/Util');
const otherSongs = require('../../assets/json/guess-song'); const playlists = require('../../assets/json/guess-song');
const { SPOTIFY_KEY, SPOTIFY_SECRET } = process.env; const { SPOTIFY_KEY, SPOTIFY_SECRET } = process.env;
module.exports = class GuessSongCommand extends Command { module.exports = class GuessSongCommand extends Command {
@@ -29,15 +28,24 @@ module.exports = class GuessSongCommand extends Command {
reason: 'API', reason: 'API',
reasonURL: 'https://developer.spotify.com/' reasonURL: 'https://developer.spotify.com/'
} }
],
args: [
{
key: 'chart',
prompt: `What chart do you want to use for the game? Either ${list(Object.keys(playlists, 'or'))}.`,
type: 'string',
oneOf: Object.keys(playlists),
parse: chart => chart.toLowerCase()
}
] ]
}); });
this.token = null; this.token = null;
this.charts = null; this.charts = new Map();
this.cache = new Map(); this.cache = new Map();
} }
async run(msg) { async run(msg, { chart }) {
const connection = this.client.voice.connections.get(msg.guild.id); const connection = this.client.voice.connections.get(msg.guild.id);
if (!connection) { if (!connection) {
const usage = this.client.registry.commands.get('join').usage(); const usage = this.client.registry.commands.get('join').usage();
@@ -50,10 +58,7 @@ module.exports = class GuessSongCommand extends Command {
let songID; let songID;
try { try {
if (!this.token) await this.fetchToken(); if (!this.token) await this.fetchToken();
if (!this.charts) await this.fetchCharts(); const data = await this.fetchRandomSong(chart);
const song = await this.fetchRandomSong();
songID = song;
const data = await this.fetchSongPreview(song);
const { body: previewBody } = await request.get(data.preview); const { body: previewBody } = await request.get(data.preview);
const dispatcher = connection.play(Readable.from([previewBody])); const dispatcher = connection.play(Readable.from([previewBody]));
this.client.dispatchers.set(msg.guild.id, dispatcher); this.client.dispatchers.set(msg.guild.id, dispatcher);
@@ -81,26 +86,26 @@ module.exports = class GuessSongCommand extends Command {
} }
} }
async fetchCharts() { async fetchCharts(playlist) {
if (this.charts) return this.charts; if (this.charts.has(playlist)) return this.charts.get(playlist);
const { text } = await request.get('https://spotifycharts.com/regional/us/daily/latest/download'); const { body } = await request
return new Promise((res, rej) => { .get('https://spotifycharts.com/regional/us/daily/latest/download')
csvParse(text, { comment: '#' }, (err, output) => { .set({ Authorization: `Bearer ${this.token}` })
if (err) return rej(err); .query({
this.charts = output.slice(2).map(song => song[4].replace('https://open.spotify.com/track/', '')); market: 'US',
setTimeout(() => { fields: 'items(track(id))',
this.charts = null; limit: 100
this.cache.clear();
}, 4.32e+7);
return res(this.charts);
}); });
}); const list = body.items.map(item => item.track.id);
this.charts.set(playlist, list);
setTimeout(() => this.charts.delete(playlist), 4.32e+7);
return list;
} }
fetchRandomSong() { async fetchRandomSong(playlist) {
const songs = [...this.charts, otherSongs]; const songs = await this.fetchCharts(playlist);
const choice = songs[Math.floor(Math.random() * songs.length)]; const choice = songs[Math.floor(Math.random() * songs.length)];
return choice; return this.fetchSongPreview(choice);
} }
async fetchSongPreview(id) { async fetchSongPreview(id) {
+1 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "138.3.0", "version": "138.4.0",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"private": true, "private": true,
@@ -44,7 +44,6 @@
"cloc": "^2.7.0", "cloc": "^2.7.0",
"common-tags": "^1.8.0", "common-tags": "^1.8.0",
"connect4-ai": "^0.1.3", "connect4-ai": "^0.1.3",
"csv-parse": "^4.15.4",
"custom-translate": "^2.2.8", "custom-translate": "^2.2.8",
"didyoumean2": "^4.2.0", "didyoumean2": "^4.2.0",
"discord.js": "^12.5.3", "discord.js": "^12.5.3",