2020-07-26 10:02:35 +00:00
import { GuildMember , Permissions } from "discord.js" ;
import { Config } from "./structures" ;
import $ from "./lib" ;
export enum PERMISSIONS { NONE , MOD , ADMIN , OWNER , BOT_SUPPORT , BOT_ADMIN , BOT_OWNER } ;
export const PermissionNames = [ "User" , "Moderator" , "Administrator" , "Server Owner" , "Bot Support" , "Bot Admin" , "Bot Owner" ] ;
// Here is where you enter in the functions that check for permissions.
const PermissionChecker : ( ( member : GuildMember ) = > boolean ) [ ] = [
// NONE //
( ) = > true ,
// MOD //
2020-08-30 21:26:18 +00:00
member = >
2020-07-26 10:02:35 +00:00
member . hasPermission ( Permissions . FLAGS . MANAGE_ROLES ) ||
member . hasPermission ( Permissions . FLAGS . MANAGE_MESSAGES ) ||
member . hasPermission ( Permissions . FLAGS . KICK_MEMBERS ) ||
member . hasPermission ( Permissions . FLAGS . BAN_MEMBERS ) ,
// ADMIN //
2020-08-30 21:26:18 +00:00
member = > member . hasPermission ( Permissions . FLAGS . ADMINISTRATOR ) ,
2020-07-26 10:02:35 +00:00
// OWNER //
2020-08-30 21:26:18 +00:00
member = > member . guild . ownerID === member . id ,
2020-07-26 10:02:35 +00:00
// BOT_SUPPORT //
2020-08-30 21:26:18 +00:00
member = > Config . support . includes ( member . id ) ,
2020-07-26 10:02:35 +00:00
// BOT_ADMIN //
2020-08-30 21:26:18 +00:00
member = > Config . admins . includes ( member . id ) ,
2020-07-26 10:02:35 +00:00
// BOT_OWNER //
2020-08-30 21:26:18 +00:00
member = > Config . owner === member . id
2020-07-26 10:02:35 +00:00
] ;
// After checking the lengths of these three objects, use this as the length for consistency.
const length = Object . keys ( PERMISSIONS ) . length / 2 ;
export function hasPermission ( member : GuildMember , permission : PERMISSIONS ) : boolean
{
for ( let i = length - 1 ; i >= permission ; i -- )
2020-08-14 12:50:24 +00:00
if ( PermissionChecker [ i ] ( member ) )
2020-07-26 10:02:35 +00:00
return true ;
return false ;
}
export function getPermissionLevel ( member : GuildMember ) : number
{
for ( let i = length - 1 ; i >= 0 ; i -- )
2020-08-14 12:50:24 +00:00
if ( PermissionChecker [ i ] ( member ) )
2020-07-26 10:02:35 +00:00
return i ;
return 0 ;
}
// Length Checking
( ( ) = > {
const lenNames = PermissionNames . length ;
const lenChecker = PermissionChecker . length ;
// By transitive property, lenNames and lenChecker have to be equal to each other as well.
if ( length !== lenNames || length !== lenChecker )
$ . error ( ` Permission object lengths aren't equal! Enum Length ( ${ length } ), Names Length ( ${ lenNames } ), and Functions Length ( ${ lenChecker } ). This WILL cause problems! ` ) ;
} ) ( )