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

let activeChannel;

exports.run = async (Bastion, message, args) => {
  if (!Bastion.credentials.ownerId.includes(message.author.id)) {
    /**
     * User has missing permissions.
     * @fires userMissingPermissions
     */
    return Bastion.emit('userMissingPermissions', this.help.userPermission);
  }

  if (!args.amount || isNaN(args.amount)) {
    /**
     * The command was ran with invalid parameters.
     * @fires commandUsage
     */
    return Bastion.emit('commandUsage', message, this.help);
  }

  if (!activeChannel) {
    /**
     * Time in hour(s) the giveaway event should go on.
     * @constant
     * @type {number}
     * @default
     */
    const TIMEOUT = 3;
    let reaction = [ '🎈', '🎊', '🎉', '🎃', '🎁', '🎁' ];

    reaction = reaction[Math.floor(Math.random() * reaction.length)];

    try {
      let giveawayMessage = await message.channel.send({
        embed: {
          color: Bastion.colors.BLUE,
          title: 'GIVEAWAY! 🎉',
          description: `Giveaway event started. React to this message with ${reaction} to get **${args.amount}** Bastion Currencies.`,
          footer: {
            text: `Event stops in ${TIMEOUT} hour. You will get your reward after the event has concluded.`
          }
        }
      });
      let giveawayMessageID = giveawayMessage.id;
      activeChannel = message.channel.id;

      setTimeout(async () => {
        let giveawayMessage = await message.channel.fetchMessage(giveawayMessageID).catch(e => {
          Bastion.log.error(e);
        });

        // reaction = encodeURIComponent(reaction);

        let winners = [];
        if (giveawayMessage.reactions.get(reaction)) {
          winners = giveawayMessage.reactions.get(reaction).users.map(u => u.id);
        }

        let winner = winners[Math.floor(Math.random() * winners.length)];
        winner = Bastion.users.get(winner);
        if (winner) {
          /**
          * User's account is debited with `args.amount` Bastion Currencies
          * @fires userDebit
          */
          Bastion.emit('userDebit', winner, args.amount);

          giveawayMessage.edit('', {
            embed: {
              color: Bastion.colors.BLUE,
              title: 'Giveaway event ended',
              description: `${winner.tag} won the giveaway! And has been awarded with **${args.amount}** Bastion Currencies.\nThank you everyone for participating. Better luck next time.`
            }
          }).catch(e => {
            Bastion.log.error(e);
          });
        }

        activeChannel = null;
      }, TIMEOUT * 60 * 60 * 1000);
    }
    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, 'giveaway'), message.channel);
  }
};

exports.config = {
  aliases: [],
  enabled: true,
  argsDefinitions: [
    { name: 'amount', type: Number, alias: 'a', multiple: true, defaultOption: true }
  ]
};

exports.help = {
  name: 'giveaway',
  botPermission: '',
  userPermission: 'BOT_OWNER',
  usage: 'giveaway <amount>',
  example: [ 'giveaway 10' ]<commit_msg>Minor code improvements<commit_after>/**
 * @file giveaway command
 * @author <NAME> (a.k.a k3rn31p4nic)
 * @license MIT
 */

let activeChannel;

exports.run = async (Bastion, message, args) => {
  if (!Bastion.credentials.ownerId.includes(message.author.id)) {
    /**
     * User has missing permissions.
     * @fires userMissingPermissions
     */
    return Bastion.emit('userMissingPermissions', this.help.userPermission);
  }

  if (!args.amount || isNaN(args.amount)) {
    /**
     * The command was ran with invalid parameters.
     * @fires commandUsage
     */
    return Bastion.emit('commandUsage', message, this.help);
  }

  if (!activeChannel) {
    /**
     * Time in hour(s) the giveaway event should go on.
     * @constant
     * @type {number}
     * @default
     */
    const TIMEOUT = 3;
    let reaction = [ '🎈', '🎊', '🎉', '🎃', '🎁', '🎁' ];

    reaction = reaction[Math.floor(Math.random() * reaction.length)];

    try {
      await message.channel.send({
        embed: {
          color: Bastion.colors.BLUE,
          title: 'GIVEAWAY! 🎉',
          description: `Giveaway event started. React to this message with ${reaction} to get **${args.amount}** Bastion Currencies.`,
          footer: {
            text: `Event stops in ${TIMEOUT} hour. You will get your reward after the event has concluded.`
          }
        }
      });

      activeChannel = message.channel.id;

      setTimeout(async () => {
        try {
          let giveawayMessage = await message.channel.fetchMessage(giveawayMessage.id);

          // reaction = encodeURIComponent(reaction);

          let winners = [];
          if (giveawayMessage.reactions.get(reaction)) {
            winners = giveawayMessage.reactions.get(reaction).users.map(u => u.id);
          }

          let winner = winners[Math.floor(Math.random() * winners.length)];
          winner = Bastion.users.get(winner);
          if (winner) {
            /**
            * User's account is debited with `args.amount` Bastion Currencies
            * @fires userDebit
            */
            Bastion.emit('userDebit', winner, args.amount);

            giveawayMessage.edit('', {
              embed: {
                color: Bastion.colors.BLUE,
                title: 'Giveaway event ended',
                description: `${winner.tag} won the giveaway! And has been awarded with **${args.amount}** Bastion Currencies.\nThank you everyone for participating. Better luck next time.`
              }
            }).catch(e => {
              Bastion.log.error(e);
            });
          }

          activeChannel = null;
        }
        catch (e) {
          Bastion.log.error(e);
        }
      }, TIMEOUT * 60 * 60 * 1000);
    }
    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, 'giveaway'), message.channel);
  }
};

exports.config = {
  aliases: [],
  enabled: true,
  argsDefinitions: [
    { name: 'amount', type: Number, alias: 'a', multiple: true, defaultOption: true }
  ]
};

exports.help = {
  name: 'giveaway',
  botPermission: '',
  userPermission: 'BOT_OWNER',
  usage: 'giveaway <amount>',
  example: [ 'giveaway 10' ]
{'fraction_non_alphanumeric': 0.14162451651294258, 'fraction_numerical': 0.0044629574531389465, 'mean_word_length': 2.2446911196911197, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, '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': '1042719', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2035, 'n_words': 480}