commit a96305e80712c40fcbc8453880cccda1d2430417 Author: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Mon Mar 2 21:38:42 2020 +0200 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1dd9b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +.env +.vscode/ +package-lock.json \ No newline at end of file diff --git a/commands/bug.js b/commands/bug.js new file mode 100644 index 0000000..16397f9 --- /dev/null +++ b/commands/bug.js @@ -0,0 +1,16 @@ +module.exports = { + name: 'bug', + alias: 'none', + usage: '', + description: 'Report a bug', + onlyDev: false, + permission: 'none', + category: 'info', + async execute(msg, args, client, Discord, prefix) { + const embed = new Discord.MessageEmbed() + .setTitle(`Found a bug with ${client.user.username}?\nDM the core developer:`) + .setDescription(`Matte#0002\nOr join the support server: https://discord.gg/rvHuJtB`) + .setColor(client.config.embedColor); + msg.channel.send(embed); + }, +}; \ No newline at end of file diff --git a/commands/cmduses.js b/commands/cmduses.js new file mode 100644 index 0000000..7e350f3 --- /dev/null +++ b/commands/cmduses.js @@ -0,0 +1,31 @@ +module.exports = { + name: 'cmduses', + alias: 'none', + usage: '', + description: 'list all commands and how many times they\'ve been used', + onlyDev: true, + permission: 'dev', + category: 'info', + async execute(msg, args, client, Discord) { + const cmduses = []; + client.commands.forEach((value, key) => { + cmduses.push([key, value.uses]); + }); + cmduses.sort((a, b) => { + return b[1] - a[1]; + }); + const cmdnamelength = Math.max(...cmduses.map(x => x[0].length)) + 4; + const numberlength = Math.max(...cmduses.map(x => x[1].toString().length), 4); + const markdownrows = ['Command' + ' '.repeat(cmdnamelength - 'command'.length) + ' '.repeat(numberlength - 'uses'.length) + 'Uses']; + cmduses.forEach(x => { + if (x[1] > 0) markdownrows.push(x[0] + '.'.repeat(cmdnamelength - x[0].length) + ' '.repeat(numberlength - x[1].toString().length) + x[1].toString()); + }); + const embed = new Discord.MessageEmbed(); + embed + .setTitle('Musix Command Usage During Current Uptime') + .setDescription('```ml\n' + markdownrows.join('\n') + '\n```') + .setFooter('These statistics are from the current uptime.') + .setColor(client.config.embedColor); + msg.channel.send(embed); + }, +}; \ No newline at end of file diff --git a/commands/eval.js b/commands/eval.js new file mode 100644 index 0000000..53d41b6 --- /dev/null +++ b/commands/eval.js @@ -0,0 +1,24 @@ +module.exports = { + name: 'eval', + alias: 'e', + usage: '', + description: 'Evaluation command. DEV ONLY!', + onlyDev: true, + permission: 'dev', + category: 'util', + async execute(msg, args, client, Discord, prefix) { + const radio = client.radio.get(msg.guild.id); + const input = msg.content.slice(prefix.length + 4); + let output; + try { + output = await eval(input); + } catch (error) { + output = error.toString(); + } + const embed = new Discord.MessageEmbed() + .setTitle('Evaluation Command') + .setColor(client.config.embedColor) + .setDescription(`Input: \`\`\`js\n${input.replace(/; /g, ';').replace(/;/g, ';\n')}\n\`\`\`\nOutput: \`\`\`\n${output}\n\`\`\``); + return msg.channel.send(embed); + }, +}; diff --git a/commands/help.js b/commands/help.js new file mode 100644 index 0000000..93a2d86 --- /dev/null +++ b/commands/help.js @@ -0,0 +1,36 @@ +module.exports = { + name: 'help', + alias: 'h', + usage: '', + description: 'See the help for RadioX.', + onlyDev: false, + permission: 'none', + category: 'info', + execute(msg, args, client, Discord, prefix, command) { + if (args[1]) { + if (!client.commands.has(args[1]) || (client.commands.has(args[1]) && client.commands.get(args[1]).omitFromHelp === true && msg.guild.id !== '489083836240494593')) return msg.channel.send('That command does not exist'); + const command = client.commands.get(args[1]); + const embed = new Discord.MessageEmbed() + .setTitle(`${client.global.db.guilds[msg.guild.id].prefix}${command.name} ${command.usage}`) + .setDescription(command.description) + .setFooter(`Command Alias: \`${command.alias}\``) + .setColor(client.config.embedColor) + msg.channel.send(embed); + } else { + const categories = []; + for (let i = 0; i < client.commands.size; i++) { + if (!categories.includes(client.commands.array()[i].category)) categories.push(client.commands.array()[i].category); + } + let commands = ''; + for (let i = 0; i < categories.length; i++) { + commands += `**» ${categories[i].toUpperCase()}**\n${client.commands.filter(x => x.category === categories[i] && !x.omitFromHelp && !x.onlyDev).map(x => `\`${x.name}\``).join(', ')}\n`; + } + const embed = new Discord.MessageEmbed() + .setTitle(`${client.user.username} help:`) + .setDescription(commands) + .setFooter(`"${client.config.prefix}help " to see more information about a command.`) + .setColor(client.config.embedColor) + msg.channel.send(embed); + } + } +}; diff --git a/commands/invite.js b/commands/invite.js new file mode 100644 index 0000000..52e9c7f --- /dev/null +++ b/commands/invite.js @@ -0,0 +1,16 @@ +module.exports = { + name: 'invite', + alias: 'i', + usage: '', + description: 'Invite RadioX.', + onlyDev: false, + permission: 'none', + category: 'info', + execute(msg, args, client, Discord, prefix) { + const embed = new Discord.MessageEmbed() + .setTitle(`Invite ${client.user.username} to your Discord server!`) + .setURL(client.config.invite) + .setColor(client.config.embedColor) + return msg.channel.send(embed); + } +}; \ No newline at end of file diff --git a/commands/join.js b/commands/join.js new file mode 100644 index 0000000..daa6944 --- /dev/null +++ b/commands/join.js @@ -0,0 +1,24 @@ +module.exports = { + name: 'join', + alias: 'j', + usage: '', + description: 'Make Musix join the channel your channel', + onlyDev: true, + permission: 'none', + category: 'util', + async execute(msg, args, client, Discord, prefix) { + try { + const radio = client.radio.get(msg.guild.id); + const voiceChannel = msg.member.voice.channel; + const connection = await voiceChannel.join(); + if (radio) { + radio.connection = connection; + } + msg.channel.send(`<:green_check_mark:674265384777416705> Joined ${voiceChannel.name}!`); + } catch (error) { + client.radio.delete(msg.guild.id); + client.channels.fetch(client.config.debug_channel).send("Error with connecting to voice channel: " + error); + return msg.channel.send(`<:redx:674263474704220182> An error occured: ${error}`); + } + } +}; \ No newline at end of file diff --git a/commands/nowplaying.js b/commands/nowplaying.js new file mode 100644 index 0000000..36991c8 --- /dev/null +++ b/commands/nowplaying.js @@ -0,0 +1,25 @@ +module.exports = { + name: 'nowplaying', + alias: 'np', + usage: '', + description: 'See the currently playing song position and length.', + onlyDev: false, + permission: 'none', + category: 'music', + async execute(msg, args, client, Discord, prefix) { + const radio = client.radio.get(msg.guild.id); + if (!radio) return msg.channel.send('<:redx:674263474704220182> There is nothing playing.'); + if (!radio.playing) return msg.channel.send('<:redx:674263474704220182> There is nothing playing.'); + radio.time = radio.connection.dispatcher.streamTime; + let completed = (radio.time.toFixed(0)); + const embed = new Discord.MessageEmbed() + .setTitle("__Now playing__") + .setDescription(`**Now playing:** ${radio.url}\n\`${client.funcs.msToTime(completed, "hh:mm:ss")}\``) + .setFooter(`Queued by ${radio.songs[0].author.tag}`) + .setURL(radio.songs[0].url) + .setThumbnail(thumbnail._rejectionHandler0) + .setColor(client.config.embedColor) + return msg.channel.send(embed); + } +}; + diff --git a/commands/pause.js b/commands/pause.js new file mode 100644 index 0000000..5b8edb9 --- /dev/null +++ b/commands/pause.js @@ -0,0 +1,18 @@ +module.exports = { + name: 'pause', + alias: 'none', + usage: '', + description: 'Pause the currently playing music.', + onlyDev: false, + permission: 'MANAGE_MESSAGES', + category: 'music', + execute(msg, args, client, Discord, prefix, command) { + const radio = client.radio.get(msg.guild.id); + if (client.funcs.check(client, msg, command)) { + if (radio.paused) return msg.channel.send('<:redx:674263474704220182> The music is already paused!'); + radio.paused = true; + radio.connection.dispatcher.pause(true); + return msg.channel.send('<:pause:674685548610322462> Paused the music!'); + } + } +}; diff --git a/commands/play.js b/commands/play.js new file mode 100644 index 0000000..953a335 --- /dev/null +++ b/commands/play.js @@ -0,0 +1,29 @@ +module.exports = { + name: 'play', + alias: 'p', + usage: '', + description: 'Play some music.', + onlyDev: false, + permission: 'none', + category: 'music', + async execute(msg, args, client, Discord, prefix) { + const searchString = args.slice(1).join(" "); + const url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : ""; + const radio = client.radio.get(msg.guild.id); + const voiceChannel = msg.member.voice.channel; + if (!radio) { + if (!msg.member.voice.channel) return msg.channel.send('<:redx:674263474704220182> I\'m sorry but you need to be in a voice channel to play music!'); + } else { + if (voiceChannel !== radio.voiceChannel) return msg.channel.send('<:redx:674263474704220182> I\'m sorry but you need to be in the same voice channel as Musix to play music!'); + } + if (!args[1]) return msg.channel.send('<:redx:674263474704220182> You need to use a link or search for a song!'); + const permissions = voiceChannel.permissionsFor(msg.client.user); + if (!permissions.has('CONNECT')) { + return msg.channel.send('<:redx:674263474704220182> I cannot connect to your voice channel, make sure I have the proper permissions!'); + } + if (!permissions.has('SPEAK')) { + return msg.channel.send('<:redx:674263474704220182> I cannot speak in your voice channel, make sure I have the proper permissions!'); + } + return client.funcs.handleRadio(msg, voiceChannel, client, url); + } +}; diff --git a/commands/reload.js b/commands/reload.js new file mode 100644 index 0000000..59baca5 --- /dev/null +++ b/commands/reload.js @@ -0,0 +1,30 @@ +const fs = require('fs'); +const path = require('path') + +module.exports = { + name: 'reload', + alias: 'none', + usage: '', + description: 'Reload all files', + onlyDev: true, + permission: 'none', + category: 'util', + async execute(msg, args, client, Discord, prefix, command) { + const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js')); + for (const file of commandFiles) { + const command = require(`./${file}`); + command.uses = 0; + client.commands.set(command.name, command); + client.commandAliases.set(command.alias, command); + } + const settingFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands/settings')).filter(f => f.endsWith('.js')); + for (const file of settingFiles) { + const option = require(`./settings/${file}`); + client.settingCmd.set(option.name, option); + } + /*fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { + this.funcs[filename.slice(0, -3)] = require(`../struct/funcs/${filename}`); + });*/ + msg.channel.send('All files reloaded!'); + } +}; diff --git a/commands/restart.js b/commands/restart.js new file mode 100644 index 0000000..148b9cf --- /dev/null +++ b/commands/restart.js @@ -0,0 +1,14 @@ +module.exports = { + name: 'restart', + alias: 'none', + usage: '', + description: 'Restart the bot', + onlyDev: true, + permission: 'none', + category: 'util', + async execute(msg, args, client, Discord, prefix, command) { + client.destroy(); + require('../index.js'); + msg.channel.send('restarted!'); + } +}; diff --git a/commands/resume.js b/commands/resume.js new file mode 100644 index 0000000..512a2c2 --- /dev/null +++ b/commands/resume.js @@ -0,0 +1,18 @@ +module.exports = { + name: 'resume', + alias: 'none', + usage: '', + description: 'Resume the paused music.', + onlyDev: false, + permission: 'MANAGE_MESSAGES', + category: 'music', + execute(msg, args, client, Discord, prefix, command) { + const radio = client.radio.get(msg.guild.id); + if (client.funcs.check(client, msg, command)) { + if (!radio.paused) return msg.channel.send('<:redx:674263474704220182> The music in not paused!'); + radio.paused = false; + radio.connection.dispatcher.resume(true); + return msg.channel.send('<:resume:674685585478254603> Resumed the music!'); + } + } +}; diff --git a/commands/stop.js b/commands/stop.js new file mode 100644 index 0000000..1b14540 --- /dev/null +++ b/commands/stop.js @@ -0,0 +1,18 @@ +module.exports = { + name: 'stop', + description: 'Stop command.', + alias: 'none', + usage: '', + onlyDev: false, + permission: 'MANAGE_CHANNELS', + category: 'music', + execute(msg, args, client, Discord, prefix, command) { + const radio = client.radio.get(msg.guild.id); + if (client.funcs.check(client, msg, command)) { + radio.songs = []; + radio.looping = false; + radio.connection.dispatcher.end('Stopped'); + msg.channel.send('<:stop:674685626108477519> Stopped the music!') + } + } +}; diff --git a/commands/volume.js b/commands/volume.js new file mode 100644 index 0000000..7fa73e1 --- /dev/null +++ b/commands/volume.js @@ -0,0 +1,23 @@ +module.exports = { + name: 'volume', + description: 'Volume command.', + alias: 'none', + usage: '', + cooldown: 5, + onlyDev: false, + permission: 'MANAGE_MESSAGES', + category: 'music', + execute(msg, args, client, Discord, prefix, command) { + const radio = client.radio.get(msg.guild.id); + if (!args[1] && radio) return msg.channel.send(`:loud_sound: The current volume is: **${radio.volume}**`); + const volume = parseFloat(args[1]); + if (client.funcs.check(client, msg, command)) { + if (isNaN(volume)) return msg.channel.send('<:redx:674263474704220182> I\'m sorry, But you need to enter a valid __number__.'); + if (volume > 100) return msg.channel.send('<:redx:674263474704220182> The max volume is `100`!'); + if (volume < 0) return msg.channel.send('<:redx:674263474704220182> The volume needs to be a positive number!'); + radio.volume = volume; + radio.connection.dispatcher.setVolume(volume / 5); + return msg.channel.send(`<:volumehigh:674685637626167307> I set the volume to: **${volume}**`); + } + } +}; diff --git a/events/dispatcher/finish.js b/events/dispatcher/finish.js new file mode 100644 index 0000000..ff59c68 --- /dev/null +++ b/events/dispatcher/finish.js @@ -0,0 +1,20 @@ +module.exports = async function (client, reason, guild) { + const radio = client.radio.get(guild.id); + radio.playing = false; + if (reason === "Stream is not generating quickly enough.") { + console.log("Song ended"); + } else if (reason === "seek") { + return; + } else { + console.log(reason); + } + if (!radio.songLooping) { + if (radio.looping) { + radio.songs.push(radio.songs[0]); + } + radio.votes = 0; + radio.voters = []; + radio.songs.shift(); + } + client.funcs.play(guild, radio.songs[0], client, 0, true); +}; \ No newline at end of file diff --git a/events/msg.js b/events/msg.js new file mode 100644 index 0000000..1240650 --- /dev/null +++ b/events/msg.js @@ -0,0 +1,27 @@ +module.exports = { + name: 'message', + async execute(client, msg, Discord) { + if (msg.author.bot || !msg.guild) return; + let prefix = client.config.prefix + if (client.config.devMode) prefix = client.config.devPrefix; + const args = msg.content.slice(prefix.length).split(' '); + if (msg.mentions.users.first()) { + if (msg.mentions.users.first().id === client.user.id) { + if (!args[1]) return; + if (args[1] === 'prefix') return msg.channel.send(`My prefix here is: \`${prefix}\`.`); + if (args[1] === 'help') { + const command = client.commands.get("help"); + return client.funcs.exe(msg, args, client, Discord, prefix, command); + } + } + } + if (!msg.content.startsWith(prefix)) return; + if (!args[0]) return; + 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; + if (command.onlyDev && msg.author.id !== client.config.devId) return msg.channel.send('<:redx:674263474704220182> You are not allowed to do that!'); + if (client.config.devMode && msg.member.id !== client.config.devId) return msg.channel.send('<:redx:674263474704220182> Dev mode has been turned on! Commands are only available to developer(s)!'); + client.funcs.exe(msg, args, client, Discord, prefix, command); + } +} diff --git a/events/ready.js b/events/ready.js new file mode 100644 index 0000000..bfe058c --- /dev/null +++ b/events/ready.js @@ -0,0 +1,14 @@ +module.exports = { + name: 'ready', + async execute(client, Discord) { + if (client.config.devMode) { + console.log('dev mode'); + } + client.user.setActivity(`@${client.user.username} help | 🎶`, { type: 'LISTENING' }); + client.user.setStatus('online'); + console.log('- Activated -'); + setInterval(() => { + client.funcs.ffmpeg(client, Discord); + }, 7200000); + } +} diff --git a/events/voiceStateUpdate.js b/events/voiceStateUpdate.js new file mode 100644 index 0000000..57a5fa5 --- /dev/null +++ b/events/voiceStateUpdate.js @@ -0,0 +1,13 @@ +module.exports = { + name: 'voiceStateUpdate', + async execute(client, newMember) { + const serverQueue = client.radio.get(newMember.guild.id); + if (!serverQueue) return; + if (newMember === client.user) { + if (newMember.voice.channel !== serverQueue.voiceChannel) { + serverQueue.voiceChannel = newMember.voice.channel; + console.log(`Changed serverQueue voiceChannel since Musix was moved to a different channel!`); + } + } + } +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..30dfbd5 --- /dev/null +++ b/index.js @@ -0,0 +1,2 @@ +const radioClient = require("./struct/client.js"); +const client = new radioClient(); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f375dac --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "radiox", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "discord.js": "^12.0.1", + "dotenv": "^8.2.0", + "fs": "0.0.1-security", + "node-opus": "^0.3.3", + "path": "^0.12.7" + } +} diff --git a/struct/client.js b/struct/client.js new file mode 100644 index 0000000..246678c --- /dev/null +++ b/struct/client.js @@ -0,0 +1,52 @@ +const { Client, Collection } = require('discord.js'); +const Discord = require('discord.js'); +const fs = require('fs'); +const path = require('path') +const events = '../events/'; + +module.exports = class extends Client { + constructor() { + super({ + disableEveryone: true, + disabledEvents: ['TYPING_START'] + }); + this.commands = new Collection(); + this.commandAliases = new Collection(); + this.radio = new Map(); + this.funcs = {}; + this.dispatcher = {}; + this.config = require('./config/config.js'); + this.dispatcher.finish = require('../events/dispatcher/finish.js'); + + fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { + this.funcs[filename.slice(0, -3)] = require(`./funcs/${filename}`); + }); + + const commandFiles = fs.readdirSync(path.join(path.dirname(__dirname), 'commands')).filter(f => f.endsWith('.js')); + for (const file of commandFiles) { + const command = require(`../commands/${file}`); + command.uses = 0; + this.commands.set(command.name, command); + this.commandAliases.set(command.alias, command); + } + + if (this.config.devMode) { + this.config.token = this.config.devToken; + } + + this.on('ready', () => { + require(`${events}ready`).execute(this, Discord); + }); + this.on('message', (msg) => { + require(`${events}msg`).execute(this, msg, Discord); + }); + this.on('voiceStateUpdate', (newMember) => { + require(`${events}voiceStateUpdate`).execute(this, newMember); + }); + this.on('error', (error) => { + client.channels.fetch(client.config.debug_channel).send('Error: ' + error); + }); + + this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); + } +}; diff --git a/struct/config/config.js b/struct/config/config.js new file mode 100644 index 0000000..d42daea --- /dev/null +++ b/struct/config/config.js @@ -0,0 +1,20 @@ +require('dotenv/config'); + +module.exports = { + //credentials + token: process.env.TOKEN, + devToken: process.env.DEVTOKEN, + //channels + debug_channel: "634718645188034560", + primary_test_channel: "617633098296721409", + secondary_test_channel: "570531724002328577", + devId: "360363051792203779", + //misc + embedColor: "", + invite: "", + //Settings + devMode: false, + prefix: "?", + devPrefix: "-", + volume: 5, +} diff --git a/struct/funcs/check.js b/struct/funcs/check.js new file mode 100644 index 0000000..44a9821 --- /dev/null +++ b/struct/funcs/check.js @@ -0,0 +1,12 @@ +module.exports = function (client, msg, command) { + const radio = client.radio.get(msg.guild.id); + const permissions = msg.channel.permissionsFor(msg.author); + if (!radio || !radio.playing) return msg.channel.send('<:redx:674263474704220182> There is nothing playing!'); + if (msg.author.id !== client.config.devId) { + if (msg.member.voice.channel !== radio.voiceChannel) return msg.channel.send(`<:redx:674263474704220182> I'm sorry but you need to be in the same voice channel as RadioX to use this command!`); + if (!permissions.has(command.permission)) { + msg.channel.send(`<:redx:674263474704220182> You need the \`${command.permission}\` permission to use this command!`); + return false; + } else return true; + } else return true; +}; diff --git a/struct/funcs/exe.js b/struct/funcs/exe.js new file mode 100644 index 0000000..820e43c --- /dev/null +++ b/struct/funcs/exe.js @@ -0,0 +1,17 @@ +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('<:redx:674263474704220182> I cannot send embeds (Embed links), make sure I have the proper permissions!'); + //if (!permissions.has('EXTERNAL_EMOJIS')) return msg.channel.send('<:redx:674263474704220182> I cannot use external emojis, make sure I have the proper permissions!'); DEPRACATED! + try { + command.uses++; + command.execute(msg, args, client, Discord, prefix, command); + } catch (error) { + msg.reply(`<:redx:674263474704220182> there was an error trying to execute that command! Please contact support with \`${prefix}bug\`!`); + const embed = new Discord.MessageEmbed() + .setTitle(`Musix ${error.toString()}`) + .setDescription(error.stack.replace(/at /g, '**at **')) + .setColor('#b50002'); + //client.channels.fetch(client.config.debug_channel).send(embed); + console.error(error); + } +}; diff --git a/struct/funcs/ffmpeg.js b/struct/funcs/ffmpeg.js new file mode 100644 index 0000000..4577f3c --- /dev/null +++ b/struct/funcs/ffmpeg.js @@ -0,0 +1,8 @@ +module.exports = async function (client) { + try { + await client.channels.fetch(client.config.secondary_test_channel) + .then(x => x.join()); + } catch (error) { + client.channels.fetch(client.config.debug_channel).send("Error detected: " + error); + } +}; \ No newline at end of file diff --git a/struct/funcs/handleRadio.js b/struct/funcs/handleRadio.js new file mode 100644 index 0000000..4a32697 --- /dev/null +++ b/struct/funcs/handleRadio.js @@ -0,0 +1,31 @@ +module.exports = async function (msg, voiceChannel, client, url) { + const radio = client.radio.get(msg.guild.id); + + if (radio) { + radio.songs.push(song); + if (playlist) return; + return msg.channel.send(`<:green_check_mark:674265384777416705> **${song.title}** has been added to the queue!`); + } + + const construct = { + textChannel: msg.channel, + voiceChannel: voiceChannel, + connection: null, + playing: false, + url: url, + name: null, + volume: client.config.volume, + }; + client.radio.set(msg.guild.id, construct); + + try { + const connection = await voiceChannel.join(); + construct.connection = connection; + client.funcs.play(msg.guild, client, url); + } catch (error) { + client.radio.delete(msg.guild.id); + //client.channels.fetch(client.config.debug_channel).send("Error with connecting to voice channel: " + error); + return msg.channel.send(`<:redx:674263474704220182> An error occured: ${error}`); + } + return; +} diff --git a/struct/funcs/msToTime.js b/struct/funcs/msToTime.js new file mode 100644 index 0000000..3247cb7 --- /dev/null +++ b/struct/funcs/msToTime.js @@ -0,0 +1,17 @@ +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}`; + } +} diff --git a/struct/funcs/play.js b/struct/funcs/play.js new file mode 100644 index 0000000..4cc448e --- /dev/null +++ b/struct/funcs/play.js @@ -0,0 +1,22 @@ +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", reason => { + client.dispatcher.finish(client, reason, guild); + }); + dispatcher.on('start', () => { + dispatcher.player.streamingData.pausedTime = 0; + }); + dispatcher.on('error', error => { + console.error(error); + client.channels.fetch(client.config.debug_channel).send('Error with the dispatcher: ' + error); + radio.voiceChannel.leave(); + client.radio.delete(guild.id); + return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!'); + }); + dispatcher.setVolume(radio.volume / 10); + radio.textChannel.send('Start playing'); + radio.playing = true; +}