{"size": 2183, "ext": "js", "max_stars_repo_path": "commands/upvote.js", "max_stars_repo_name": "CodeBull/SFRVotingBot", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "commands/upvote.js", "max_issues_repo_name": "CodeBull/SFRVotingBot", "max_issues_repo_issues_event_min_datetime": "2021-03-10T07:16:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-11T02:15:13.000Z", "max_forks_repo_path": "commands/upvote.js", "max_forks_repo_name": "reazuliqbal/SFRVotingBot", "max_forks_repo_forks_event_min_datetime": "2019-11-02T05:58:58.000Z", "max_forks_repo_forks_event_max_datetime": "2019-12-15T07:23:35.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 29.5, "max_line_length": 179, "alphanum_fraction": 0.568483738}
const config = require('../config');
const { checkPost, getAuthorAndPermlink } = require('../modules/utils');

module.exports = {
  name: 'upvote',
  args: true,
  guildOnly: true,
  usage: '<AMOUNT> <URL>',
  description: 'Request upvote from the bot account.',
  async execute(message, args) {
    let [amount, url] = args;

    amount = parseFloat(amount);
    url = url.trim().replace(/<|>/g, '');

    if (Number.isNaN(amount)) {
      return message.reply('Invalid Amount! Amount should be a number.');
    }

    const { author, permlink } = getAuthorAndPermlink(url);

    if (!author || !permlink) {
      return message.reply('Invalid URL! Please enter URL in `@author/permalink` format.');
    }

    const status = await checkPost(author, permlink);

    if (status.success && status.error === null) {
      const json = JSON.stringify({
        contractName: 'tokens',
        contractAction: 'transfer',
        contractPayload: {
          symbol: config.SE_COIN_SYMBOL,
          to: config.BOT_ACCOUNT,
          quantity: `${amount}`,
          memo: url,
        },
      });

      let sc = 'https://steemconnect.com/sign/custom-json?';
      sc += `required_posting_auths=${encodeURI('[]')}`;
      sc += `&required_auths=${encodeURI(`["${author}"]`)}`;
      sc += `&id=${config.SE_CHAIN_ID}`;
      sc += `&json=${encodeURI(json)}`;

      message.channel.send(`${message.member} please send:\nAmount: \`${amount} ${config.SE_COIN_SYMBOL}\`\nTo: \`${config.BOT_ACCOUNT}\`\nMemo: \`${url}\`\nSteemConnect: ${sc}`);
    } else {
      let msg = '';

      switch (status.error) {
        case 'not_whitelisted':
          msg = 'Author is not whitelisted.';
          break;
        case 'comment_not_allowed':
          msg = 'Comment not allowed.';
          break;
        case 'already_voted':
          msg = 'We already upvoted the content.';
          break;
        case 'max_age':
          msg = `Post is older than ${config.MAX_POST_AGE} hours.`;
          break;
        case 'min_age':
          msg = `Post should be at least ${config.MIN_POST_AGE} minutes old.`;
          break;

        default:
      }

      message.reply(msg);
    }
  },
};
