role stuff

This commit is contained in:
Skillz4Killz
2020-12-15 17:52:58 -05:00
parent 7559ad68cc
commit 2f14cf75ac
+60 -3
View File
@@ -1,13 +1,57 @@
import { Permission, RoleData, Unpromise } from "../types/types.ts";
import { deleteRole, editRole } from "../handlers/guild.ts";
import { CreateRoleOptions, RoleData } from "../types/types.ts";
import { cache } from "../utils/cache.ts";
import { Collection } from "../utils/collection.ts";
import { createNewProp } from "../utils/utils.ts";
import { Guild } from "./guild.ts";
import { Member } from "./member.ts";
const baseRole: any = {
const baseRole: Partial<Role> = {
get guild() {
return cache.guilds.find(g => g.roles.has(this.id));
},
get hexColor() {
return this.color.toString(16);
return this.color!.toString(16);
},
get members() {
return cache.members.filter(m => m.guilds.some(g => g.roles.includes(this.id)));
},
get mention() {
return `<@&${this.id}>`;
},
// METHODS
delete: function (guildID?: string) {
// If not guild id was provided try and find one
if (!guildID) guildID = guildID || this.guild?.id;
// If a guild id is still not available error out
if (!guildID) throw new Error("role.delete() did not find a valid guild in cache. Please provide the guildID like role.delete(guildID)")
return deleteRole(guildID, this.id!).catch(console.error);
},
edit(options: CreateRoleOptions, guildID?: string) {
// If not guild id was provided try and find one
if (!guildID) guildID = guildID || this.guild?.id;
// If a guild id is still not available error out
if (!guildID) throw new Error("role.edit() did not find a valid guild in cache. Please provide the guildID like role.edit({}, guildID)")
return editRole(guildID, this.id!, options);
},
higherThanRoleID(roleID: string, position?: number) {
// If no position try and find one from cache
if (!position) position = this.guild?.roles.get(roleID)?.position;
// If still none error out.
if (!position) throw new Error("role.higherThanRoleID() did not have a position provided and the role or guild was not found in cache. Please provide a position like role.higherThanRoleID(roleID, position)")
// Rare edge case handling
if (this.position === position) {
return this.id! < roleID
}
return this.position! > position;
},
}
export async function createRole(data: RoleData) {
@@ -55,8 +99,21 @@ export interface Role {
// GETTERS
/** The guild where this role is. If undefined, the guild is not cached */
guild?: Guild;
/** The hex color for this role. */
hexColor: string;
/** The cached members that have this role */
members: Collection<string, Member>;
/** The @ mention of the role in a string. */
mention: string;
// METHODS
/** Delete the role */
delete(guildID?: string): Promise<unknown>;
/** Edits the role */
edit(options: CreateRoleOptions): Promise<unknown>;
/** Checks if this role is higher than another role. */
higherThanRoleID(roleID: string, position?: number): boolean;
}