<commit_before>/**
 * @file poll command
 * @author <NAME> (a.k.a k3rn31p4nic)
 * @license MIT
 */

exports.exec = async (Bastion, message, args) => {
  try {
    if (args.length < 1 || !/^(.+( ?; ?.+[^;])+)$/i.test(args.join(' '))) {
      /**
      * The command was ran with invalid parameters.
      * @fires commandUsage
      */
      return Bastion.emit('commandUsage', message, this.help);
    }
    args = args.join(' ').split(';');

    if (!message.channel.hasOwnProperty('poll')) {
      message.channel.poll = {};
      message.channel.poll.usersVoted = [];

      let answers = [];
      for (let i = 1; i < args.length; i++) {
        answers.push({
          name: `${i}.`,
          value: `${args[i]}`,
          inline: true
        });
      }

      let pollStatus = await message.channel.send({
        embed: {
          color: Bastion.colors.BLUE,
          title: 'Poll started',
          description: `A poll has been started by ${message.author}.\n\n**${args[0]}**`,
          fields: answers,
          footer: {
            text: 'Vote by typing the corresponding number of the option.'
          }
        }
      });

      message.channel.poll.collector = message.channel.createMessageCollector(
        m => (!m.author.bot && parseInt(m.content) > 0 && parseInt(m.content) < args.length && !message.channel.poll.usersVoted.includes(m.author.id)),
        { time: 6 * 60 * 60 * 1000 }
      );

      message.channel.poll.collector.on('collect', (msg, votes) => {
        if (msg.deletable) {
          msg.delete().catch(e => {
            Bastion.log.error(e);
          });
        }
        msg.channel.send({
          embed: {
            description: `Thank you, ${msg.author}, for voting.`,
            footer: {
              text: `${votes.collected.size} votes in total.`
            }
          }
        }).then(m => {
          message.channel.poll.usersVoted.push(msg.author.id);
          m.delete(5000).catch(e => {
            Bastion.log.error(e);
          });
        });
      });

      message.channel.poll.collector.on('end', (pollRes) => {
        pollRes = pollRes.map(r => r.content);
        pollRes = pollRes.filter(res => parseInt(res) && parseInt(res) > 0 && parseInt(res) < args.length);
        if (pollRes.length === 0) {
          return message.channel.send({
            embed: {
              color: Bastion.colors.RED,
              title: 'Poll Ended',
              description: 'Unfortunately, no votes were given.'
            }
          }).then(() => {
            pollStatus.delete().catch(e => {
              Bastion.log.error(e);
            });
            delete message.channel.poll;
          }).catch(e => {
            Bastion.log.error(e);
          });
        }

        for (let i = args.length - 1; i > 0; i--) {
          pollRes.unshift(i);
        }
        let count = {};
        for (let i = 0; i < pollRes.length; i++) {
          count[pollRes[i]] = count[pollRes[i]] ? count[pollRes[i]] + 1 : 1;
        }
        let result = [];
        for (let i = 1; i < args.length; i++) {
          result.push({
            name: args[i],
            value: `${((count[Object.keys(count)[i - 1]] - 1) / (pollRes.length - (args.length - 1))) * 100}%`,
            inline: true
          });
        }

        message.channel.send({
          embed: {
            color: Bastion.colors.BLUE,
            title: 'Poll Ended',
            description: `Poll results for **${args[0]}**`,
            fields: result
          }
        }).then(() => {
          pollStatus.delete().catch(e => {
            Bastion.log.error(e);
          });
          delete message.channel.poll;
        }).catch(e => {
          Bastion.log.error(e);
        });
      });
    }
    else {
      /**
      * Error condition is encountered.
      * @fires error
      */
      return Bastion.emit('error', Bastion.strings.error(message.guild.language, 'busy'), Bastion.strings.error(message.guild.language, 'isEventInUse', true, 'poll'), message.channel);
    }
  }
  catch (e) {
    Bastion.log.error(e);
  }
};

exports.config = {
  aliases: [],
  enabled: true
};

exports.help = {
  name: 'poll',
  botPermission: '',
  userTextPermission: 'MANAGE_MESSAGES',
  userVoicePermission: '',
  usage: 'poll <question>;<option1>;<option2>[;<option3>[...]]',
  example: [ 'poll Which is the game of the week?;Call of Duty©: Infinity Warfare;Tom Clancy\'s Ghost Recon© Wildlands;Watch Dogs 2' ]
};
<commit_msg>Minor improvements to poll command<commit_after>/**
 * @file poll command
 * @author <NAME> (a.k.a k3rn31p4nic)
 * @license MIT
 */

exports.exec = async (Bastion, message, args) => {
  try {
    if (!args.pollMessage || !/^(.+( ?; ?.+[^;])+)$/i.test(args.pollMessage.join(' '))) {
      /**
      * The command was ran with invalid parameters.
      * @fires commandUsage
      */
      return Bastion.emit('commandUsage', message, this.help);
    }
    let pollMessage = args.pollMessage.join(' ').split(';');

    if (!message.channel.hasOwnProperty('poll')) {
      message.channel.poll = {};
      message.channel.poll.usersVoted = [];

      let answers = [];
      for (let i = 1; i < pollMessage.length; i++) {
        answers.push({
          name: `${i}.`,
          value: `${pollMessage[i]}`,
          inline: true
        });
      }

      let pollStatus = await message.channel.send({
        embed: {
          color: Bastion.colors.BLUE,
          title: 'Poll started',
          description: `A poll has been started by ${message.author}.\n\n**${pollMessage[0]}**`,
          fields: answers,
          footer: {
            text: 'Vote by typing the corresponding number of the option.'
          }
        }
      });

      message.channel.poll.collector = message.channel.createMessageCollector(
        m => (!m.author.bot && parseInt(m.content) > 0 && parseInt(m.content) < pollMessage.length && !message.channel.poll.usersVoted.includes(m.author.id)),
        { time: 6 * 60 * 60 * 1000 }
      );

      message.channel.poll.collector.on('collect', (msg, votes) => {
        if (msg.deletable) {
          msg.delete().catch(e => {
            Bastion.log.error(e);
          });
        }
        msg.channel.send({
          embed: {
            description: `Thank you, ${msg.author}, for voting.`,
            footer: {
              text: `${votes.collected.size} votes in total.`
            }
          }
        }).then(m => {
          message.channel.poll.usersVoted.push(msg.author.id);
          m.delete(5000).catch(e => {
            Bastion.log.error(e);
          });
        });
      });

      message.channel.poll.collector.on('end', (pollRes) => {
        pollRes = pollRes.map(r => r.content);
        pollRes = pollRes.filter(res => parseInt(res) && parseInt(res) > 0 && parseInt(res) < pollMessage.length);
        if (pollRes.length === 0) {
          return message.channel.send({
            embed: {
              color: Bastion.colors.RED,
              title: 'Poll Ended',
              description: 'Unfortunately, no votes were given.'
            }
          }).then(() => {
            pollStatus.delete().catch(e => {
              Bastion.log.error(e);
            });
            delete message.channel.poll;
          }).catch(e => {
            Bastion.log.error(e);
          });
        }

        for (let i = pollMessage.length - 1; i > 0; i--) {
          pollRes.unshift(i);
        }
        let count = {};
        for (let i = 0; i < pollRes.length; i++) {
          count[pollRes[i]] = count[pollRes[i]] ? count[pollRes[i]] + 1 : 1;
        }
        let result = [];
        for (let i = 1; i < pollMessage.length; i++) {
          result.push({
            name: pollMessage[i],
            value: `${((count[Object.keys(count)[i - 1]] - 1) / (pollRes.length - (pollMessage.length - 1))) * 100}%`,
            inline: true
          });
        }

        message.channel.send({
          embed: {
            color: Bastion.colors.BLUE,
            title: 'Poll Ended',
            description: `Poll results for **${pollMessage[0]}**`,
            fields: result
          }
        }).then(() => {
          pollStatus.delete().catch(e => {
            Bastion.log.error(e);
          });
          delete message.channel.poll;
        }).catch(e => {
          Bastion.log.error(e);
        });
      });
    }
    else {
      /**
       * Error condition is encountered.
       * @fires error
       */
      return Bastion.emit('error', Bastion.strings.error(message.guild.language, 'busy'), Bastion.strings.error(message.guild.language, 'isEventInUse', true, 'poll'), message.channel);
    }
  }
  catch (e) {
    Bastion.log.error(e);
  }
};

exports.config = {
  aliases: [],
  enabled: true,
  argsDefinitions: [
    { name: 'pollMessage', type: String, multiple: true, defaultOption: true }
  ]
};

exports.help = {
  name: 'poll',
  botPermission: '',
  userTextPermission: 'MANAGE_MESSAGES',
  userVoicePermission: '',
  usage: 'poll <question>;<option1>[;<option2>[...]] [-t TIME_IN_MINUTES]',
  example: [ 'poll Which is the game of the week?;Call of Duty©: Infinity Warfare;<NAME>\'s Ghost Recon© Wildlands' ]
};

{'fraction_non_alphanumeric': 0.17018498367791077, 'fraction_numerical': 0.00837867247007617, 'mean_word_length': 1.803843807199512, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1169508', 'n_tokens_mistral': 2907, 'n_tokens_neox': 2837, 'n_words': 572}