mrmBot-Matrix/utils/soundplayer.js

37 lines
1.8 KiB
JavaScript

const client = require("./client.js");
const fs = require("fs");
const logger = require("./logger.js");
const connections = require("./collections.js").voiceConnections;
module.exports = async (sound, message) => {
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 checkConnection = connections.get(message.channel.guild.id);
const connection = checkConnection ? checkConnection : await voiceChannel.join();
const playingMessage = await client.createMessage(message.channel.id, "🔊 Playing sound...");
connections.set(message.channel.guild.id, connection);
if (connection.playing) {
connection.stopPlaying();
}
connection.play(fs.createReadStream(sound));
connection.on("error", (error) => {
connections.delete(message.channel.guild.id);
voiceChannel.leave();
playingMessage.delete();
logger.error(error);
});
connection.on("warn", (warn) => {
logger.warn(warn);
});
connection.once("end", () => {
connections.delete(message.channel.guild.id);
voiceChannel.leave();
playingMessage.delete();
});
} else {
client.createMessage(message.channel.id, `${message.author.mention}, you need to be in a voice channel first!`);
}
};