fix(Message): Fix inline replies; feat(RESTManager): handle status codes correctly

This commit is contained in:
DjDeveloperr 2020-11-17 09:05:25 +05:30
parent 86f82715ca
commit 25a5025802
4 changed files with 16 additions and 14 deletions

View File

@ -24,6 +24,10 @@ export interface RequestHeaders {
[name: string]: string
}
export class DiscordAPIError extends Error {
name = 'DiscordAPIError'
}
export interface QueuedItem {
bucket?: string | null
url: string
@ -263,7 +267,7 @@ export class RESTManager {
if (text === 'undefined') text = undefined
if (status === HttpResponseCode.Unauthorized)
throw new Error(`Request was not successful (Unauthorized). Invalid Token.\n${text}`)
throw new DiscordAPIError(`Request was not successful (Unauthorized). Invalid Token.\n${text}`)
// At this point we know it is error
let error = { url: response.url, status, method: data.method, body: data.body }
@ -275,10 +279,10 @@ export class RESTManager {
HttpResponseCode.Forbidden,
HttpResponseCode.MethodNotAllowed
].includes(status)) {
throw new Error(Deno.inspect(error))
throw new DiscordAPIError(Deno.inspect(error))
} else if (status === HttpResponseCode.GatewayUnavailable) {
throw new Error(Deno.inspect(error))
} else throw new Error('Request - Unknown Error')
throw new DiscordAPIError(Deno.inspect(error))
} else throw new DiscordAPIError('Request - Unknown Error')
}
async make(
@ -327,8 +331,7 @@ export class RESTManager {
if (response.status === 204) return resolve(undefined)
const json: any = await response.json()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.handleStatusCode(response, json, requestData)
await this.handleStatusCode(response, json, requestData)
if (
json.retry_after !== undefined ||

View File

@ -111,7 +111,7 @@ export class Message extends Base {
/** These will **not** work in all servers, as this feature is coming slowly. */
async reply(text?: string | AllMessageOptions, option?: AllMessageOptions): Promise<Message> {
return this.channel.send(text, Object.assign(option, { reply: this }))
return this.channel.send(text, option, this)
}
async delete (): Promise<void> {

View File

@ -27,7 +27,7 @@ export class TextChannel extends Channel {
this.lastPinTimestamp = data.last_pin_timestamp ?? this.lastPinTimestamp
}
async send(text?: string | AllMessageOptions, option?: AllMessageOptions): Promise<Message> {
async send(text?: string | AllMessageOptions, option?: AllMessageOptions, reply?: Message): Promise<Message> {
if (typeof text === "object") {
option = text
text = undefined
@ -47,11 +47,11 @@ export class TextChannel extends Channel {
allowed_mentions: option?.allowedMention
}
if (option?.reply !== undefined) {
if (reply !== undefined) {
const reference: MessageReference = {
message_id: option.reply.id,
channel_id: option.reply.channel.id,
guild_id: option.reply.guild?.id,
message_id: reply.id,
channel_id: reply.channel.id,
guild_id: reply.guild?.id,
}
payload.message_reference = reference
}

View File

@ -106,8 +106,7 @@ export interface MessageOption {
parse: ['everyone', 'users', 'roles']
roles: string[]
users: string[]
},
reply?: Message
}
}
export interface ChannelMention {