chomens-bot-js/commands/music.js

354 lines
10 KiB
JavaScript
Raw Normal View History

2022-11-27 02:35:28 -05:00
/* eslint-disable no-case-declarations */
2022-11-27 02:35:28 -05:00
const fs = require('fs/promises')
const { EmbedBuilder } = require('discord.js')
2022-11-27 02:35:28 -05:00
const path = require('path')
2022-12-30 03:40:17 -05:00
const getFilenameFromUrl = require('../util/getFilenameFromUrl')
2022-11-27 02:35:28 -05:00
const fileExists = require('../util/file-exists')
const fileList = require('../util/file-list')
const axios = require('axios')
const os = require('os')
2022-08-14 05:51:45 -04:00
2022-11-27 02:35:28 -05:00
let SONGS_PATH
2022-08-14 05:51:45 -04:00
2022-11-07 07:26:38 -05:00
if (os.hostname() === 'chomens-kubuntu') {
2022-11-27 02:35:28 -05:00
SONGS_PATH = path.join(__dirname, '..', '..', 'nginx-html', 'midis')
2022-08-14 05:51:45 -04:00
} else {
2022-11-27 02:35:28 -05:00
SONGS_PATH = path.join(__dirname, '..', 'midis')
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
let song
2022-12-01 05:15:30 -05:00
async function play (bot, values, discord, channeldc, selector, config) {
2022-10-20 00:41:25 -04:00
try {
2022-12-20 05:59:45 -05:00
const songs = await fileList(SONGS_PATH)
2022-11-27 02:35:28 -05:00
const filepath = values.join(' ')
2022-12-20 05:59:45 -05:00
2022-12-22 08:34:08 -05:00
const file = songs.filter((song) => song.toLowerCase().includes(filepath.toLowerCase()))[0]
2022-12-20 05:59:45 -05:00
let absolutePath
if (!path.isAbsolute(filepath) &&
2022-12-22 08:11:22 -05:00
!filepath.includes('/') && // please improve, this is so bad
2022-12-20 05:59:45 -05:00
filepath !== '') absolutePath = await resolve(file)
else absolutePath = await resolve(filepath)
2022-12-30 03:40:17 -05:00
song = await bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath))
2023-02-28 02:17:49 -05:00
2022-10-20 00:41:25 -04:00
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Music')
.setDescription(`Added ${song.name} to the song queue`)
channeldc.send({ embeds: [Embed] })
2022-10-20 00:41:25 -04:00
} else {
2022-11-27 02:35:28 -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
}
2023-02-28 02:17:49 -05:00
bot.music.queue.push(song)
bot.music.play(song)
2022-10-20 00:41:25 -04:00
} catch (e) {
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.error)
2022-11-27 02:35:28 -05:00
.setTitle('Error')
.setDescription('```SyntaxError: Invalid file```')
channeldc.send({ embeds: [Embed] })
2022-10-20 00:41:25 -04:00
} else {
2022-11-27 02:35:28 -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-12-01 05:15:30 -05:00
async function playUrl (bot, values, discord, channeldc, selector, config) {
let response
2022-10-20 00:41:25 -04:00
try {
2022-11-27 02:35:28 -05:00
const url = values.join(' ')
response = await axios.get('https://http-proxy.nongsonchome.repl.co', {
2022-10-20 00:41:25 -04:00
params: {
2022-11-27 02:35:28 -05:00
uri: url
2022-10-20 00:41:25 -04:00
},
2022-11-27 02:35:28 -05:00
responseType: 'arraybuffer'
})
2023-02-28 02:17:49 -05:00
2023-02-28 02:21:14 -05:00
song = await bot.music.load(response.data, getFilenameFromUrl(url))
2022-10-20 00:41:25 -04:00
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Music')
.setDescription(`Added ${song.name} to the song queue`)
channeldc.send({ embeds: [Embed] })
2022-10-20 00:41:25 -04:00
} else {
2022-11-27 02:35:28 -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
}
2023-02-28 02:17:49 -05:00
bot.music.queue.push(song)
bot.music.play(song)
} catch (_err) {
2022-12-27 03:45:06 -05:00
const e = _err.toString().includes('Bad MIDI file. Expected \'MHdr\', got: ') ? response.data.toString() : _err
2022-10-20 00:41:25 -04:00
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.error)
2022-11-27 02:35:28 -05:00
.setTitle('Error')
.setDescription(`\`\`\`${e}\`\`\``)
2022-11-27 02:35:28 -05:00
channeldc.send({ embeds: [Embed] })
2022-10-20 00:41:25 -04:00
} else {
bot.tellraw(selector, { text: e, 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-27 02:35:28 -05:00
async function resolve (filepath) {
2022-10-20 00:41:25 -04:00
if (!path.isAbsolute(filepath) && await fileExists(SONGS_PATH)) {
2022-11-27 02:35:28 -05:00
return path.join(SONGS_PATH, filepath)
2022-10-20 00:41:25 -04:00
}
2022-11-27 02:35:28 -05:00
return filepath
2022-10-20 00:41:25 -04:00
}
2022-12-01 05:15:30 -05:00
async function list (bot, discord, channeldc, prefix, selector, args, config) {
2022-11-18 05:10:34 -05:00
try {
2022-11-27 02:35:28 -05:00
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-27 02:35:28 -05:00
if (!absolutePath.includes('midis')) throw new Error('bro trying to hack my server?!/1?!')
2022-11-18 05:10:34 -05:00
2022-11-27 02:35:28 -05:00
const listed = await fileList(absolutePath)
2022-11-18 05:10:34 -05:00
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Songs')
.setDescription(listed.join(', '))
channeldc.send({ embeds: [Embed] })
return
2022-11-18 05:10:34 -05:00
}
2022-08-17 09:39:25 -04:00
2022-11-27 02:35:28 -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) {
2022-11-27 02:35:28 -05:00
const isFile = (await fs.lstat(path.join(absolutePath, value))).isFile()
2022-11-18 05:10:34 -05:00
message.push({
text: value + ' ',
color: (!((primary = !primary)) ? 'gold' : 'yellow'),
clickEvent: {
action: 'suggest_command',
2022-11-27 02:35:28 -05:00
value: `${prefix}music ${isFile ? 'play' : 'list'} ${path.join(args.slice(1).join(' '), value)}`
2022-11-18 05:10:34 -05:00
},
hoverEvent: {
action: 'show_text',
contents: [
2022-11-27 02:35:28 -05:00
{ text: 'Name: ', color: 'white' },
{ text: value, color: 'gold' },
2022-11-18 05:10:34 -05:00
'\n',
2022-11-27 02:35:28 -05:00
{ 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-27 02:35:28 -05:00
bot.tellraw(selector, message)
2022-11-18 05:10:34 -05:00
} catch (e) {
2022-11-18 05:52:17 -05:00
if (discord) {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.error)
2022-11-27 02:35:28 -05:00
.setTitle('Error')
.setDescription(`\`\`\`${e.toString()}\`\`\``)
channeldc.send({ embeds: [Embed] })
2022-11-18 05:52:17 -05:00
} else {
2022-11-27 02:35:28 -05:00
bot.tellraw(selector, { text: e.toString(), color: 'red' })
2022-11-18 05:52:17 -05:00
}
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: [
2023-02-28 02:21:14 -05:00
'play <song|url>',
'stop',
'loop <all|current|off>',
'list [directory]',
'skip',
'nowplaying',
'queue'
],
execute (bot, username, sender, prefix, args, config, hash, ownerhash, selector) {
2022-11-09 07:48:54 -05:00
switch (args[0]) {
case 'play':
2023-02-28 02:21:14 -05:00
case 'playurl': // deprecated
2023-02-28 02:22:33 -05:00
if (args.slice(1).join(' ').startsWith('http')) {
2023-02-28 02:21:14 -05:00
playUrl(bot, args.slice(1), false, null, selector, config)
} else {
play(bot, args.slice(1), false, null, selector, config)
}
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'stop':
2023-02-28 19:43:53 -05:00
bot.tellraw(selector, { text: 'Cleared the song queue' })
2022-11-27 02:35:28 -05:00
bot.music.stop()
break
2022-11-09 07:48:54 -05:00
case 'skip':
try {
2022-11-27 02:35:28 -05:00
bot.tellraw(selector, [{ text: 'Skipping ' }, { text: bot.music.song.name, color: 'gold' }])
bot.music.skip()
2022-11-09 07:48:54 -05:00
} catch (e) {
2022-11-27 02:35:28 -05:00
throw new Error('No music is currently playing!')
2022-11-09 07:48:54 -05:00
}
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'loop':
switch (args[1]) {
case 'off':
2022-11-27 02:35:28 -05:00
bot.music.loop = 0
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
2022-11-27 02:35:28 -05:00
text: 'Looping is now '
2022-11-09 07:48:54 -05:00
},
{
text: 'disabled',
2022-11-27 02:35:28 -05:00
color: 'red'
}
])
break
2022-11-09 07:48:54 -05:00
case 'current':
2022-11-27 02:35:28 -05:00
bot.music.loop = 1
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, [
2022-11-09 07:48:54 -05:00
{
2022-11-27 02:35:28 -05:00
text: 'Now Looping '
2022-11-09 07:48:54 -05:00
},
{
text: song.name,
2022-11-27 02:35:28 -05:00
color: 'gold'
}
])
break
2022-11-09 07:48:54 -05:00
case 'all':
2022-11-27 02:35:28 -05:00
bot.music.loop = 2
2022-11-16 06:41:30 -05:00
bot.tellraw(selector, {
2023-02-28 02:17:49 -05:00
text: 'Now looping every song'
2022-11-27 02:35:28 -05:00
})
break
2022-11-09 07:48:54 -05:00
default:
2022-11-27 02:35:28 -05:00
throw new SyntaxError('Invalid argument')
2022-11-09 07:48:54 -05:00
}
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'list':
2022-12-01 05:15:30 -05:00
list(bot, false, null, prefix, selector, args, config)
2022-11-27 02:35:28 -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
{
2022-11-27 02:35:28 -05:00
text: 'Now playing '
2022-11-09 07:48:54 -05:00
},
{
text: bot.music.song.name,
2022-11-27 02:35:28 -05:00
color: 'gold'
}
])
break
2022-11-09 07:48:54 -05:00
case 'queue':
2022-11-27 02:35:28 -05:00
const queueWithName = []
for (const song of bot.music.queue) 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: ',
2022-11-27 02:35:28 -05:00
color: 'green'
2022-11-09 07:48:54 -05:00
},
{
text: queueWithName.join(', '),
2022-11-27 02:35:28 -05:00
color: 'aqua'
}
])
break
2022-11-09 07:48:54 -05:00
default:
2022-11-27 02:35:28 -05:00
throw new SyntaxError('Invalid argument')
2022-08-14 05:51:45 -04:00
}
},
discordExecute (bot, username, sender, prefix, args, channeldc, message, config) {
2022-11-27 02:35:28 -05:00
let Embed
2022-11-09 07:48:54 -05:00
switch (args[0]) {
case 'play':
2022-12-01 05:15:30 -05:00
play(bot, args.slice(1), true, channeldc, config)
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'playurl':
2022-12-01 05:15:30 -05:00
playUrl(bot, args.slice(1), true, channeldc, config)
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'stop':
try {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Stop')
.setDescription('Cleared the song queue')
channeldc.send({ embeds: [Embed] })
2022-11-09 07:48:54 -05:00
} catch (e) {
2022-11-27 02:35:28 -05:00
return
2022-11-09 07:48:54 -05:00
}
2022-11-27 02:35:28 -05:00
bot.music.stop()
break
2022-11-09 07:48:54 -05:00
case 'skip':
try {
const Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Skip')
.setDescription(`Skipping ${bot.music.song.name}`)
channeldc.send({ embeds: [Embed] })
bot.music.skip()
2022-11-09 07:48:54 -05:00
} catch (e) {
2022-11-27 02:35:28 -05:00
throw new Error('No music is currently playing!')
2022-11-09 07:48:54 -05:00
}
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'loop':
switch (args[1]) {
case 'off':
2022-11-27 02:35:28 -05:00
bot.music.loop = 0
Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Loop')
.setDescription('Looping is now disabled')
channeldc.send({ embeds: [Embed] })
break
2022-11-09 07:48:54 -05:00
case 'current':
2022-11-27 02:35:28 -05:00
bot.music.loop = 1
Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Loop')
.setDescription(`Now looping ${song.name}`)
channeldc.send({ embeds: [Embed] })
break
2022-11-09 07:48:54 -05:00
case 'all':
2022-11-27 02:35:28 -05:00
bot.music.loop = 2
Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Loop')
2023-02-28 02:17:49 -05:00
.setDescription('Now looping every song')
2022-11-27 02:35:28 -05:00
channeldc.send({ embeds: [Embed] })
break
2022-11-09 07:48:54 -05:00
}
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'list':
2022-12-01 05:15:30 -05:00
list(bot, true, channeldc, prefix, '@a', args, config)
2022-11-27 02:35:28 -05:00
break
2022-11-09 07:48:54 -05:00
case 'nowplaying':
Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Now playing')
.setDescription(`Now playing ${bot.music.song.name}`)
channeldc.send({ embeds: [Embed] })
break
2022-11-09 07:48:54 -05:00
case 'queue':
2022-11-27 02:35:28 -05:00
const queueWithName = []
for (const song of bot.music.queue) queueWithName.push(song.name)
Embed = new EmbedBuilder()
2022-12-01 05:15:30 -05:00
.setColor(config.discord.embedsColors.normal)
2022-11-27 02:35:28 -05:00
.setTitle('Queue')
.setDescription(queueWithName.join(', '))
channeldc.send({ embeds: [Embed] })
break
2022-11-09 07:48:54 -05:00
default:
2022-11-27 02:35:28 -05:00
throw new SyntaxError('Invalid argument')
2022-10-20 00:41:25 -04:00
}
2022-11-27 02:35:28 -05:00
}
}