/ src / preconditions / connected.ts
connected.ts
 1  import { Precondition } from 'amethystjs';
 2  import { GuildMember } from 'discord.js';
 3  import { players } from '../cache/players';
 4  
 5  export default new Precondition('inVoiceChannel').setChatInputRun(({ interaction }) => {
 6      if (interaction.replied || interaction.deferred)
 7          return { ok: false, message: 'Already handled', type: 'chatInput', interaction };
 8      if (!interaction.guild)
 9          return {
10              ok: false,
11              message: 'Not connected to a voice channel',
12              type: 'chatInput',
13              interaction,
14              metadata: {
15                  message: interaction.client.langs.getText(interaction, 'preconditions', 'connected')
16              }
17          };
18      if (
19          !(interaction.member as GuildMember).voice?.channel ||
20          (players.has(interaction.guild.id) &&
21              interaction.guild.members.me?.voice?.channel?.members?.filter((x) => x.user.id === interaction.user.id)
22                  .size === 0)
23      )
24          return {
25              ok: false,
26              message: 'Not connected to a voice channel',
27              type: 'chatInput',
28              interaction,
29              metadata: {
30                  message: interaction.client.langs.getText(interaction, 'preconditions', 'connected')
31              }
32          };
33      return {
34          ok: true,
35          type: 'chatInput',
36          interaction
37      };
38  });