diff --git a/src/gateway/handlers/channelPinsUpdate.ts b/src/gateway/handlers/channelPinsUpdate.ts index efbe2d2..f1fe526 100644 --- a/src/gateway/handlers/channelPinsUpdate.ts +++ b/src/gateway/handlers/channelPinsUpdate.ts @@ -12,7 +12,7 @@ export const channelPinsUpdate: GatewayEventHandler = ( const before = after.refreshFromData({ last_pin_timestamp: d.last_pin_timestamp }) - let raw = gateway.client.channels._get(d.channel_id) as ChannelPayload; + const raw = gateway.client.channels._get(d.channel_id) ; gateway.client.channels.set(after.id, Object.assign(raw, { last_pin_timestamp: d.last_pin_timestamp })) gateway.client.emit('channelPinsUpdate', before, after) } diff --git a/src/gateway/index.ts b/src/gateway/index.ts index a0aa1b2..247e1f7 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -142,7 +142,7 @@ class Gateway { private async sendIdentify () { this.debug("Fetching /gateway/bot...") - let info = await this.client.rest.get(GATEWAY_BOT()) as GatewayBotPayload + const info = await this.client.rest.get(GATEWAY_BOT()) as GatewayBotPayload if(info.session_start_limit.remaining == 0) throw new Error("Session Limit Reached. Retry After " + info.session_start_limit.reset_after + "ms") this.debug("Recommended Shards: " + info.shards) this.debug("=== Session Limit Info ===") diff --git a/src/managers/BaseManager.ts b/src/managers/BaseManager.ts index 30b8e03..fef9e46 100644 --- a/src/managers/BaseManager.ts +++ b/src/managers/BaseManager.ts @@ -17,7 +17,7 @@ export class BaseManager { } get(key: string): T2 | void { - let raw = this._get(key) + const raw = this._get(key) if(!raw) return return new this.dataType(this.client, raw) as any } diff --git a/src/managers/MessagesManager.ts b/src/managers/MessagesManager.ts new file mode 100644 index 0000000..472513f --- /dev/null +++ b/src/managers/MessagesManager.ts @@ -0,0 +1,20 @@ +import { Client } from "../models/client.ts"; +import { Message } from "../structures/message.ts"; +import { MessagePayload } from "../types/channelTypes.ts"; +import { CHANNEL_MESSAGE } from "../types/endpoint.ts"; +import { BaseManager } from "./BaseManager.ts"; + +export class MessagesManager extends BaseManager { + constructor(client: Client) { + super(client, "messages", Message) + } + + fetch(channelID: string, id: string) { + return new Promise((res, rej) => { + this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(data => { + this.set(id, data as MessagePayload) + res(new Message(this.client, data as MessagePayload)) + }).catch(e => rej(e)) + }) + } +} \ No newline at end of file diff --git a/src/models/CacheAdapter.ts b/src/models/CacheAdapter.ts index 8768438..087558e 100644 --- a/src/models/CacheAdapter.ts +++ b/src/models/CacheAdapter.ts @@ -20,7 +20,7 @@ export class DefaultCacheAdapter implements ICacheAdapter { } get(cacheName: string, key: string) { - let cache = this.data[cacheName] + const cache = this.data[cacheName] if (!cache) return; return cache.get(key) } @@ -35,13 +35,13 @@ export class DefaultCacheAdapter implements ICacheAdapter { } delete(cacheName: string, key: string) { - let cache = this.data[cacheName] + const cache = this.data[cacheName] if (!cache) return false return cache.delete(key) } array(cacheName: string) { - let cache = this.data[cacheName] + const cache = this.data[cacheName] if (!cache) return return cache.array() } diff --git a/src/models/client.ts b/src/models/client.ts index 4cf1444..074ff21 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -8,6 +8,7 @@ import { UserManager } from "../managers/UsersManager.ts" import { GuildManager } from "../managers/GuildsManager.ts" import { EmojisManager } from "../managers/EmojisManager.ts" import { ChannelsManager } from "../managers/ChannelsManager.ts" +import { MessagesManager } from "../managers/MessagesManager.ts" /** Some Client Options to modify behaviour */ export interface ClientOptions { @@ -27,9 +28,11 @@ export class Client extends EventEmitter { token?: string cache: ICacheAdapter = new DefaultCacheAdapter(this) intents?: GatewayIntents[] + users: UserManager = new UserManager(this) guilds: GuildManager = new GuildManager(this) channels: ChannelsManager = new ChannelsManager(this) + messages: MessagesManager = new MessagesManager(this) emojis: EmojisManager = new EmojisManager(this) constructor (options: ClientOptions = {}) { diff --git a/src/models/rest.ts b/src/models/rest.ts index 5099664..e8824be 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -149,7 +149,7 @@ export class RESTManager { `DiscordBot (discord.deno)`, }; - if(!this.client.token) delete headers["Authorization"]; + if(!this.client.token) delete headers.Authorization; if (method === "get") body = undefined; @@ -201,7 +201,7 @@ export class RESTManager { const errorStack = new Error("Location In Your Files:"); Error.captureStackTrace(errorStack); - return new Promise((resolve, reject) => { + return await new Promise((resolve, reject) => { const callback = async () => { try { const rateLimitResetIn = await this.checkRatelimits(url); @@ -349,15 +349,19 @@ export class RESTManager { get(url: string, body?: unknown) { return this.runMethod("get", url, body); } + post(url: string, body?: unknown) { return this.runMethod("post", url, body); } + delete(url: string, body?: unknown) { return this.runMethod("delete", url, body); } + patch(url: string, body?: unknown) { return this.runMethod("patch", url, body); } + put(url: string, body?: unknown) { return this.runMethod("put", url, body); } diff --git a/src/models/shard.ts b/src/models/shard.ts index bd56586..36fb07e 100644 --- a/src/models/shard.ts +++ b/src/models/shard.ts @@ -1 +1 @@ -//TODO: write code \ No newline at end of file +// TODO: write code \ No newline at end of file diff --git a/src/structures/message.ts b/src/structures/message.ts index 8dca65d..87c1269 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -51,14 +51,14 @@ export class Message extends Base { this.channelID = data.channel_id this.guildID = data.guild_id this.author = - cache.get('user', data.author.id) ?? new User(this.client, data.author) + this.client.users.get(data.author.id) || new User(this.client, data.author) this.content = data.content this.timestamp = data.timestamp this.editedTimestamp = data.edited_timestamp this.tts = data.tts this.mentionEveryone = data.mention_everyone this.mentions = data.mentions.map( - v => cache.get('user', v.id) ?? new User(client, v) + v => this.client.users.get(v.id) || new User(client, v) ) this.mentionRoles = data.mention_roles this.mentionChannels = data.mention_channels @@ -73,7 +73,7 @@ export class Message extends Base { this.application = data.application this.messageReference = data.message_reference this.flags = data.flags - cache.set('message', this.id, this) + this.client.messages.set(this.id, data) } protected readFromData (data: MessagePayload): void { @@ -81,8 +81,8 @@ export class Message extends Base { this.channelID = data.channel_id ?? this.channelID this.guildID = data.guild_id ?? this.guildID this.author = - cache.get('user', data.author.id) ?? - this.author ?? + this.client.users.get(data.author.id) || + this.author || new User(this.client, data.author) this.content = data.content ?? this.content this.timestamp = data.timestamp ?? this.timestamp @@ -91,7 +91,7 @@ export class Message extends Base { this.mentionEveryone = data.mention_everyone ?? this.mentionEveryone this.mentions = data.mentions.map( - v => cache.get('user', v.id) ?? new User(this.client, v) + v => this.client.users.get(v.id) || new User(this.client, v) ) ?? this.mentions this.mentionRoles = data.mention_roles ?? this.mentionRoles this.mentionChannels = data.mention_channels ?? this.mentionChannels @@ -109,43 +109,24 @@ export class Message extends Base { } // TODO: We have to seperate fetch() - async editMessage (text?: string, option?: MessageOption): Promise { + async edit (text?: string, option?: MessageOption): Promise { if (text !== undefined && option !== undefined) { throw new Error('Either text or option is necessary.') } - const resp = await fetch(CHANNEL_MESSAGE(this.channelID, this.id), { - headers: { - Authorization: `Bot ${this.client.token}`, - 'Content-Type': 'application/json' - }, - method: 'PATCH', - body: JSON.stringify({ - content: text, - embed: option?.embed.toJSON(), - file: option?.file, - tts: option?.tts, - allowed_mentions: option?.allowedMention - }) - }) - return new Message(this.client, await resp.json()) + let newMsg = await this.client.rest.patch(CHANNEL_MESSAGE(this.channelID, this.id), { + content: text, + embed: option?.embed.toJSON(), + file: option?.file, + tts: option?.tts, + allowed_mentions: option?.allowedMention + }) as MessagePayload + + return new Message(this.client, newMsg) } // TODO: We have to seperate fetch() - async delete (): Promise { - const resp = await fetch(CHANNEL_MESSAGE(this.channelID, this.id), { - headers: { - Authorization: `Bot ${this.client.token}` - }, - method: 'DELETE' - }) - - // TODO: improve Error and Promise - return await new Promise((resolve, reject) => { - if (resp.status !== 204) { - reject(new Error()) - } - resolve() - }) + delete (): Promise { + return this.client.rest.delete(CHANNEL_MESSAGE(this.channelID, this.id)) as any } } diff --git a/src/utils/collection.ts b/src/utils/collection.ts index fc94d05..58b749e 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -22,7 +22,7 @@ export class Collection extends Map { } random() { - let arr = [...this.values()] + const arr = [...this.values()] return arr[Math.floor(Math.random() * arr.length)] } @@ -32,7 +32,7 @@ export class Collection extends Map { if (callback(value, key)) return value } // If nothing matched - return; + } filter(callback: (value: V, key: K) => boolean) {