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 ;
if ( os . hostname ( ) === 'chomens-kubuntu' ) {
SONGS _PATH = path . join ( _ _dirname , '..' , '..' , 'nginx-html' , 'midis' ) ;
} else {
SONGS _PATH = path . join ( _ _dirname , '..' , 'midis' ) ;
}
2022-10-20 00:41:25 -04:00
async function play ( bot , values , discord , channeldc ) {
try {
const filepath = values . join ( ' ' ) ;
const absolutePath = await resolve ( filepath ) ;
2022-10-30 08:17:20 -04:00
song = bot . music . load ( await fs . readFile ( absolutePath ) , path . basename ( absolutePath ) ) ;
2022-10-31 05:46:23 -04:00
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' )
2022-10-30 08:17:20 -04:00
. setDescription ( ` Added ${ song . name } to the song queue ` ) ;
2022-10-20 00:41:25 -04:00
channeldc . send ( { embeds : [ Embed ] } ) ;
} else {
2022-10-30 08:17:20 -04:00
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { 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 {
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( { 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-10-20 00:41:25 -04:00
async function playUrl ( bot , values , discord , channeldc ) {
try {
const url = values . join ( ' ' ) ;
const response = await axios . get ( 'https://http-proxy.nongsonchome.repl.co' , {
params : {
uri : url ,
} ,
responseType : 'arraybuffer' ,
} ) ;
2022-10-30 08:17:20 -04:00
song = bot . music . load ( response . data , url ) ;
2022-10-31 05:46:23 -04:00
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' )
2022-10-30 08:17:20 -04:00
. setDescription ( ` Added ${ song . name } to the song queue ` ) ;
2022-10-20 00:41:25 -04:00
channeldc . send ( { embeds : [ Embed ] } ) ;
} else {
2022-10-30 08:17:20 -04:00
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { 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 {
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( { 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 ;
}
async function list ( bot , discord , channeldc ) {
const absolutePath = await resolve ( SONGS _PATH ) ;
const listed = await fileList ( absolutePath ) ;
2022-08-14 05:51:45 -04:00
2022-10-20 00:41:25 -04:00
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-10-20 00:41:25 -04:00
let color = 'gold' ;
const message = [ ] ;
2022-08-17 09:39:25 -04:00
2022-10-20 00:41:25 -04:00
listed . forEach ( ( value ) => {
if ( color === 'gold' ) {
color = 'yellow' ;
} else if ( color === 'yellow' ) {
color = 'gold' ;
2022-08-14 05:51:45 -04:00
} ;
2022-10-20 00:41:25 -04:00
message . push ( { text : value + ' ' ,
color ,
clickEvent : {
action : 'suggest_command' ,
value : ` *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 . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( message ) ) ;
} ;
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 ,
2022-10-31 09:33:40 -04:00
usage : '<play|playurl|stop|loop|list|skip|loop|nowplaying> <song|all|current|off>' ,
2022-10-20 00:41:25 -04:00
execute : function ( bot , username , usernameraw , sender , prefix , args ) {
2022-08-14 05:51:45 -04:00
if ( args [ 0 ] === 'play' ) {
play ( bot , args . slice ( 1 ) ) ;
}
if ( args [ 0 ] === 'playurl' ) {
playUrl ( bot , args . slice ( 1 ) ) ;
}
if ( args [ 0 ] === 'stop' ) {
2022-08-16 08:15:11 -04:00
try {
2022-10-31 05:46:23 -04:00
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( { text : 'Cleared the song queue' } ) ) ;
2022-08-16 08:15:11 -04:00
} catch ( e ) {
return ;
}
2022-08-14 05:51:45 -04:00
bot . music . stop ( ) ;
}
2022-10-31 08:17:42 -04:00
if ( args [ 0 ] === 'skip' ) {
try {
2022-11-01 07:02:24 -04:00
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { text : 'Skipping ' } , { text : bot . music . song . name , color : 'gold' } ] ) ) ;
2022-10-31 08:17:42 -04:00
bot . music . skip ( ) ;
} catch ( e ) {
throw new Error ( 'No music is currently playing!' ) ;
}
}
2022-08-14 05:51:45 -04:00
if ( args [ 0 ] === 'loop' ) {
2022-10-30 08:17:20 -04:00
if ( args [ 1 ] === 'off' ) {
bot . music . loop = 0 ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { text : 'Looping is now ' } , { text : 'disabled' , color : 'red' } ] ) ) ;
}
if ( args [ 1 ] === 'current' ) {
bot . music . loop = 1 ;
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { text : 'Now Looping ' } , { text : song . name , color : 'gold' } ] ) ) ;
}
if ( args [ 1 ] === 'all' ) {
bot . music . loop = 2 ;
2022-10-31 05:46:23 -04:00
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( { text : 'Now looping every song in the queue' } ) ) ;
2022-10-30 08:17:20 -04:00
}
2022-08-14 05:51:45 -04:00
}
if ( args [ 0 ] === 'list' ) {
2022-10-20 00:41:25 -04:00
list ( bot ) ;
2022-08-14 05:51:45 -04:00
}
if ( args [ 0 ] === 'nowplaying' ) {
bot . core . run ( 'minecraft:tellraw @a ' + JSON . stringify ( [ { text : 'Now playing ' } , { text : bot . music . song . name , color : 'gold' } ] ) ) ;
}
} ,
2022-10-20 00:41:25 -04:00
discordExecute : function ( bot , username , usernameraw , sender , prefix , args , channeldc , message ) {
if ( args [ 0 ] === 'play' ) {
play ( bot , args . slice ( 1 ) , true , channeldc ) ;
}
if ( args [ 0 ] === 'playurl' ) {
playUrl ( bot , args . slice ( 1 ) , true , channeldc ) ;
}
if ( args [ 0 ] === 'stop' ) {
try {
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Stop' )
2022-10-31 05:46:23 -04:00
. setDescription ( 'Cleared the song queue' ) ;
2022-10-20 00:41:25 -04:00
channeldc . send ( { embeds : [ Embed ] } ) ;
} catch ( e ) {
return ;
}
bot . music . stop ( ) ;
}
2022-11-01 07:02:24 -04:00
if ( args [ 0 ] === 'skip' ) {
try {
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
2022-11-01 07:06:29 -04:00
. setTitle ( 'Skip' )
2022-11-01 07:02:24 -04:00
. setDescription ( ` Skipping ${ bot . music . song . name } ` ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
bot . music . skip ( ) ;
} catch ( e ) {
throw new Error ( 'No music is currently playing!' ) ;
}
}
2022-10-20 00:41:25 -04:00
if ( args [ 0 ] === 'loop' ) {
2022-10-31 05:46:23 -04:00
if ( args [ 1 ] === 'off' ) {
bot . music . loop = 0 ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Loop' )
2022-11-01 07:02:24 -04:00
. setDescription ( 'Looping is now disabled' ) ;
2022-10-31 05:46:23 -04:00
channeldc . send ( { embeds : [ Embed ] } ) ;
}
if ( args [ 1 ] === 'current' ) {
bot . music . loop = 1 ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Loop' )
. setDescription ( ` Now looping ${ song . name } ` ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
}
if ( args [ 1 ] === 'all' ) {
bot . music . loop = 2 ;
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Loop' )
. setDescription ( 'Now looping every song in the queue' ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
}
2022-10-20 00:41:25 -04:00
}
if ( args [ 0 ] === 'list' ) {
list ( bot , true , channeldc ) ;
}
if ( args [ 0 ] === 'nowplaying' ) {
const Embed = new MessageEmbed ( )
. setColor ( '#FFFF00' )
. setTitle ( 'Now playing' )
. setDescription ( ` Now playing ${ bot . music . song . name } ` ) ;
channeldc . send ( { embeds : [ Embed ] } ) ;
}
} ,
2022-08-14 05:51:45 -04:00
} ;