/* eslint-disable require-jsdoc */ /* eslint-disable max-len */ const fs = require('fs/promises'); const {MessageEmbed} = require('discord.js'); const path = require('path'); const fileExists = require('../util/file-exists'); const fileList = require('../util/file-list'); const axios = require('axios'); const os = require('os'); let SONGS_PATH; if (os.hostname() === 'chomens-kubuntu') { SONGS_PATH = path.join(__dirname, '..', '..', 'nginx-html', 'midis'); } else { SONGS_PATH = path.join(__dirname, '..', 'midis'); } async function play(bot, values, discord, channeldc, selector) { try { const filepath = values.join(' '); const absolutePath = await resolve(filepath); song = bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath)); bot.music.queue.push(song); bot.music.play(song); if (discord) { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Music') .setDescription(`Added ${song.name} to the song queue`); channeldc.send({embeds: [Embed]}); } else { bot.tellraw(selector, [{text: 'Added ', color: 'white'}, {text: song.name, color: 'gold'}, {text: ' to the song queue', color: 'white'}]); } } catch (e) { if (discord) { const Embed = new MessageEmbed() .setColor('#FF0000') .setTitle('Error') .setDescription(`\`\`\`SyntaxError: Invalid file\`\`\``); channeldc.send({embeds: [Embed]}); } else { bot.tellraw(selector, {text: 'SyntaxError: Invalid file', color: 'red'}); } } } async function playUrl(bot, values, discord, channeldc, selector) { try { const url = values.join(' '); const response = await axios.get('https://http-proxy.nongsonchome.repl.co', { params: { uri: url, }, responseType: 'arraybuffer', }); song = bot.music.load(response.data, url); bot.music.queue.push(song); bot.music.play(song); if (discord) { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Music') .setDescription(`Added ${song.name} to the song queue`); channeldc.send({embeds: [Embed]}); } else { bot.tellraw(selector, [{text: 'Added ', color: 'white'}, {text: song.name, color: 'gold'}, {text: ' to the song queue', color: 'white'}]); } } catch (e) { if (discord) { const Embed = new MessageEmbed() .setColor('#FF0000') .setTitle('Error') .setDescription(`\`\`\`SyntaxError: Invalid URL\`\`\``); channeldc.send({embeds: [Embed]}); } else { bot.tellraw(selector, {text: 'SyntaxError: Invalid URL', color: 'red'}); } } } async function resolve(filepath) { if (!path.isAbsolute(filepath) && await fileExists(SONGS_PATH)) { return path.join(SONGS_PATH, filepath); } return filepath; } async function list(bot, discord, channeldc, prefix, selector) { const absolutePath = await resolve(SONGS_PATH); const listed = await fileList(absolutePath); if (discord) { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Songs') .setDescription(listed.join(', ')); channeldc.send({embeds: [Embed]}); return; } let color = 'gold'; const message = []; listed.forEach((value) => { if (color === 'gold') { color = 'yellow'; } else if (color === 'yellow') { color = 'gold'; }; message.push({ text: value + ' ', color, clickEvent: { action: 'suggest_command', value: `${prefix}music play ${value}`, }, hoverEvent: { action: 'show_text', contents: [ {text: 'Name: ', color: 'white'}, {text: value, color: 'gold'}, '\n', {text: 'Click here to suggest the command!', color: 'green'}, ], }}); }); bot.tellraw(selector, message); }; module.exports = { name: 'music', description: 'Plays music', alias: [], trusted: 0, usage: [ ' ', ' ', '', ' ', '', '', '', '', ], execute: function(bot, username, usernameraw, sender, prefix, args, config, hash, ownerhash, selector) { switch (args[0]) { case 'play': play(bot, args.slice(1), false, null, selector); break; case 'playurl': playUrl(bot, args.slice(1), false, null, selector); break; case 'stop': try { bot.tellraw(selector, {text: 'Cleared the song queue'}); } catch (e) { return; } bot.music.stop(); break; case 'skip': try { bot.tellraw(selector, [{text: 'Skipping '}, {text: bot.music.song.name, color: 'gold'}]); bot.music.skip(); } catch (e) { throw new Error('No music is currently playing!'); } break; case 'loop': switch (args[1]) { case 'off': bot.music.loop = 0; bot.tellraw(selector, [ { text: 'Looping is now ', }, { text: 'disabled', color: 'red', }, ]); break; case 'current': bot.music.loop = 1; bot.tellraw(selector, [ { text: 'Now Looping ', }, { text: song.name, color: 'gold', }, ]); break; case 'all': bot.music.loop = 2; bot.tellraw(selector, { text: 'Now looping every song in the queue', }); break; default: throw new SyntaxError('Invalid argument'); } break; case 'list': list(bot, false, null, prefix, selector); break; case 'nowplaying': bot.tellraw(selector, [ { text: 'Now playing ', }, { text: bot.music.song.name, color: 'gold', }, ]); break; case 'queue': const queueWithName = []; bot.music.queue.forEach((song) => queueWithName.push(song.name)); bot.tellraw(selector, [ { text: 'Queue: ', color: 'green', }, { text: queueWithName.join(', '), color: 'aqua', }, ]); break; default: throw new SyntaxError('Invalid argument'); } }, discordExecute: function(bot, username, usernameraw, sender, prefix, args, channeldc, message) { let Embed; switch (args[0]) { case 'play': play(bot, args.slice(1), true, channeldc); break; case 'playurl': playUrl(bot, args.slice(1), true, channeldc); break; case 'stop': try { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Stop') .setDescription('Cleared the song queue'); channeldc.send({embeds: [Embed]}); } catch (e) { return; } bot.music.stop(); break; case 'skip': try { const Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Skip') .setDescription(`Skipping ${bot.music.song.name}`); channeldc.send({embeds: [Embed]}); bot.music.skip(); } catch (e) { throw new Error('No music is currently playing!'); } break; case 'loop': switch (args[1]) { case 'off': bot.music.loop = 0; Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Loop') .setDescription('Looping is now disabled'); channeldc.send({embeds: [Embed]}); break; case 'current': bot.music.loop = 1; Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Loop') .setDescription(`Now looping ${song.name}`); channeldc.send({embeds: [Embed]}); break; case 'all': bot.music.loop = 2; Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Loop') .setDescription('Now looping every song in the queue'); channeldc.send({embeds: [Embed]}); break; } break; case 'list': list(bot, true, channeldc, prefix); break; case 'nowplaying': Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Now playing') .setDescription(`Now playing ${bot.music.song.name}`); channeldc.send({embeds: [Embed]}); break; case 'queue': const queueWithName = []; bot.music.queue.forEach((song) => queueWithName.push(song.name)); Embed = new MessageEmbed() .setColor('#FFFF00') .setTitle('Queue') .setDescription(queueWithName.join(', ')); channeldc.send({embeds: [Embed]}); break; default: throw new SyntaxError('Invalid argument'); } }, };