eximiabots-radiox/commands/play.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-03-02 19:38:42 +00:00
module.exports = {
name: 'play',
alias: 'p',
usage: '<song name>',
description: 'Play some music.',
permission: 'none',
category: 'music',
async execute(msg, args, client, Discord, prefix) {
2020-03-09 11:17:47 +00:00
let url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : "";
2020-03-02 19:38:42 +00:00
const radio = client.radio.get(msg.guild.id);
const voiceChannel = msg.member.voice.channel;
if (!radio) {
2020-03-12 00:59:52 +00:00
if (!msg.member.voice.channel) return msg.channel.send('You need to be in a voice channel to play music!');
2020-03-02 19:38:42 +00:00
} else {
2020-03-12 00:59:52 +00:00
if (voiceChannel !== radio.voiceChannel) return msg.channel.send('You need to be in the same voice channel as RadioX to play music!');
2020-03-02 19:38:42 +00:00
}
2020-03-12 00:50:05 +00:00
if (!args[1]) return msg.channel.send('You need to use a number or search for a supported station!');
2020-03-02 19:38:42 +00:00
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT')) {
2020-03-12 00:50:05 +00:00
return msg.channel.send('I cannot connect to your voice channel.');
2020-03-02 19:38:42 +00:00
}
if (!permissions.has('SPEAK')) {
2020-03-12 00:50:05 +00:00
return msg.channel.send('I cannot speak in your voice channel.');
2020-03-02 19:38:42 +00:00
}
2020-03-09 11:17:47 +00:00
let station;
const number = parseInt(args[1] - 1);
if (url.startsWith('http')) {
return;
} else if (!isNaN(number)) {
if (number > client.stations.length - 1) {
2020-03-12 00:59:52 +00:00
return msg.channel.send('No such station!');
2020-03-09 11:17:47 +00:00
} else {
url = client.stations[number].stream[client.stations[number].stream.default];
station = client.stations[number];
}
} else {
2020-03-12 00:59:52 +00:00
if (args[1].length < 4) return msg.channel.send('Station must be over 3 characters!');
2020-03-09 11:17:47 +00:00
const sstation = await client.funcs.searchStation(args.slice(1).join(' '), client);
2020-03-11 21:11:00 +00:00
if (!sstation) return msg.channel.send('No stations found!');
2020-03-09 11:17:47 +00:00
url = sstation.stream[sstation.stream.default];
station = sstation
}
2020-03-07 20:07:54 +00:00
if (radio) {
2020-03-08 15:03:39 +00:00
radio.connection.dispatcher.destroy();
2020-03-09 11:17:47 +00:00
radio.station = station;
client.funcs.play(msg.guild, client, url);
2020-03-08 15:31:53 +00:00
return;
2020-03-07 20:07:54 +00:00
}
2020-03-09 11:17:47 +00:00
const construct = {
2020-03-07 20:07:54 +00:00
textChannel: msg.channel,
voiceChannel: voiceChannel,
connection: null,
playing: false,
2020-03-09 11:17:47 +00:00
station: station,
2020-03-07 20:07:54 +00:00
volume: client.config.volume,
2020-03-09 11:17:47 +00:00
time: null
2020-03-07 20:07:54 +00:00
};
client.radio.set(msg.guild.id, construct);
2020-03-09 11:17:47 +00:00
2020-03-07 20:07:54 +00:00
try {
const connection = await voiceChannel.join();
construct.connection = connection;
2020-03-09 11:17:47 +00:00
client.funcs.play(msg.guild, client, url);
2020-03-07 20:07:54 +00:00
} catch (error) {
client.radio.delete(msg.guild.id);
client.debug_channel.send("Error with connecting to voice channel: " + error);
2020-03-12 00:50:05 +00:00
return msg.channel.send(`An error occured: ${error}`);
2020-03-07 20:07:54 +00:00
}
2020-03-02 19:38:42 +00:00
}
};