aboutsummaryrefslogtreecommitdiff
path: root/Utilities/Voting.js
blob: 452dc55458aae2533ea7e17b846998ed0589447f (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**************************************************************************
 *
 *  DLAP Bot: A Discord bot that plays local audio tracks.
 *  (C) Copyright 2022
 *  Programmed by Andrew Lee
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 ***************************************************************************/
import { nextAudio, playerStatus, previousAudio } from '../AudioBackend/AudioControl.js';
import { PermissionFlagsBits } from 'discord-api-types/v10';
import { readFileSync } from 'node:fs';
import { player } from '../AudioBackend/VoiceInitialization.js';
import i18next from '../Utilities/i18n.js';

const { djRole, ownerID } = JSON.parse(readFileSync('./config.json', 'utf-8'));
const t = i18next.t;
export const votes = new Set();
let nextCheck;

async function commandCheck(interaction, bot) {
  if (nextCheck) {
    await interaction.reply({ content: t('musicNext') });
    player.stop();
    return await nextAudio(bot);
  } else {
    await previousAudio(bot, interaction);
  }
}

export async function voteSkip(interaction, bot) {
  if (interaction.commandName === 'next') {
    if (nextCheck !== true) {
      // Reset the votes if the current value of nextCheck is different from the command being executed
      votes.clear();
    }
    nextCheck = true;
  } else if (interaction.commandName === 'previous') {
    if (nextCheck !== false) {
      // Reset the votes if the current value of nextCheck is different from the command being executed
      votes.clear();
    }
    nextCheck = false;
  }

  if (interaction.options.getSubcommand() === 'vote') {
    // Get the members of the voice channel who have not voted yet
    const voiceChannel = interaction.member.voice.channel;
    const members = voiceChannel.members.filter(m => !votes.has(m.id));

    // Calculate the number of votes required to skip the audio track
    const votesRequired = Math.ceil(members.size / 2);

    // Check if the message author has already voted
    if (votes.has(interaction.user.id)) {
      return interaction.reply({ content: t('alreadyVoted', votesRequired), ephemeral: true });
    }

    if (playerStatus === 0 || playerStatus === 1) {
      // Add the message author to the set of members who have voted
      votes.add(interaction.user.id);
      if (votes.size >= votesRequired) {
        console.log(t('enoughVotes'));
        // Reset the number of votes
        votes.clear();
        // Do something to skip the audio track here (e.g. player.stop())
        await commandCheck(interaction, bot);
      } else {
        // Send a message with the number of votes needed to skip the audio track
        console.log(t('votesNeeded', { votesRequired: votesRequired - 1 }));
        await interaction.reply({ content: t('votesNeeded', { votesRequired: votesRequired - 1 }) });
      }
    } else if (playerStatus === 2) {
      return await interaction.reply({ content: t('playerStopped'), ephemeral: true });
    }
  }

  if (interaction.options.getSubcommand() === 'force') {
    if (!interaction.member.roles.cache.has(djRole) && interaction.user.id !== ownerID && !interaction.memberPermissions.has(PermissionFlagsBits.ManageGuild)) return interaction.reply({ content: t('rolePermission'), ephemeral: true });
    console.log(t('forceSkip'));
    if (playerStatus === 0 || playerStatus === 1) {
      votes.clear();
      // Do something to skip the audio track here (e.g. player.stop())
      await commandCheck(interaction, bot);
    } else if (playerStatus === 2) {
      return await interaction.reply({ content: t('playerStopped'), ephemeral: true });
    }
  }
}