{"size": 1900, "ext": "js", "max_stars_repo_path": "Interactions/Utils/Functions/addRole.js", "max_stars_repo_name": "wakayamadesu/Aesthete", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Interactions/Utils/Functions/addRole.js", "max_issues_repo_name": "wakayamadesu/Aesthete", "max_issues_repo_issues_event_min_datetime": "2021-11-21T14:03:35.000Z", "max_issues_repo_issues_event_max_datetime": "2021-11-21T16:09:48.000Z", "max_forks_repo_path": "Interactions/Utils/Functions/addRole.js", "max_forks_repo_name": "wakayamadesu/Aesthete", "max_forks_repo_forks_event_min_datetime": "2022-02-21T16:52:06.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-21T18:20:06.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 26.7605633803, "max_line_length": 77, "alphanum_fraction": 0.6378947368}
const emojiRegex = require('emoji-regex');
const localization = require('../../../Utils/localization');
module.exports = async (interaction, locale, role, message) => {
  const label = interaction.options.getString('label'),
    emoji = interaction.options.getString('emoji')?.split(/ +/g)[0],
    newRole = interaction.options.getRole('new-role');

  const components = message.components;
  const row = message.components.find((row) =>
    row.components[0].options.find((option) => option.value === role.id)
  );

  if (!row) {
    return interaction.reply({
      content: localization.errors.messageWithNoSelectRoles[locale],
      ephemeral: true,
    });
  }

  if (row.components[0].options.length + 1 > 25) {
    return interaction.reply({
      content: localization.errors.tooManyRoles[locale],
      ephemeral: true,
    });
  }

  if (!label) {
    return interaction.reply({
      content: localization.errors.noLabel[locale],
      ephemeral: true,
    });
  }

  if (!newRole) {
    return interaction.reply({
      content: localization.errors.noNewRole[locale],
      ephemeral: true,
    });
  }

  if (
    message.components.find((row) =>
      row.components[0].options.find((option) => option.value === newRole.id)
    )
  ) {
    return interaction.reply({
      content: localization.errors.roleAlreadyAdded[locale],
      ephemeral: true,
    });
  }

  if (
    emoji &&
    (emoji.startsWith('<a:') ||
      emoji.startsWith('<:') ||
      emojiRegex().test(emoji))
  ) {
    row.components[0].addOptions({ label, value: newRole.id, emoji });
  } else {
    row.components[0].addOptions({ label, value: newRole.id });
  }

  row.components[0].setMaxValues(components.length + 1);
  components[components.indexOf(row)] = row;
  await message.edit({ components });
  interaction.reply({
    content: localization.selectRoles.roleAdded[locale],
    ephemeral: true,
  });
};
