Giter Site home page Giter Site logo

permissions-plugin's Issues

validateComponents

export function validateComponents(bot: Bot, components: MessageComponents) {
  if (!components?.length) return;

  let actionRowCounter = 0;

  for (const component of components) {
    actionRowCounter++;
    // Max of 5 ActionRows per message
    if (actionRowCounter > 5) throw new Error(Errors.TOO_MANY_ACTION_ROWS);

    // Max of 5 Buttons (or any component type) within an ActionRow
    if (component.components?.length > 5) {
      throw new Error(Errors.TOO_MANY_COMPONENTS);
    } else if (
      component.components?.length > 1 &&
      component.components.some((subcomponent) => subcomponent.type === MessageComponentTypes.SelectMenu)
    ) {
      throw new Error(Errors.COMPONENT_SELECT_MUST_BE_ALONE);
    }

    for (const subcomponent of component.components) {
      if (subcomponent.customId && !bot.utils.validateLength(subcomponent.customId, { max: 100 })) {
        throw new Error(Errors.COMPONENT_CUSTOM_ID_TOO_BIG);
      }

      // 5 Link buttons can not have a customId
      if (subcomponent.type === MessageComponentTypes.Button) {
        if (subcomponent.style === ButtonStyles.Link && subcomponent.customId) {
          throw new Error(Errors.LINK_BUTTON_CANNOT_HAVE_CUSTOM_ID);
        }
        // Other buttons must have a customId
        if (!subcomponent.customId && subcomponent.style !== ButtonStyles.Link) {
          throw new Error(Errors.BUTTON_REQUIRES_CUSTOM_ID);
        }

        if (!bot.utils.validateLength(subcomponent.label, { max: 80 })) {
          throw new Error(Errors.COMPONENT_LABEL_TOO_BIG);
        }

        subcomponent.emoji = makeEmojiFromString(subcomponent.emoji);
      }

      if (subcomponent.type === MessageComponentTypes.SelectMenu) {
        if (subcomponent.placeholder && !bot.utils.validateLength(subcomponent.placeholder, { max: 100 })) {
          throw new Error(Errors.COMPONENT_PLACEHOLDER_TOO_BIG);
        }

        if (subcomponent.minValues) {
          if (subcomponent.minValues < 1) {
            throw new Error(Errors.COMPONENT_SELECT_MINVALUE_TOO_LOW);
          }

          if (subcomponent.minValues > 25) {
            throw new Error(Errors.COMPONENT_SELECT_MINVALUE_TOO_MANY);
          }

          if (!subcomponent.maxValues) subcomponent.maxValues = subcomponent.minValues;
          if (subcomponent.minValues > subcomponent.maxValues) {
            throw new Error(Errors.COMPONENT_SELECT_MIN_HIGHER_THAN_MAX);
          }
        }

        if (subcomponent.maxValues) {
          if (subcomponent.maxValues < 1) {
            throw new Error(Errors.COMPONENT_SELECT_MAXVALUE_TOO_LOW);
          }

          if (subcomponent.maxValues > 25) {
            throw new Error(Errors.COMPONENT_SELECT_MAXVALUE_TOO_MANY);
          }
        }

        if (subcomponent.options.length < 1) {
          throw new Error(Errors.COMPONENT_SELECT_OPTIONS_TOO_LOW);
        }

        if (subcomponent.options.length > 25) {
          throw new Error(Errors.COMPONENT_SELECT_OPTIONS_TOO_MANY);
        }

        let defaults = 0;

        for (const option of subcomponent.options) {
          if (option.default) {
            defaults++;
            if (defaults > (subcomponent.maxValues || 25)) {
              throw new Error(Errors.SELECT_OPTION_TOO_MANY_DEFAULTS);
            }
          }

          if (!bot.utils.validateLength(option.label, { max: 25 })) {
            throw new Error(Errors.SELECT_OPTION_LABEL_TOO_BIG);
          }

          if (!bot.utils.validateLength(option.value, { max: 100 })) {
            throw new Error(Errors.SELECT_OPTION_VALUE_TOO_BIG);
          }

          if (option.description && !bot.utils.validateLength(option.description, { max: 50 })) {
            throw new Error(Errors.SELECT_OPTION_VALUE_TOO_BIG);
          }

          option.emoji = makeEmojiFromString(option.emoji);
        }
      }
    }
  }
}

validateSlashOptions

export function validateSlashOptions(bot: Bot, options: ApplicationCommandOption[]) {
  const requiredOptions: ApplicationCommandOption[] = [];
  const optionalOptions: ApplicationCommandOption[] = [];

  for (const option of options) {
    bot.events.debug(`Running for of loop in validateSlashOptions function.`);
    option.name = option.name.toLowerCase();

    if (option.choices?.length) {
      if (option.choices.length > 25) throw new Error(Errors.TOO_MANY_SLASH_OPTION_CHOICES);
      if (option.type !== ApplicationCommandOptionTypes.String && option.type !== ApplicationCommandOptionTypes.Integer)
        throw new Error(Errors.ONLY_STRING_OR_INTEGER_OPTIONS_CAN_HAVE_CHOICES);
    }

    if (!bot.utils.validateLength(option.name, { min: 1, max: 32 })) throw new Error(Errors.INVALID_SLASH_OPTION_NAME);

    if (!bot.utils.validateLength(option.description, { min: 1, max: 100 }))
      throw new Error(Errors.INVALID_SLASH_OPTION_DESCRIPTION);

    if (option.choices) {
      bot.utils.validateSlashOptionChoices(bot, option.choices, option.type);
    }

    if (option.required) {
      requiredOptions.push(option);
      continue;
    }

    optionalOptions.push(option);
  }

  return [...requiredOptions, ...optionalOptions];
}

validateSlashCommands

export function validateSlashCommands(
  bot: Bot,
  commands: (CreateGlobalApplicationCommand | EditGlobalApplicationCommand)[],
  create = false
) {
  return commands.map((command) => {
    bot.events.debug(`Running for of loop in validateSlashCommands function.`);
    if (create) {
      if (!command.name) throw new Error(Errors.INVALID_SLASH_NAME);
      // Slash commands require description
      if (!command.description && (!command.type || command.type === ApplicationCommandTypes.ChatInput))
        throw new Error(Errors.INVALID_CONTEXT_MENU_COMMAND_DESCRIPTION);

      if (command.description && (!command.type || command.type !== ApplicationCommandTypes.ChatInput))
        throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
    }

    if (command.name) {
      if (!command.type || command.type === ApplicationCommandTypes.ChatInput) {
        if (!SLASH_COMMANDS_NAME_REGEX.test(command.name)) {
          throw new Error(Errors.INVALID_SLASH_NAME);
        }

        // Only slash need to be lowercase
        command.name = command.name.toLowerCase();
      } else {
        if (!CONTEXT_MENU_COMMANDS_NAME_REGEX.test(command.name)) {
          throw new Error(Errors.INVALID_CONTEXT_MENU_COMMAND_NAME);
        }
      }
    }

    if (command.description && !bot.utils.validateLength(command.description, { min: 1, max: 100 })) {
      throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
    }

    if (command.options?.length) {
      if (command.options.length > 25) {
        throw new Error(Errors.TOO_MANY_SLASH_OPTIONS);
      }

      command.options = bot.utils.validateSlashOptions(bot, command.options);
    }

    return command;
  });
}export function validateSlashCommands(
  bot: Bot,
  commands: (CreateGlobalApplicationCommand | EditGlobalApplicationCommand)[],
  create = false
) {
  return commands.map((command) => {
    bot.events.debug(`Running for of loop in validateSlashCommands function.`);
    if (create) {
      if (!command.name) throw new Error(Errors.INVALID_SLASH_NAME);
      // Slash commands require description
      if (!command.description && (!command.type || command.type === ApplicationCommandTypes.ChatInput))
        throw new Error(Errors.INVALID_CONTEXT_MENU_COMMAND_DESCRIPTION);

      if (command.description && (!command.type || command.type !== ApplicationCommandTypes.ChatInput))
        throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
    }

    if (command.name) {
      if (!command.type || command.type === ApplicationCommandTypes.ChatInput) {
        if (!SLASH_COMMANDS_NAME_REGEX.test(command.name)) {
          throw new Error(Errors.INVALID_SLASH_NAME);
        }

        // Only slash need to be lowercase
        command.name = command.name.toLowerCase();
      } else {
        if (!CONTEXT_MENU_COMMANDS_NAME_REGEX.test(command.name)) {
          throw new Error(Errors.INVALID_CONTEXT_MENU_COMMAND_NAME);
        }
      }
    }

    if (command.description && !bot.utils.validateLength(command.description, { min: 1, max: 100 })) {
      throw new Error(Errors.INVALID_SLASH_DESCRIPTION);
    }

    if (command.options?.length) {
      if (command.options.length > 25) {
        throw new Error(Errors.TOO_MANY_SLASH_OPTIONS);
      }

      command.options = bot.utils.validateSlashOptions(bot, command.options);
    }

    return command;
  });
}

validateSlashOptionChoices

return choices.every((choice) => {
    bot.events.debug(`Running for of loop in validateSlashOptionChoices function.`);
    if (!bot.utils.validateLength(choice.name, { min: 1, max: 100 })) {
      throw new Error(Errors.INVALID_SLASH_OPTION_CHOICE_NAME);
    }

    if (
      optionType === ApplicationCommandOptionTypes.String &&
      (typeof choice.value !== "string" || choice.value.length < 1 || choice.value.length > 100)
    ) {
      throw new Error(Errors.INVALID_SLASH_OPTIONS_CHOICE_VALUE_TYPE);
    }

    if (optionType === ApplicationCommandOptionTypes.Integer && typeof choice.value !== "number") {
      throw new Error(Errors.INVALID_SLASH_OPTIONS_CHOICE_VALUE_TYPE);
    }
  });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.