summaryrefslogtreecommitdiff
path: root/src/utils/helpers.js
blob: b407fb08e72bd3313b5897827041ccb7897f855d (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
47
48
49
50
51
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;
  }

  if (voiceChannel.full && !perms.has(PermissionsBitField.Flags.MoveMembers)) {
    interaction.editReply(`That voice channel is full (limit: ${voiceChannel.userLimit}). Raise the user limit or give me Move Members permission.`);
    return null;
  }

  return voiceChannel;
}

module.exports = {
  getQueueOrReply,
  requireVoiceChannel,
};