summaryrefslogtreecommitdiff
path: root/src/utils/helpers.js
blob: 0362528d9f87210f08a4b508f4e8523d5d2baad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const { PermissionsBitField } = require('discord.js');

function getQueueOrReply(interaction, queues, message = 'Not in a voice channel!') {
  const queue = queues.get(interaction.guild.id);
  
  if (!queue) {
    interaction.reply(message);
    return null;
  }
  
  return queue;
}

function requireVoiceChannel(interaction) {
  const voiceChannel = interaction.member?.voice?.channel;

  if (!voiceChannel) {
    interaction.editReply('You need to be in a voice channel!');
    return null;
  }

  const botMember = interaction.guild?.members?.me;
  if (!botMember) {
    interaction.editReply('Could not validate bot permissions in your voice channel. Try again in a moment.');
    return null;
  }

  const perms = voiceChannel.permissionsFor(botMember);
  const missing = [];

  if (!perms?.has(PermissionsBitField.Flags.ViewChannel)) missing.push('View Channel');
  if (!perms?.has(PermissionsBitField.Flags.Connect)) missing.push('Connect');
  if (!perms?.has(PermissionsBitField.Flags.Speak)) missing.push('Speak');

  if (missing.length > 0) {
    interaction.editReply(`I am missing permissions in that voice channel: ${missing.join(', ')}`);
    return null;
  }

  return voiceChannel;
}

module.exports = {
  getQueueOrReply,
  requireVoiceChannel,
};