Guess Song Command

This commit is contained in:
Dragon Fire
2021-03-09 19:49:09 -05:00
parent 77d2432379
commit 05776613f6
5 changed files with 137 additions and 3 deletions
+2
View File
@@ -54,6 +54,8 @@ IMGUR_KEY=
OPENWEATHERMAP_KEY=
OSU_KEY=
PERSONAL_GOOGLE_CALENDAR_ID=
SPOTIFY_KEY=
SPOTIFY_SECRET=
STACKOVERFLOW_KEY=
TENOR_KEY=
TMDB_KEY=
+4 -1
View File
@@ -222,6 +222,7 @@ API. All are free unless otherwise stated.
* `IMGUR_KEY` can be obtained by [Registering an Application at the Imgur website](https://api.imgur.com/oauth2/addclient).
* `OPENWEATHERMAP_KEY` can be obtained at the [OpenWeatherMap website](https://openweathermap.org/price). Click "Get API Key" on the plan you want (probably Free).
* `OSU_KEY` can be obtained by [signing up at the osu! API page](https://osu.ppy.sh/p/api/). Whether this link takes you to the right page or not is hit-or-miss.
* `SPOTIFY_KEY` and `SPOTIFY_SECRET` can be obtained at the [Spotify Developer Hub](https://developer.spotify.com/).
* `STACKOVERFLOW_KEY` can be obtained by [registering your app at stackapps](https://stackapps.com/apps/oauth/register).
* `TENOR_KEY` can be obtained by [Registering an Application at the Tenor website](https://tenor.com/developer/keyregistration).
* `THECATAPI_KEY` can be obtained at the [TheCatAPI website](https://thecatapi.com/).
@@ -266,7 +267,7 @@ in the appropriate channel's topic to use it.
## Commands
Total: 618
Total: 619
### Utility:
@@ -603,6 +604,7 @@ Total: 618
* **doors:** Open the right door, and you win the money! Make the wrong choice, and you get the fire!
* **fishy:** Go fishing.
* **google-feud:** Attempt to determine the top suggestions for a Google search.
* **guess-song:** Guess what song is playing.
* **hangman:** Prevent a man from being hanged by guessing a word as fast as you can.
* **hearing-test:** Test your hearing.
* **horse-info:** Responds with detailed information on a horse.
@@ -1837,6 +1839,7 @@ here.
- [Spongebob Fanon](https://spongebob-new-fanon.fandom.com/wiki/SpongeBob_Fanon_Wiki)
* spongebob-time-card ([Images](https://spongebob-new-fanon.fandom.com/wiki/Gallery_of_Textless_Title_Cards))
- [Spotify](https://www.spotify.com/us/)
* guess-song ([API](https://developer.spotify.com/))
* spotify-now-playing (Original Design)
- [Square Enix](https://square-enix-games.com/)
* nobody-name ([Original "Kingdom Hearts" Game](https://www.kingdomhearts.com/home/us/))
+124
View File
@@ -0,0 +1,124 @@
const Command = require('../../structures/Command');
const csvParse = require('csv-parse');
const { Readable } = require('stream');
const { reactIfAble, base64, list } = require('../../util/Util');
const otherSongs = require('../../assets/json/guess-song');
const { SPOTIFY_KEY, SPOTIFY_SECRET } = process.env;
module.exports = class GuessSongCommand extends Command {
constructor(client) {
super(client, {
name: 'guess-song',
aliases: ['song-guess', 'song-game', 'music-guess', 'guess-music', 'music-game'],
group: 'games-sp',
memberName: 'guess-song',
description: 'Guess what song is playing.',
throttling: {
usages: 1,
duration: 30
},
guildOnly: true,
userPermissions: ['CONNECT', 'SPEAK'],
clientPermissions: ['ATTACH_FILES'],
credit: [
{
name: 'Spotify',
url: 'https://www.spotify.com/us/',
reason: 'API',
reasonURL: 'https://developer.spotify.com/'
}
]
});
this.token = null;
this.charts = null;
this.cache = new Map();
}
async run(msg) {
const connection = this.client.voice.connections.get(msg.guild.id);
if (!connection) {
const usage = this.client.registry.commands.get('join').usage();
return msg.reply(`I am not in a voice channel. Use ${usage} to fix that!`);
}
const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.');
this.client.games.set(msg.channel.id, { name: this.name });
try {
if (!this.token) await this.fetchToken();
if (!this.charts) await this.fetchCharts();
const song = await this.fetchRandomSong();
const data = await this.fetchSongPreview(song);
const { body: previewBody } = await request.get(data.preview);
const dispatcher = connection.play(Readable.from([previewBody]));
this.client.dispatchers.set(msg.guild.id, dispatcher);
dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id));
dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id));
await reactIfAble(msg, this.client.user, '🔉');
await msg.reply('**You have 30 seconds, what song is this?**');
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
max: 1,
time: 30000
});
this.client.games.delete(msg.channel.id);
dispatcher.end();
this.client.dispatchers.delete(msg.guild.id);
if (!msgs.size) return msg.reply(`Time! It's **${data.name}** by **${data.artist}**!`);
const guess = msgs.first().content.toLowerCase();
if (guess.includes(data.name.toLowerCase())) {
return msg.reply(`Nope! It's **${data.name}** by **${data.artist}**!`);
}
return msg.reply(`Nice! It's **${data.name}** by **${data.artist}**!`);
} catch (err) {
this.client.dispatchers.delete(msg.guild.id);
this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
async fetchCharts() {
if (this.charts) return this.charts;
const { text } = await request.get('https://spotifycharts.com/regional/us/daily/latest/download');
return new Promise((res, rej) => {
csvParse(text, { comment: '#' }, (err, output) => {
if (err) return rej(error);
this.charts = output.slice(2).map(song => song[4].replace('https://open.spotify.com/track/', ''));
setTimeout(() => { this.charts = null; }, 4.32e+7);
return res(this.charts);
});
});
}
async fetchRandomSong() {
const songs = [...this.charts, otherSongs];
const choice = songs[Math.floor(Math.random() * songs.length)];
return choice;
}
async fetchSongPreview(id) {
if (this.cache.has(id)) return this.cache.get(id);
const { body } = await request
.get(`https://api.spotify.com/v1/tracks/${id}`)
.set({ Authorization: `Bearer ${this.token}` })
.query({ market: 'us' });
const result = {
id,
name: body.name,
artist: list(body.artists.map(artist => artist.name)),
preview: body.preview_url
};
this.cache.set(id, result);
return result;
}
async fetchToken() {
const { body } = await request
.post('https://accounts.spotify.com/api/token')
.set({ Authorization: `Basic ${base64(`${SPOTIFY_KEY}:${SPOTIFY_SECRET}`)}` })
.send('grant_type=client_credentials');
this.token = body.access_token;
setTimeout(() => { this.token = null; }, body.expires_in);
return body;
}
};
+6 -1
View File
@@ -85,12 +85,16 @@ module.exports = class WhosThatPokemonCryCommand extends Command {
}
const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
if (this.client.dispatchers.has(msg.guild.id)) return msg.reply('I am already playing audio in this server.');
this.client.games.set(msg.channel.id, { name: this.name });
try {
const data = await this.client.pokemon.fetch(pokemon.toString());
const names = data.names.map(name => name.name.toLowerCase());
const attachment = await this.client.registry.commands.get('whos-that-pokemon').createImage(data, false);
connection.play(data.cry);
const dispatcher = connection.play(data.cry);
this.client.dispatchers.set(msg.guild.id, dispatcher);
dispatcher.once('finish', () => this.client.dispatchers.delete(msg.guild.id));
dispatcher.once('error', () => this.client.dispatchers.delete(msg.guild.id));
await reactIfAble(msg, this.client.user, '🔉');
await msg.reply('**You have 15 seconds, who\'s that Pokémon?**');
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
@@ -107,6 +111,7 @@ module.exports = class WhosThatPokemonCryCommand extends Command {
}
return msg.reply(`Nice! It's **${data.name}**!`, { files: [attachment] });
} catch (err) {
this.client.dispatchers.delete(msg.guild.id);
this.client.games.delete(msg.channel.id);
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "131.12.2",
"version": "131.13.0",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {