y21

Pylon PermissionOverwritesBuilder

y21
Apr 17th, 2020
355
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export enum ModifyType {
  2.   DENY = 'deny',
  3.   ALLOW = 'allow'
  4. }
  5.  
  6. export default class PermissionOverwritesBuilder {
  7.   public overwrites: discord.Channel.IPermissionOverwrite[];
  8.  
  9.   constructor(overwrites?: discord.Channel.IPermissionOverwrite[]) {
  10.     this.overwrites = overwrites || [];
  11.   }
  12.  
  13.   private modify(
  14.     modifyType: ModifyType,
  15.     overwriteType: discord.Channel.PermissionOverwriteType,
  16.     id: string,
  17.     permissions: number
  18.   ): PermissionOverwritesBuilder {
  19.     const overwrite = this.overwrites.find(
  20.       (v) => v.type === overwriteType && v.id === id
  21.     );
  22.  
  23.     if (
  24.       overwrite &&
  25.       ((overwrite.deny & permissions) === permissions ||
  26.         (overwrite.allow & permissions) === permissions)
  27.     ) {
  28.       throw new Error('Permission has already been denied or allowed');
  29.     }
  30.  
  31.     if (overwrite && !overwrite.hasOwnProperty(modifyType)) {
  32.       throw new Error("modifyType must be either 'deny' or 'allow'");
  33.     }
  34.  
  35.     if (overwrite) {
  36.       overwrite[modifyType] |= permissions;
  37.     } else {
  38.       const newOverwrite: discord.Channel.IPermissionOverwrite = {
  39.         allow: 0,
  40.         deny: 0,
  41.         id,
  42.         type: overwriteType
  43.       };
  44.  
  45.       if (modifyType === ModifyType.ALLOW) {
  46.         newOverwrite.allow = permissions;
  47.       } else {
  48.         newOverwrite.deny = permissions;
  49.       }
  50.  
  51.       this.overwrites.push(newOverwrite);
  52.     }
  53.  
  54.     return this;
  55.   }
  56.  
  57.   public denyForRole(
  58.     roleId: string,
  59.     permissions: number
  60.   ): PermissionOverwritesBuilder {
  61.     return this.modify(
  62.       ModifyType.DENY,
  63.       discord.Channel.PermissionOverwriteType.ROLE,
  64.       roleId,
  65.       permissions
  66.     );
  67.   }
  68.  
  69.   public denyForMember(memberId: string, permissions: number) {
  70.     return this.modify(
  71.       ModifyType.DENY,
  72.       discord.Channel.PermissionOverwriteType.MEMBER,
  73.       memberId,
  74.       permissions
  75.     );
  76.   }
  77.  
  78.   public allowForRole(
  79.     roleId: string,
  80.     permissions: number
  81.   ): PermissionOverwritesBuilder {
  82.     return this.modify(
  83.       ModifyType.ALLOW,
  84.       discord.Channel.PermissionOverwriteType.ROLE,
  85.       roleId,
  86.       permissions
  87.     );
  88.   }
  89.  
  90.   public allowForMember(memberId: string, permissions: number) {
  91.     return this.modify(
  92.       ModifyType.ALLOW,
  93.       discord.Channel.PermissionOverwriteType.MEMBER,
  94.       memberId,
  95.       permissions
  96.     );
  97.   }
  98.  
  99.   public formatAsOverwrites(): discord.Channel.IPermissionOverwrite[] {
  100.     return this.overwrites;
  101.   }
  102. }
RAW Paste Data