diff --git a/commands/nowplaying.js b/commands/nowplaying.js index 3802012..360a068 100644 --- a/commands/nowplaying.js +++ b/commands/nowplaying.js @@ -14,8 +14,25 @@ module.exports = { const embed = new Discord.MessageEmbed() .setTitle("Now Playing") .setColor(client.config.embedColor) - .setDescription(`**${radio.station.name}** \n Owner: ${radio.station.owner} \n\`${client.funcs.msToTime(completed, "hh:mm:ss")}\``) + .setDescription(`**${radio.station.name}** \n Owner: ${radio.station.owner} \n\`${msToTime(completed, "hh:mm:ss")}\``) .setFooter('EximiaBots by Warén Media'); return msg.channel.send(embed); } -}; \ No newline at end of file +}; +function msToTime(duration, format) { + var seconds = Math.floor((duration / 1000) % 60), + minutes = Math.floor((duration / (1000 * 60)) % 60), + hours = Math.floor((duration / (1000 * 60 * 60)) % 24); + days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24); + + days = (days < 10) ? "0" + days : days; + hours = (hours < 10) ? "0" + hours : hours; + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + + if (format === "hh:mm:ss") { + return `${hours}:${minutes}:${seconds}`; + } else if (format === "dd:hh:mm:ss") { + return `${days}:${hours}:${minutes}:${seconds}`; + } +} \ No newline at end of file diff --git a/commands/play.js b/commands/play.js index 1b6bbb1..5e266cd 100644 --- a/commands/play.js +++ b/commands/play.js @@ -34,7 +34,8 @@ module.exports = { station = client.stations[number]; } } else { - const sstation = await client.funcs.searchStation(args.slice(1).join(' '), client); + if (args[1].length < 3) return msg.channel.send('Station must be over 2 characters!'); + const sstation = await searchStation(args.slice(1).join(' '), client); if (!sstation) return msg.channel.send('No stations found!'); url = sstation.stream[sstation.stream.default]; station = sstation @@ -43,7 +44,7 @@ module.exports = { if (radio) { radio.connection.dispatcher.destroy(); radio.station = station; - client.funcs.play(msg.guild, client, url); + play(msg.guild, client, url); return; } @@ -61,7 +62,7 @@ module.exports = { try { const connection = await voiceChannel.join(); construct.connection = connection; - client.funcs.play(msg.guild, client, url); + play(msg.guild, client, url); } catch (error) { client.radio.delete(msg.guild.id); client.debug_channel.send("Error with connecting to voice channel: " + error); @@ -69,3 +70,71 @@ module.exports = { } } }; +function play(guild, client, url) { + + const radio = client.radio.get(guild.id); + + const dispatcher = radio.connection + .play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 }) + .on("finish", () => { + radio.voiceChannel.leave(); + client.radio.delete(guild.id); + return; + }); + + dispatcher.on('start', () => { + dispatcher.player.streamingData.pausedTime = 0; + }); + + dispatcher.on('error', error => { + console.error(error); + radio.voiceChannel.leave(); + client.radio.delete(guild.id); + return radio.textChannel.send('An error has occured while playing radio!'); + }); + + dispatcher.setVolume(radio.volume / 10); + + radio.textChannel.send(`Start playing: ${radio.station.name}`); + radio.playing = true; + +}; + +function searchStation(key, client) { + if (client.stations === null) return false; + let foundStations = []; + if (!key) return false; + if (key == 'radio') return false; + if (key.startsWith("radio ")) key = key.slice(6); + const probabilityIncrement = 100 / key.split(' ').length / 2; + for (let i = 0; i < key.split(' ').length; i++) { + client.stations.filter(x => x.name.toUpperCase().includes(key.split(' ')[i].toUpperCase()) || x === key).forEach(x => foundStations.push({ station: x, name: x.name, probability: probabilityIncrement })); + } + if (foundStations.length === 0) return false; + for (let i = 0; i < foundStations.length; i++) { + for (let j = 0; j < foundStations.length; j++) { + if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); + } + } + for (let i = 0; i < foundStations.length; i++) { + if (foundStations[i].name.length > key.length) { + foundStations[i].probability -= (foundStations[i].name.split(' ').length - key.split(' ').length) * (probabilityIncrement * 0.5); + } else if (foundStations[i].name.length === key.length) { + foundStations[i].probability += (probabilityIncrement * 0.9); + } + + for (let j = 0; j < key.split(' ').length; j++) { + if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(' ')[j])) { + foundStations[i].probability -= (probabilityIncrement * 0.5); + } + } + } + let highestProbabilityStation; + for (let i = 0; i < foundStations.length; i++) { + if (!highestProbabilityStation || highestProbabilityStation.probability < foundStations[i].probability) highestProbabilityStation = foundStations[i]; + if (highestProbabilityStation && highestProbabilityStation.probability === foundStations[i].probability) { + highestProbabilityStation = foundStations[i].station; + } + } + return highestProbabilityStation; +}; diff --git a/commands/stop.js b/commands/stop.js index 3cecc70..4192c9a 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -7,9 +7,11 @@ module.exports = { category: 'music', execute(msg, args, client, Discord, prefix, command) { const radio = client.radio.get(msg.guild.id); - radio.connection.dispatcher.destroy(); - radio.voiceChannel.leave(); - client.radio.delete(msg.guild.id); - msg.channel.send('Stopped playback!'); + if (client.funcs.check(client, msg, command)) { + radio.connection.dispatcher.destroy(); + radio.voiceChannel.leave(); + client.radio.delete(msg.guild.id); + msg.channel.send('Stopped playback!'); + } } }; \ No newline at end of file diff --git a/events/msg.js b/events/msg.js index dafa2cf..9b23a6f 100644 --- a/events/msg.js +++ b/events/msg.js @@ -9,6 +9,14 @@ module.exports = { const commandName = args[0].toLowerCase(); const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)) || client.commandAliases.get(commandName); if (!command && msg.content !== `${prefix}`) return; - client.funcs.exe(msg, args, client, Discord, prefix, command); + const permissions = msg.channel.permissionsFor(msg.client.user); + if (!permissions.has('EMBED_LINKS')) return msg.channel.send('I cannot send embeds (Embed links).'); + try { + command.uses++; + command.execute(msg, args, client, Discord, prefix, command); + } catch (error) { + msg.reply(`Error!`); + console.error(error); + } } } diff --git a/struct/funcs/check.js b/struct/check.js similarity index 100% rename from struct/funcs/check.js rename to struct/check.js diff --git a/struct/client.js b/struct/client.js index 8a364e5..e4d021f 100644 --- a/struct/client.js +++ b/struct/client.js @@ -15,11 +15,9 @@ module.exports = class extends Client { this.radio = new Map(); this.funcs = {}; this.dispatcher = {}; - this.config = require('../config.js'); + this.config = require('./config.js'); - fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { - this.funcs[filename.slice(0, -3)] = require(`./funcs/${filename}`); - }); + this.funcs.check = require('./check.js'); const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js')); for (const file of commandFiles) { @@ -39,7 +37,7 @@ module.exports = class extends Client { require(`${events}voiceStateUpdate`).execute(this, oldState, newState); }); this.on('error', (error) => { - client.channels.fetch(client.config.debug_channel).send('Error: ' + error); + console.error(error); }); this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); diff --git a/config.js b/struct/config.js similarity index 90% rename from config.js rename to struct/config.js index 3bdef5d..8ff5db0 100644 --- a/config.js +++ b/struct/config.js @@ -1,21 +1,20 @@ require('dotenv/config'); module.exports = { - + //credentials - token: "", - + token: process.env.DISCORD_TOKEN, //support supportGuild: "https://discord.gg/rRA65Mn", devId: [ "493174343484833802", "360363051792203779" ], - + //misc embedColor: "#88aa00", invite: "https://discordapp.com/api/oauth2/authorize?client_id=684109535312609409&permissions=3427328&scope=bot", - + //Settings prefix: "rx>", volume: 5 diff --git a/struct/funcs/exe.js b/struct/funcs/exe.js deleted file mode 100644 index 2013a3e..0000000 --- a/struct/funcs/exe.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = function (msg, args, client, Discord, prefix, command) { - const permissions = msg.channel.permissionsFor(msg.client.user); - if (!permissions.has('EMBED_LINKS')) return msg.channel.send('I cannot send embeds (Embed links).'); - try { - command.uses++; - command.execute(msg, args, client, Discord, prefix, command); - } catch (error) { - msg.reply(`Error!`); - console.error(error); - } -}; diff --git a/struct/funcs/msToTime.js b/struct/funcs/msToTime.js deleted file mode 100644 index 14f3fdd..0000000 --- a/struct/funcs/msToTime.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = function msToTime(duration, format) { - var seconds = Math.floor((duration / 1000) % 60), - minutes = Math.floor((duration / (1000 * 60)) % 60), - hours = Math.floor((duration / (1000 * 60 * 60)) % 24); - days = Math.floor((duration / (1000 * 60 * 60 * 24)) % 24); - - days = (days < 10) ? "0" + days : days; - hours = (hours < 10) ? "0" + hours : hours; - minutes = (minutes < 10) ? "0" + minutes : minutes; - seconds = (seconds < 10) ? "0" + seconds : seconds; - - if (format === "hh:mm:ss") { - return `${hours}:${minutes}:${seconds}`; - } else if (format === "dd:hh:mm:ss") { - return `${days}:${hours}:${minutes}:${seconds}`; - } -} \ No newline at end of file diff --git a/struct/funcs/play.js b/struct/funcs/play.js deleted file mode 100644 index 2df6d7b..0000000 --- a/struct/funcs/play.js +++ /dev/null @@ -1,29 +0,0 @@ -module.exports = async function (guild, client, url) { - - const radio = client.radio.get(guild.id); - - const dispatcher = radio.connection - .play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 }) - .on("finish", () => { - radio.voiceChannel.leave(); - client.radio.delete(guild.id); - return; - }); - - dispatcher.on('start', () => { - dispatcher.player.streamingData.pausedTime = 0; - }); - - dispatcher.on('error', error => { - console.error(error); - radio.voiceChannel.leave(); - client.radio.delete(guild.id); - return radio.textChannel.send('An error has occured while playing radio!'); - }); - - dispatcher.setVolume(radio.volume / 10); - - radio.textChannel.send(`Start playing: ${radio.station.name}`); - radio.playing = true; - -} diff --git a/struct/funcs/searchStation.js b/struct/funcs/searchStation.js deleted file mode 100644 index c222ded..0000000 --- a/struct/funcs/searchStation.js +++ /dev/null @@ -1,38 +0,0 @@ -module.exports = function (key, client) { - if (client.stations === null) return false; - let foundStations = []; - if (!key) return false; - if (key == 'radio') return false; - if (key.startsWith("radio ")) key = key.slice(6); - const probabilityIncrement = 100 / key.split(' ').length / 2; - for (let i = 0; i < key.split(' ').length; i++) { - client.stations.filter(x => x.name.toUpperCase().includes(key.split(' ')[i].toUpperCase()) || x === key).forEach(x => foundStations.push({ station: x, name: x.name, probability: probabilityIncrement })); - } - if (foundStations.length === 0) return false; - for (let i = 0; i < foundStations.length; i++) { - for (let j = 0; j < foundStations.length; j++) { - if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); - } - } - for (let i = 0; i < foundStations.length; i++) { - if (foundStations[i].name.length > key.length) { - foundStations[i].probability -= (foundStations[i].name.split(' ').length - key.split(' ').length) * (probabilityIncrement * 0.5); - } else if (foundStations[i].name.length === key.length) { - foundStations[i].probability += (probabilityIncrement * 0.9); - } - - for (let j = 0; j < key.split(' ').length; j++) { - if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(' ')[j])) { - foundStations[i].probability -= (probabilityIncrement * 0.5); - } - } - } - let highestProbabilityStation; - for (let i = 0; i < foundStations.length; i++) { - if (!highestProbabilityStation || highestProbabilityStation.probability < foundStations[i].probability) highestProbabilityStation = foundStations[i]; - if (highestProbabilityStation && highestProbabilityStation.probability === foundStations[i].probability) { - highestProbabilityStation = foundStations[i].station; - } - } - return highestProbabilityStation; -}; \ No newline at end of file