chomens-bot-js/commands/music.js

338 lines
9.9 KiB
JavaScript
Raw Normal View History

2022-11-16 06:41:30 -05:00
/* eslint-disable require-jsdoc */
2022-08-14 05:51:45 -04:00
/* eslint-disable max-len */
const fs = require('fs/promises');
2022-10-20 00:41:25 -04:00
const {MessageEmbed} = require('discord.js');
2022-08-14 05:51:45 -04:00
const path = require('path');
const fileExists = require('../util/file-exists');
2022-10-20 00:41:25 -04:00
const fileList = require('../util/file-list');
2022-08-14 05:51:45 -04:00
const axios = require('axios');
const os = require('os');
let SONGS_PATH;
2022-11-07 07:26:38 -05:00
if (os.hostname() === 'chomens-kubuntu') {
2022-08-14 05:51:45 -04:00
SONGS_PATH = path.join(__dirname, '..', '..', 'nginx-html', 'midis');
} else {
SONGS_PATH = path.join(__dirname, '..', 'midis');
}
2022-11-16 06:41:30 -05:00
async function play(bot, values, discord, channeldc, selector) {
2022-10-20 00:41:25 -04:00
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);
2022-10-20 00:41:25 -04:00
bot.music.play(song);
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Music')
.setDescription(`Added ${song.name} to the song queue`);
2022-10-20 00:41:25 -04:00
channeldc.send({embeds: [Embed]});
} else {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [{text: 'Added ', color: 'white'}, {text: song.name, color: 'gold'}, {text: ' to the song queue', color: 'white'}]);
2022-08-14 05:51:45 -04:00
}
2022-10-20 00:41:25 -04:00
} catch (e) {
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`SyntaxError: Invalid file\`\`\``);
channeldc.send({embeds: [Embed]});
} else {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, {text: 'SyntaxError: Invalid file', color: 'red'});
2022-08-14 05:51:45 -04:00
}
2022-10-20 00:41:25 -04:00
}
}
2022-08-14 05:51:45 -04:00
2022-11-16 06:41:30 -05:00
async function playUrl(bot, values, discord, channeldc, selector) {
2022-10-20 00:41:25 -04:00
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);
2022-10-20 00:41:25 -04:00
bot.music.play(song);
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Music')
.setDescription(`Added ${song.name} to the song queue`);
2022-10-20 00:41:25 -04:00
channeldc.send({embeds: [Embed]});
} else {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [{text: 'Added ', color: 'white'}, {text: song.name, color: 'gold'}, {text: ' to the song queue', color: 'white'}]);
2022-10-20 00:41:25 -04:00
}
} catch (e) {
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`SyntaxError: Invalid URL\`\`\``);
channeldc.send({embeds: [Embed]});
} else {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, {text: 'SyntaxError: Invalid URL', color: 'red'});
2022-08-14 05:51:45 -04:00
}
2022-10-20 00:41:25 -04:00
}
}
2022-08-14 05:51:45 -04:00
2022-10-20 00:41:25 -04:00
async function resolve(filepath) {
if (!path.isAbsolute(filepath) && await fileExists(SONGS_PATH)) {
return path.join(SONGS_PATH, filepath);
}
return filepath;
}
2022-11-18 05:10:34 -05:00
async function list(bot, discord, channeldc, prefix, selector, args) {
try {
let absolutePath;
if (args[1]) absolutePath = await resolve(path.join(SONGS_PATH, args.slice(1).join(' ')));
else absolutePath = await resolve(SONGS_PATH);
2022-08-14 05:51:45 -04:00
2022-11-18 05:10:34 -05:00
if (!absolutePath.includes('midis')) throw new Error('bro trying to hack my server?!/1?!');
const listed = await fileList(absolutePath);
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Songs')
.setDescription(listed.join(', '));
channeldc.send({embeds: [Embed]});
return;
}
2022-08-17 09:39:25 -04:00
2022-11-18 05:10:34 -05:00
let primary = true;
const message = [];
2022-08-17 09:39:25 -04:00
2022-11-18 05:10:34 -05:00
for (const value of listed) {
const isFile = (await fs.lstat(path.join(absolutePath, value))).isFile();
message.push({
text: value + ' ',
color: (!((primary = !primary)) ? 'gold' : 'yellow'),
clickEvent: {
action: 'suggest_command',
value: `${prefix}music ${isFile ? 'play' : 'list'} ${path.join(args.slice(1).join(' '), value)}`,
},
hoverEvent: {
action: 'show_text',
contents: [
{text: 'Name: ', color: 'white'},
{text: value, color: 'gold'},
'\n',
{text: 'Click here to suggest the command!', color: 'green'},
],
},
});
2022-08-14 05:51:45 -04:00
};
2022-10-20 00:41:25 -04:00
2022-11-18 05:10:34 -05:00
bot.tellraw(selector, message);
} catch (e) {
2022-11-18 05:52:17 -05:00
if (discord) {
const Embed = new MessageEmbed()
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`${e.toString()}\`\`\``);
channeldc.send({embeds: [Embed]});
} else {
bot.tellraw(selector, {text: e.toString(), color: 'red'});
}
2022-11-18 05:10:34 -05:00
}
2022-10-20 00:41:25 -04:00
};
2022-08-14 05:51:45 -04:00
2022-10-20 00:41:25 -04:00
module.exports = {
name: 'music',
description: 'Plays music',
alias: [],
trusted: 0,
usage: [
'<play> <song>',
'<playurl> <url>',
'<stop>',
'<loop> <all|current|off>',
2022-11-18 05:10:34 -05:00
'<list> [directory]',
'<skip>',
'<nowplaying>',
'<queue>',
],
2022-11-16 06:41:30 -05:00
execute: function(bot, username, usernameraw, sender, prefix, args, config, hash, ownerhash, selector) {
2022-11-09 07:48:54 -05:00
switch (args[0]) {
case 'play':
2022-11-16 06:41:30 -05:00
play(bot, args.slice(1), false, null, selector);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'playurl':
2022-11-16 06:41:30 -05:00
playUrl(bot, args.slice(1), false, null, selector);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'stop':
try {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, {text: 'Cleared the song queue'});
2022-11-09 07:48:54 -05:00
} catch (e) {
return;
}
bot.music.stop();
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'skip':
try {
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [{text: 'Skipping '}, {text: bot.music.song.name, color: 'gold'}]);
2022-11-09 07:48:54 -05:00
bot.music.skip();
} catch (e) {
throw new Error('No music is currently playing!');
}
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'loop':
switch (args[1]) {
case 'off':
bot.music.loop = 0;
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
text: 'Looping is now ',
},
{
text: 'disabled',
color: 'red',
},
]);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'current':
bot.music.loop = 1;
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
text: 'Now Looping ',
},
{
text: song.name,
color: 'gold',
},
]);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'all':
bot.music.loop = 2;
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, {
2022-11-09 07:48:54 -05:00
text: 'Now looping every song in the queue',
});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
default:
throw new SyntaxError('Invalid argument');
}
2022-11-10 05:35:51 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'list':
2022-11-18 05:10:34 -05:00
list(bot, false, null, prefix, selector, args);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'nowplaying':
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
text: 'Now playing ',
},
{
text: bot.music.song.name,
color: 'gold',
},
]);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'queue':
const queueWithName = [];
bot.music.queue.forEach((song) => queueWithName.push(song.name));
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
text: 'Queue: ',
color: 'green',
},
{
text: queueWithName.join(', '),
color: 'aqua',
},
]);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
default:
2022-11-07 08:08:29 -05:00
throw new SyntaxError('Invalid argument');
2022-08-14 05:51:45 -04:00
}
},
2022-10-20 00:41:25 -04:00
discordExecute: function(bot, username, usernameraw, sender, prefix, args, channeldc, message) {
2022-11-09 08:02:20 -05:00
let Embed;
2022-11-09 07:48:54 -05:00
switch (args[0]) {
case 'play':
play(bot, args.slice(1), true, channeldc);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'playurl':
playUrl(bot, args.slice(1), true, channeldc);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
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();
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
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!');
}
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
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]});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'current':
bot.music.loop = 1;
Embed = new MessageEmbed()
.setColor('#FFFF00')
.setTitle('Loop')
.setDescription(`Now looping ${song.name}`);
channeldc.send({embeds: [Embed]});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
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]});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
}
2022-11-10 05:35:51 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'list':
2022-11-18 05:52:17 -05:00
list(bot, true, channeldc, prefix, '@a', args);
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'nowplaying':
Embed = new MessageEmbed()
.setColor('#FFFF00')
2022-11-09 07:48:54 -05:00
.setTitle('Now playing')
.setDescription(`Now playing ${bot.music.song.name}`);
channeldc.send({embeds: [Embed]});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
case 'queue':
const queueWithName = [];
bot.music.queue.forEach((song) => queueWithName.push(song.name));
Embed = new MessageEmbed()
.setColor('#FFFF00')
2022-11-09 07:48:54 -05:00
.setTitle('Queue')
.setDescription(queueWithName.join(', '));
channeldc.send({embeds: [Embed]});
2022-11-09 08:02:20 -05:00
break;
2022-11-09 07:48:54 -05:00
default:
throw new SyntaxError('Invalid argument');
2022-10-20 00:41:25 -04:00
}
},
2022-08-14 05:51:45 -04:00
};