mrmBot-Matrix/utils/soundplayer.js

31 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-09-13 20:02:41 +00:00
const client = require("./client.js");
const logger = require("./logger.js");
2019-09-13 20:02:41 +00:00
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 connection = await voiceChannel.join({
opusOnly: true
});
if (connection.playing) return client.createMessage(message.channel.id, `${message.author.mention}, I'm already playing a sound!`);
const playingMessage = await client.createMessage(message.channel.id, "🔊 Playing sound...");
2019-09-13 20:02:41 +00:00
if (connection.playing) {
connection.stopPlaying();
}
connection.play(sound);
2019-09-13 20:02:41 +00:00
connection.on("error", (error) => {
voiceChannel.leave();
playingMessage.delete();
logger.error(error);
2019-09-13 20:02:41 +00:00
});
connection.once("end", () => {
voiceChannel.leave();
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!`);
}
};