Merge pull request #78 from DjDeveloperr/slash

feat: new rest methods
This commit is contained in:
Helloyunho 2020-12-29 15:26:56 +09:00 committed by GitHub
commit 41891b45c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 101 additions and 11 deletions

View File

@ -37,10 +37,15 @@ export class BaseManager<T, T2> {
}
/** Deletes a key from Cache */
async delete(key: string): Promise<boolean> {
async _delete(key: string): Promise<boolean> {
return this.client.cache.delete(this.cacheName, key)
}
/** Alias to _delete (cache) for compatibility purposes */
async delete(key: string): Promise<boolean> {
return await this._delete(key)
}
/** Gets an Array of values from Cache */
async array(): Promise<T2[]> {
let arr = await (this.client.cache.array(this.cacheName) as T[])

View File

@ -36,6 +36,7 @@ export class GuildChannelsManager extends BaseChildManager<
else return undefined
}
/** Delete a Guild Channel */
async delete(id: string): Promise<boolean> {
return this.client.rest.delete(CHANNEL(id))
}

View File

@ -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<InvitePayload, Invite> {
return new Invite(this.client, raw)
}
async fetch(id: string): Promise<Invite | undefined> {
/** Fetch an Invite */
async fetch(id: string): Promise<Invite> {
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))
})

View File

@ -60,7 +60,7 @@ export class MemberRolesManager extends BaseChildManager<RolePayload, Role> {
true
)
return res.status === 204
return res.response.status === 204
}
async remove(role: string | Role): Promise<boolean> {
@ -76,6 +76,6 @@ export class MemberRolesManager extends BaseChildManager<RolePayload, Role> {
true
)
return res.status === 204
return res.response.status === 204
}
}

View File

@ -58,6 +58,7 @@ export class MembersManager extends BaseManager<MemberPayload, Member> {
)
}
/** Fetch a Guild Member */
async fetch(id: string): Promise<Member> {
return await new Promise((resolve, reject) => {
this.client.rest

View File

@ -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<void> {
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<MessageReactionsManager> {
const val = encodeURIComponent(
(typeof emoji === 'object' ? emoji.id ?? emoji.name : emoji) as string
)
await this.client.rest.delete(
MESSAGE_REACTION(this.message.channel.id, this.message.id, val)
)
return this
}
}

View File

@ -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<RolePayload, Role> {
guild: Guild
@ -13,13 +22,14 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
this.guild = guild
}
/** Fetch a Guild Role (from API) */
async fetch(id: string): Promise<Role> {
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<RolePayload, Role> {
}
return true
}
/** Create a Guild Role */
async create(data?: CreateGuildRoleOptions): Promise<Role> {
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<boolean> {
await this.client.rest.delete(
GUILD_ROLE(this.guild.id, typeof role === 'object' ? role.id : role)
)
return true
}
}

View File

@ -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'
import { ClientEvents } from '../gateway/handlers/index.ts'
/** OS related properties sent with Gateway Identify */
@ -229,6 +231,18 @@ export class Client extends EventEmitter {
return new Application(this, app)
}
/** Fetch an Invite */
async fetchInvite(id: string): Promise<Invite> {
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.