async function calculateChannelOverwrites(
  channelOrId,
  memberOrId,
) {
  const channel = await getCached("channels", channelOrId);
  // This is a DM channel so return ADMINISTRATOR permission
  if (!channel?.guildId) return "8";
  const member = await getCached("members", memberOrId);
  if (!channel || !member) return "8";
  // Get all the role permissions this member already has
  let permissions = BigInt(
    await calculateBasePermissions(channel.guildId, member),
  );
  // First calculate @everyone overwrites since these have the lowest priority
  const overwriteEveryone = channel.permissionOverwrites?.find((overwrite) =>
    overwrite.id === channel.guildId
  );
  if (overwriteEveryone) {
    // First remove denied permissions since denied < allowed
    permissions &= ~BigInt(overwriteEveryone.deny);
    permissions |= BigInt(overwriteEveryone.allow);
  }
  const overwrites = channel.permissionOverwrites;
  // In order to calculate the role permissions correctly we need to temporarily save the allowed and denied permissions
  let allow = 0n;
  let deny = 0n;
  const memberRoles = member.guilds.get(channel.guildId)?.roles || [];
  // Second calculate members role overwrites since these have middle priority
  for (const overwrite of overwrites || []) {
    if (!memberRoles.includes(overwrite.id)) continue;
    deny |= BigInt(overwrite.deny);
    allow |= BigInt(overwrite.allow);
  }
  // After role overwrite calculate save allowed permissions first we remove denied permissions since "denied < allowed"
  permissions &= ~deny;
  permissions |= allow;
  // Third calculate member specific overwrites since these have the highest priority
  const overwriteMember = overwrites?.find((overwrite) =>
    overwrite.id === member.id
  );
  if (overwriteMember) {
    permissions &= ~BigInt(overwriteMember.deny);
    permissions |= BigInt(overwriteMember.allow);
  }
  return permissions.toString();
}
/** Calculates the permissions this member has for the given Channel */
Calculates the permissions this member has for the given Channel
Calculates the permissions this member has for the given Channel
async function calculateChannelOverwrites(
  channelOrId,
  memberOrId,
) {
  const channel = await getCached("channels", channelOrId);
  if (!channel?.guildId) return "8";
  const member = await getCached("members", memberOrId);
  if (!channel || !member) return "8";
  let permissions = BigInt(
    await calculateBasePermissions(channel.guildId, member),
  );
  const overwriteEveryone = channel.permissionOverwrites?.find((overwrite) =>
    overwrite.id === channel.guildId
  );
  if (overwriteEveryone) {
    permissions &= ~BigInt(overwriteEveryone.deny);
    permissions |= BigInt(overwriteEveryone.allow);
  }
  const overwrites = channel.permissionOverwrites;
  let allow = 0n;
  let deny = 0n;
  const memberRoles = member.guilds.get(channel.guildId)?.roles || [];
  for (const overwrite of overwrites || []) {
    if (!memberRoles.includes(overwrite.id)) continue;
    deny |= BigInt(overwrite.deny);
    allow |= BigInt(overwrite.allow);
  }
  permissions &= ~deny;
  permissions |= allow;
  const overwriteMember = overwrites?.find((overwrite) =>
    overwrite.id === member.id
  );
  if (overwriteMember) {
    permissions &= ~BigInt(overwriteMember.deny);
    permissions |= BigInt(overwriteMember.allow);
  }
  return permissions.toString();
}
async function calculateChannelOverwrites ( channelOrId , memberOrId , ) { const channel = await getCached ( "channels" , channelOrId ) ; if ( ! channel ?. guildId ) return "8" ; const member = await getCached ( "members" , memberOrId ) ; if ( ! channel || ! member ) return "8" ; let permissions = BigInt ( await calculateBasePermissions ( channel . guildId , member ) , ) ; const overwriteEveryone = channel . permissionOverwrites ?. find ( ( overwrite ) => overwrite . id === channel . guildId ) ; if ( overwriteEveryone ) { permissions &= ~ BigInt ( overwriteEveryone . deny ) ; permissions |= BigInt ( overwriteEveryone . allow ) ; } const overwrites = channel . permissionOverwrites ; let allow = 0n ; let deny = 0n ; const memberRoles = member . guilds . get ( channel . guildId ) ?. roles || [ ] ; for ( const overwrite of overwrites || [ ] ) { if ( ! memberRoles . includes ( overwrite . id ) ) continue ; deny |= BigInt ( overwrite . deny ) ; allow |= BigInt ( overwrite . allow ) ; } permissions &= ~ deny ; permissions |= allow ; const overwriteMember = overwrites ?. find ( ( overwrite ) => overwrite . id === member . id ) ; if ( overwriteMember ) { permissions &= ~ BigInt ( overwriteMember . deny ) ; permissions |= BigInt ( overwriteMember . allow ) ; } return permissions . toString ( ) ; }
Calculates the permissions this member has for the given Channel
Calculates the permissions this member has for the given Channel
// This is a DM channel so return ADMINISTRATOR permission // Get all the role permissions this member already has // First calculate @everyone overwrites since these have the lowest priority // First remove denied permissions since denied < allowed // In order to calculate the role permissions correctly we need to temporarily save the allowed and denied permissions // Second calculate members role overwrites since these have middle priority // After role overwrite calculate save allowed permissions first we remove denied permissions since "denied < allowed" // Third calculate member specific overwrites since these have the highest priority