mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-27 17:45:34 -05:00
200 lines
6 KiB
JavaScript
200 lines
6 KiB
JavaScript
/* 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) {
|
|
try {
|
|
const filepath = values.join(' ');
|
|
const absolutePath = await resolve(filepath);
|
|
const song = bot.music.load(await fs.readFile(absolutePath), path.basename(absolutePath));
|
|
bot.music.play(song);
|
|
if (discord) {
|
|
const Embed = new MessageEmbed()
|
|
.setColor('#FFFF00')
|
|
.setTitle('Music')
|
|
.setDescription(`Now playing ${song.name}`);
|
|
channeldc.send({embeds: [Embed]});
|
|
} else {
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Now playing '}, {text: song.name, color: 'gold'}]));
|
|
}
|
|
} 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'}));
|
|
}
|
|
}
|
|
}
|
|
|
|
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',
|
|
});
|
|
const song = bot.music.load(response.data, url);
|
|
bot.music.play(song);
|
|
if (discord) {
|
|
const Embed = new MessageEmbed()
|
|
.setColor('#FFFF00')
|
|
.setTitle('Music')
|
|
.setDescription(`Now playing ${song.name}`);
|
|
channeldc.send({embeds: [Embed]});
|
|
} else {
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Now playing '}, {text: song.name, color: 'gold'}]));
|
|
}
|
|
} 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'}));
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
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: `*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));
|
|
};
|
|
|
|
module.exports = {
|
|
name: 'music',
|
|
description: 'Plays music',
|
|
alias: [],
|
|
trusted: 0,
|
|
usage: '<play|playurl|stop|loop|list|nowplaying> <song>',
|
|
execute: function(bot, username, usernameraw, sender, prefix, args) {
|
|
if (args[0]==='play') {
|
|
play(bot, args.slice(1));
|
|
}
|
|
if (args[0]==='playurl') {
|
|
playUrl(bot, args.slice(1));
|
|
}
|
|
if (args[0]==='stop') {
|
|
try {
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Stopped playing '}, {text: bot.music.song.name, color: 'gold'}]));
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
bot.music.stop();
|
|
}
|
|
if (args[0]==='loop') {
|
|
bot.music.loop = !bot.music.loop;
|
|
const loop = bot.music.loop ? {text: 'enabled', color: 'green'} : {text: 'disabled', color: 'red'};
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Looping is now '}, loop]));
|
|
}
|
|
if (args[0]==='list') {
|
|
list(bot);
|
|
}
|
|
if (args[0]==='nowplaying') {
|
|
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([{text: 'Now playing '}, {text: bot.music.song.name, color: 'gold'}]));
|
|
}
|
|
},
|
|
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')
|
|
.setDescription(`Stopped playing ${bot.music.song.name}`);
|
|
channeldc.send({embeds: [Embed]});
|
|
} catch (e) {
|
|
return;
|
|
}
|
|
bot.music.stop();
|
|
}
|
|
if (args[0]==='loop') {
|
|
bot.music.loop = !bot.music.loop;
|
|
const loop = bot.music.loop ? 'enabled' : 'disabled';
|
|
const Embed = new MessageEmbed()
|
|
.setColor('#FFFF00')
|
|
.setTitle('Loop')
|
|
.setDescription(`Looping is now ${loop}`);
|
|
channeldc.send({embeds: [Embed]});
|
|
}
|
|
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]});
|
|
}
|
|
},
|
|
};
|