1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-23 00:23:17 +00:00

Update index.js

This commit is contained in:
MatteZ02 2019-07-10 13:01:42 +03:00
parent 3dea69bfec
commit 6a1d049938

View File

@ -4,27 +4,13 @@ const YouTube = require('simple-youtube-api')
const ytdl = require('ytdl-core'); const ytdl = require('ytdl-core');
const GOOGLE_API_KEY = (process.env.API_KEY) const GOOGLE_API_KEY = (process.env.API_KEY)
const PREFIX = ('-') const PREFIX = ('-')
const client = new Client({ disableEveryone: true }); const client = new Client({ disableEveryone: true });
const youtube = new YouTube(GOOGLE_API_KEY); const youtube = new YouTube(GOOGLE_API_KEY);
const queue = new Map(); const queue = new Map();
client.on('ready', () => {
client.on('warn', console.warn);
client.on('error', console.error);
client.on('disconnect', () => console.log('- Disconnected -'));
client.on('reconnecting', () => console.log('- Reconnecting -'));
client.on('ready', () => { //startup stuff
console.log('- Connected -');
client.user.setActivity('-help', { type: 'LISTENING' }) client.user.setActivity('-help', { type: 'LISTENING' })
client.user.setStatus('dnd'); client.user.setStatus('dnd');
}); });
client.on('message', async msg => { client.on('message', async msg => {
if (!msg.guild || msg.author.bot) return undefined; if (!msg.guild || msg.author.bot) return undefined;
if (msg.content.toUpperCase().startsWith(`MUSIX`)) { if (msg.content.toUpperCase().startsWith(`MUSIX`)) {
@ -42,7 +28,6 @@ client.on('message', async msg => {
msg.channel.send(':x: I\'m sorry, But you can\'t do that!') msg.channel.send(':x: I\'m sorry, But you can\'t do that!')
} }
if (msg.author.username === 'Matte') { if (msg.author.username === 'Matte') {
console.log('restarting...')
msg.channel.send('Restarting...') msg.channel.send('Restarting...')
} }
return undefined; return undefined;
@ -134,8 +119,6 @@ client.on('message', async msg => {
${videos.map(video2 => `**${++index} -** ${video2.title}`).join('\n')} ${videos.map(video2 => `**${++index} -** ${video2.title}`).join('\n')}
Please provide a value to select one of the search results ranging from __1-10__. Please provide a value to select one of the search results ranging from __1-10__.
`); `);
// eslint-disable-next-line max-depth
console.log('trying response')
try { try {
var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, { var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
maxMatches: 1, maxMatches: 1,
@ -143,20 +126,14 @@ Please provide a value to select one of the search results ranging from __1-10__
errors: ['time'] errors: ['time']
}); });
} catch (err) { } catch (err) {
console.log('Error 1')
console.error(err);
return msg.channel.send(':x: No or invalid value entered, cancelling song selection.'); return msg.channel.send(':x: No or invalid value entered, cancelling song selection.');
} }
console.log('Start playing')
const videoIndex = parseInt(response.first().content); const videoIndex = parseInt(response.first().content);
var video = await youtube.getVideoByID(videos[videoIndex - 1].id); var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
} catch (err) { } catch (err) {
console.log('Error 2')
console.error(err);
return msg.channel.send(':x: I could not obtain any search results.'); return msg.channel.send(':x: I could not obtain any search results.');
} }
} }
console.log('Play command done')
return handleVideo(video, msg, voiceChannel); return handleVideo(video, msg, voiceChannel);
} }
} else if (command === 'skip') { } else if (command === 'skip') {
@ -175,7 +152,6 @@ Please provide a value to select one of the search results ranging from __1-10__
if (!serverQueue) return msg.channel.send(':x: There is nothing playing.'); if (!serverQueue) return msg.channel.send(':x: There is nothing playing.');
if (!args[1]) return msg.channel.send(`The current volume is: **${serverQueue.volume}** :speaker:`); if (!args[1]) return msg.channel.send(`The current volume is: **${serverQueue.volume}** :speaker:`);
if (isNaN(args[1])) { if (isNaN(args[1])) {
console.log('NAN')
return msg.channel.send(':x: I\'m sorry, But you need to enter a valid __number__.') return msg.channel.send(':x: I\'m sorry, But you need to enter a valid __number__.')
} }
serverQueue.volume = args[1]; serverQueue.volume = args[1];
@ -272,13 +248,11 @@ Please provide a value to select one of the search results ranging from 1-10.
errors: ['time'] errors: ['time']
}); });
} catch (err) { } catch (err) {
console.error(err);
return msg.channel.send(':x: No or invalid value entered, cancelling video selection.'); return msg.channel.send(':x: No or invalid value entered, cancelling video selection.');
} }
const videoIndex = parseInt(response.first().content); const videoIndex = parseInt(response.first().content);
var video = await youtube.getVideoByID(videos[videoIndex - 1].id); var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
} catch (err) { } catch (err) {
console.error(err);
return msg.channel.send(':x: I could not obtain any search results.'); return msg.channel.send(':x: I could not obtain any search results.');
} }
} }
@ -340,10 +314,8 @@ Please provide a value to select one of the search results ranging from 1-10.
return undefined; return undefined;
} }
}); });
async function handleVideo(video, msg, voiceChannel, playlist = false) { async function handleVideo(video, msg, voiceChannel, playlist = false) {
const serverQueue = queue.get(msg.guild.id); const serverQueue = queue.get(msg.guild.id);
console.log(video);
const song = { const song = {
id: video.id, id: video.id,
title: Util.escapeMarkdown(video.title), title: Util.escapeMarkdown(video.title),
@ -359,49 +331,39 @@ async function handleVideo(video, msg, voiceChannel, playlist = false) {
playing: true playing: true
}; };
queue.set(msg.guild.id, queueConstruct); queue.set(msg.guild.id, queueConstruct);
queueConstruct.songs.push(song); queueConstruct.songs.push(song);
try { try {
var connection = await voiceChannel.join(); var connection = await voiceChannel.join();
queueConstruct.connection = connection; queueConstruct.connection = connection;
play(msg.guild, queueConstruct.songs[0]); play(msg.guild, queueConstruct.songs[0]);
} catch (error) { } catch (error) {
console.error(`I could not join the voice channel: ${error}`);
queue.delete(msg.guild.id); queue.delete(msg.guild.id);
return msg.channel.send(`:x: I could not join the voice channel: ${error}`); return msg.channel.send(`:x: I could not join the voice channel: ${error}`);
} }
} else { } else {
serverQueue.songs.push(song); serverQueue.songs.push(song);
console.log(serverQueue.songs);
if (playlist) return undefined; if (playlist) return undefined;
else return msg.channel.send(`:white_check_mark: **${song.title}** has been added to the queue!`); else return msg.channel.send(`:white_check_mark: **${song.title}** has been added to the queue!`);
} }
return undefined; return undefined;
} }
function play(guild, song) { function play(guild, song) {
const serverQueue = queue.get(guild.id); const serverQueue = queue.get(guild.id);
if (!song) { if (!song) {
serverQueue.voiceChannel.leave(); serverQueue.voiceChannel.leave();
queue.delete(guild.id); queue.delete(guild.id);
return; return;
} }
console.log(serverQueue.songs);
const dispatcher = serverQueue.connection.playStream(ytdl(song.url, { quality: `highestaudio`, filter: 'audioonly' })) const dispatcher = serverQueue.connection.playStream(ytdl(song.url, { quality: `highestaudio`, filter: 'audioonly' }))
.on('end', reason => { .on('end', reason => {
if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.'); if (reason === 'Stream is not generating quickly enough.')
else console.log(reason); else msg.channel.send(reason);
serverQueue.songs.shift(); serverQueue.songs.shift();
play(guild, serverQueue.songs[0]); play(guild, serverQueue.songs[0]);
}) })
.on('error', error => console.error(error)); .on('error', error => msg.channel.send(error));
dispatcher.setVolumeLogarithmic(1 / 5); dispatcher.setVolumeLogarithmic(1 / 5);
serverQueue.volume = 1 serverQueue.volume = 1
serverQueue.textChannel.send(`:musical_note: Start playing: **${song.title}**`); serverQueue.textChannel.send(`:musical_note: Start playing: **${song.title}**`);
} }
client.login(process.env.BOT_TOKEN); client.login(process.env.BOT_TOKEN);