{"size": 1954, "ext": "js", "max_stars_repo_path": "packages/aquarius/src/core/helpers/permissions.js", "max_stars_repo_name": "bucket3432/aquarius", "max_stars_repo_stars_event_min_datetime": "2017-08-13T17:27:53.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-17T13:51:42.000Z", "max_issues_repo_path": "packages/aquarius/src/core/helpers/permissions.js", "max_issues_repo_name": "bucket3432/aquarius", "max_issues_repo_issues_event_min_datetime": "2016-05-03T03:35:28.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T23:00:30.000Z", "max_forks_repo_path": "packages/aquarius/src/core/helpers/permissions.js", "max_forks_repo_name": "bucket3432/aquarius", "max_forks_repo_forks_event_min_datetime": "2016-05-05T01:24:23.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T05:30:18.000Z"}
{"max_stars_count": 60, "max_issues_count": 777, "max_forks_count": 44, "avg_line_length": 30.53125, "max_line_length": 76, "alphanum_fraction": 0.70419652}
import { getPermissionName, isGuildAdmin } from '@aquarius-bot/permissions';
import pluralize from 'pluralize';
import aquarius from '../../aquarius';
import { humanize } from './lists';

/**
 * @typedef {import('discord.js').User} User
 * @typedef {import('discord.js').Guild} Guild
 */

/**
 * Checks to see if a User is a bot owner
 * @param {User} user - user to check
 * @returns {boolean} Whether the user is the bot owner
 */
export function isBotOwner(user) {
  return user.id === aquarius.config.owner;
}

/**
 * Checks to see if a User is a bot admin
 * @param {User} user - user to check
 * @returns {boolean} Whether the user is a bot admin
 */
export function isBotAdmin(user) {
  return aquarius.config.admins.includes(user.id) || isBotOwner(user);
}

/**
 * Checks to see if a User is a Guild Admin or Bot Admin
 * @param {Guild} guild - Guild to check admin status in
 * @param {User} user - User to check admin status for
 * @returns {boolean} Whether the user is a Guild Admin or Bot Admin
 */
export function isAdmin(guild, user) {
  return isGuildAdmin(guild, user) || isBotAdmin(user);
}

/**
 * Checks to see if non-Admin Users are ignored in a Guild
 * @param {Guild} guild - Guild to check ignore status in
 * @param {User} user - User to check ignore status for
 * @returns {boolean} Whether the user is ignored
 */
export function isUserIgnored(guild, user) {
  if (isAdmin(guild, user)) {
    return false;
  }

  return aquarius.guildManager.get(guild.id).isUserIgnored(user.id);
}

/**
 * Tages a list of permission flags and generates a message requesting
 * the permissions be granted
 * @param {number[]} permissions - array of permission flags
 * @returns {string} Request message for the given permission flags
 */
export function getRequestMessage(permissions) {
  return `I need the ${humanize(
    permissions.map(getPermissionName).sort()
  )} ${pluralize('permission', permissions.length)} in order to do that!`;
}
