diff --git a/src/managers/base.ts b/src/managers/base.ts index 5fef9d2..9d5bd7e 100644 --- a/src/managers/base.ts +++ b/src/managers/base.ts @@ -37,10 +37,15 @@ export class BaseManager { } /** Deletes a key from Cache */ - async delete(key: string): Promise { + async _delete(key: string): Promise { return this.client.cache.delete(this.cacheName, key) } + /** Alias to _delete (cache) for compatibility purposes */ + async delete(key: string): Promise { + return await this._delete(key) + } + /** Gets an Array of values from Cache */ async array(): Promise { let arr = await (this.client.cache.array(this.cacheName) as T[]) diff --git a/src/managers/guildChannels.ts b/src/managers/guildChannels.ts index a6854a8..3bdaed8 100644 --- a/src/managers/guildChannels.ts +++ b/src/managers/guildChannels.ts @@ -36,6 +36,7 @@ export class GuildChannelsManager extends BaseChildManager< else return undefined } + /** Delete a Guild Channel */ async delete(id: string): Promise { return this.client.rest.delete(CHANNEL(id)) } diff --git a/src/managers/invites.ts b/src/managers/invites.ts index 6af3896..e5baf27 100644 --- a/src/managers/invites.ts +++ b/src/managers/invites.ts @@ -1,7 +1,7 @@ import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' import { Invite } from '../structures/invite.ts' -import { GUILD_INVITES } from '../types/endpoint.ts' +import { INVITE } from '../types/endpoint.ts' import { InvitePayload } from '../types/invite.ts' import { BaseManager } from './base.ts' @@ -19,14 +19,15 @@ export class InviteManager extends BaseManager { return new Invite(this.client, raw) } - async fetch(id: string): Promise { + /** Fetch an Invite */ + async fetch(id: string): Promise { return await new Promise((resolve, reject) => { this.client.rest - .get(GUILD_INVITES(this.guild.id)) + .get(INVITE(id)) .then(async (data) => { this.set(id, data as InvitePayload) const newInvite = await this.get(data.code) - resolve(newInvite) + resolve(newInvite as Invite) }) .catch((e) => reject(e)) }) diff --git a/src/managers/memberRoles.ts b/src/managers/memberRoles.ts index bb715e0..a405340 100644 --- a/src/managers/memberRoles.ts +++ b/src/managers/memberRoles.ts @@ -60,7 +60,7 @@ export class MemberRolesManager extends BaseChildManager { true ) - return res.status === 204 + return res.response.status === 204 } async remove(role: string | Role): Promise { @@ -76,6 +76,6 @@ export class MemberRolesManager extends BaseChildManager { true ) - return res.status === 204 + return res.response.status === 204 } } diff --git a/src/managers/members.ts b/src/managers/members.ts index b06bb06..200e890 100644 --- a/src/managers/members.ts +++ b/src/managers/members.ts @@ -58,6 +58,7 @@ export class MembersManager extends BaseManager { ) } + /** Fetch a Guild Member */ async fetch(id: string): Promise { return await new Promise((resolve, reject) => { this.client.rest diff --git a/src/managers/messageReactions.ts b/src/managers/messageReactions.ts index 7c43ae6..baccc4c 100644 --- a/src/managers/messageReactions.ts +++ b/src/managers/messageReactions.ts @@ -4,6 +4,7 @@ import { Guild } from '../structures/guild.ts' import { Message } from '../structures/message.ts' import { MessageReaction } from '../structures/messageReaction.ts' import { Reaction } from '../types/channel.ts' +import { MESSAGE_REACTION, MESSAGE_REACTIONS } from '../types/endpoint.ts' import { BaseManager } from './base.ts' export class MessageReactionsManager extends BaseManager< @@ -58,4 +59,22 @@ export class MessageReactionsManager extends BaseManager< await this.client.cache.deleteCache(`reaction_users:${this.message.id}`) return this.client.cache.deleteCache(this.cacheName) } + + /** Remove all Reactions from the Message */ + async removeAll(): Promise { + await this.client.rest.delete( + MESSAGE_REACTIONS(this.message.channel.id, this.message.id) + ) + } + + /** Remove a specific Emoji from Reactions */ + async removeEmoji(emoji: Emoji | string): Promise { + const val = encodeURIComponent( + typeof emoji === 'object' ? emoji.id ?? emoji.name : emoji + ) + await this.client.rest.delete( + MESSAGE_REACTION(this.message.channel.id, this.message.id, val) + ) + return this + } } diff --git a/src/managers/roles.ts b/src/managers/roles.ts index 437c6c0..0ad93ef 100644 --- a/src/managers/roles.ts +++ b/src/managers/roles.ts @@ -1,10 +1,19 @@ +import { Permissions } from '../../mod.ts' import { Client } from '../models/client.ts' import { Guild } from '../structures/guild.ts' import { Role } from '../structures/role.ts' -import { GUILD_ROLE } from '../types/endpoint.ts' +import { GUILD_ROLE, GUILD_ROLES } from '../types/endpoint.ts' import { RolePayload } from '../types/role.ts' import { BaseManager } from './base.ts' +export interface CreateGuildRoleOptions { + name?: string + permissions?: number | string | Permissions + color?: number | string + hoist?: boolean + mentionable?: boolean +} + export class RolesManager extends BaseManager { guild: Guild @@ -13,13 +22,14 @@ export class RolesManager extends BaseManager { this.guild = guild } + /** Fetch a Guild Role (from API) */ async fetch(id: string): Promise { return await new Promise((resolve, reject) => { this.client.rest .get(GUILD_ROLE(this.guild.id, id)) - .then((data) => { - this.set(id, data as RolePayload) - resolve(new Role(this.client, data as RolePayload)) + .then(async (data) => { + await this.set(id, data as RolePayload) + resolve(((await this.get(id)) as unknown) as Role) }) .catch((e) => reject(e)) }) @@ -31,4 +41,43 @@ export class RolesManager extends BaseManager { } return true } + + /** Create a Guild Role */ + async create(data?: CreateGuildRoleOptions): Promise { + if (typeof data?.color === 'string') { + if (data.color.startsWith('#')) data.color = data.color.slice(1) + } + + const roleRaw = ((await this.client.rest.post(GUILD_ROLES(this.guild.id), { + name: data?.name, + permissions: + data?.permissions === undefined + ? undefined + : (typeof data.permissions === 'object' + ? data.permissions.bitfield + : data.permissions + ).toString(), + color: + data?.color === undefined + ? undefined + : typeof data.color === 'string' + ? isNaN(parseInt(data.color, 16)) + ? 0 + : parseInt(data.color, 16) + : data.color, + hoist: data?.hoist ?? false, + mentionable: data?.mentionable ?? false + })) as unknown) as RolePayload + + await this.set(roleRaw.id, roleRaw) + return ((await this.get(roleRaw.id)) as unknown) as Role + } + + /** Delete a Guild Role */ + async delete(role: Role | string): Promise { + await this.client.rest.delete( + GUILD_ROLE(this.guild.id, typeof role === 'object' ? role.id : role) + ) + return true + } } diff --git a/src/models/client.ts b/src/models/client.ts index f0bcc73..ac96ab8 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -17,6 +17,8 @@ import { Interaction } from '../structures/slash.ts' import { SlashModule } from './slashModule.ts' import type { ShardManager } from './shard.ts' import { Application } from '../structures/application.ts' +import { Invite } from '../structures/invite.ts' +import { INVITE } from '../types/endpoint.ts' /** OS related properties sent with Gateway Identify */ export interface ClientProperties { @@ -211,6 +213,18 @@ export class Client extends EventEmitter { return new Application(this, app) } + /** Fetch an Invite */ + async fetch(id: string): Promise { + return await new Promise((resolve, reject) => { + this.rest + .get(INVITE(id)) + .then((data) => { + resolve(new Invite(this, data)) + }) + .catch((e) => reject(e)) + }) + } + /** * This function is used for connecting to discord. * @param token Your token. This is required.