mrmBot-Matrix/utils/soundplayer.js

68 lines
2.7 KiB
JavaScript
Raw Normal View History

2019-09-13 20:02:41 +00:00
const client = require("./client.js");
const logger = require("./logger.js");
const fetch = require("node-fetch");
const { Manager } = require("@lavacord/eris");
2019-09-13 20:02:41 +00:00
const nodes = [
{ id: "1", host: "localhost", port: 2333, password: process.env.LAVAPASS }
];
let manager;
exports.status = false;
exports.checkStatus = async () => {
const statuses = [];
for (const node of nodes) {
try {
const response = await fetch(`http://${node.host}:${node.port}/version`, { headers: { Authorization: node.password } }).then(res => res.text());
if (response) statuses.push(false);
} catch {
statuses.push(true);
}
}
const result = statuses.filter(Boolean);
this.status = result.length > 0 ? true : false;
return this.status;
};
exports.connect = async () => {
manager = new Manager(client, nodes, {
user: client.user.id
});
const { length } = await manager.connect();
logger.log(`Successfully connected to ${length} Lavalink node(s).`);
manager.on("error", (error, node) => {
logger.error(`An error occurred on Lavalink node ${node}: ${error}`);
});
};
exports.play = async (sound, message) => {
2019-09-13 20:02:41 +00:00
if (message.member.voiceState.channelID) {
if (!message.channel.guild.members.get(client.user.id).permission.has("voiceConnect") || !message.channel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I can't join this voice channel!`);
const voiceChannel = message.channel.guild.channels.get(message.member.voiceState.channelID);
if (!voiceChannel.permissionsOf(client.user.id).has("voiceConnect")) return client.createMessage(message.channel.id, `${message.author.mention}, I don't have permission to join this voice channel!`);
const node = manager.idealNodes[0];
const { tracks } = await fetch(`http://${node.host}:${node.port}/loadtracks?identifier=${sound}`, { headers: { Authorization: node.password } }).then(res => res.json());
const connection = await manager.join({
guild: voiceChannel.guild.id,
channel: voiceChannel.id,
node: node.id
});
const playingMessage = await client.createMessage(message.channel.id, "🔊 Playing sound...");
await connection.play(tracks[0].track);
2019-09-13 20:02:41 +00:00
connection.on("error", (error) => {
manager.leave(voiceChannel.guild.id);
playingMessage.delete();
logger.error(error);
2019-09-13 20:02:41 +00:00
});
connection.once("end", (data) => {
if (data.reason === "REPLACED") return;
manager.leave(voiceChannel.guild.id);
playingMessage.delete();
2019-09-13 20:02:41 +00:00
});
} else {
client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`);
}
};