commit
ce444630d0
3 changed files with 38 additions and 26 deletions
|
@ -24,6 +24,10 @@ export interface RequestHeaders {
|
||||||
[name: string]: string
|
[name: string]: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class DiscordAPIError extends Error {
|
||||||
|
name = 'DiscordAPIError'
|
||||||
|
}
|
||||||
|
|
||||||
export interface QueuedItem {
|
export interface QueuedItem {
|
||||||
bucket?: string | null
|
bucket?: string | null
|
||||||
url: string
|
url: string
|
||||||
|
@ -263,7 +267,7 @@ export class RESTManager {
|
||||||
if (text === 'undefined') text = undefined
|
if (text === 'undefined') text = undefined
|
||||||
|
|
||||||
if (status === HttpResponseCode.Unauthorized)
|
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
|
// At this point we know it is error
|
||||||
let error = { url: response.url, status, method: data.method, body: data.body }
|
let error = { url: response.url, status, method: data.method, body: data.body }
|
||||||
|
@ -275,10 +279,10 @@ export class RESTManager {
|
||||||
HttpResponseCode.Forbidden,
|
HttpResponseCode.Forbidden,
|
||||||
HttpResponseCode.MethodNotAllowed
|
HttpResponseCode.MethodNotAllowed
|
||||||
].includes(status)) {
|
].includes(status)) {
|
||||||
throw new Error(Deno.inspect(error))
|
throw new DiscordAPIError(Deno.inspect(error))
|
||||||
} else if (status === HttpResponseCode.GatewayUnavailable) {
|
} else if (status === HttpResponseCode.GatewayUnavailable) {
|
||||||
throw new Error(Deno.inspect(error))
|
throw new DiscordAPIError(Deno.inspect(error))
|
||||||
} else throw new Error('Request - Unknown Error')
|
} else throw new DiscordAPIError('Request - Unknown Error')
|
||||||
}
|
}
|
||||||
|
|
||||||
async make(
|
async make(
|
||||||
|
@ -327,8 +331,7 @@ export class RESTManager {
|
||||||
if (response.status === 204) return resolve(undefined)
|
if (response.status === 204) return resolve(undefined)
|
||||||
|
|
||||||
const json: any = await response.json()
|
const json: any = await response.json()
|
||||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
await this.handleStatusCode(response, json, requestData)
|
||||||
this.handleStatusCode(response, json, requestData)
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
json.retry_after !== undefined ||
|
json.retry_after !== undefined ||
|
||||||
|
|
|
@ -16,7 +16,6 @@ import { Embed } from './embed.ts'
|
||||||
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
||||||
import { MessageMentions } from './messageMentions.ts'
|
import { MessageMentions } from './messageMentions.ts'
|
||||||
import { TextChannel } from './textChannel.ts'
|
import { TextChannel } from './textChannel.ts'
|
||||||
import { DMChannel } from './dmChannel.ts'
|
|
||||||
import { Guild } from './guild.ts'
|
import { Guild } from './guild.ts'
|
||||||
|
|
||||||
type AllMessageOptions = MessageOption | Embed
|
type AllMessageOptions = MessageOption | Embed
|
||||||
|
@ -110,10 +109,9 @@ export class Message extends Base {
|
||||||
return this.channel.editMessage(this.id, text, option)
|
return this.channel.editMessage(this.id, text, option)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** These will **not** work in all servers, as this feature is coming slowly. */
|
||||||
async reply(text?: string | AllMessageOptions, option?: AllMessageOptions): Promise<Message> {
|
async reply(text?: string | AllMessageOptions, option?: AllMessageOptions): Promise<Message> {
|
||||||
// TODO: Use inline replies once they're out
|
return this.channel.send(text, option, this)
|
||||||
if (this.channel instanceof DMChannel) return this.channel.send(text, option)
|
|
||||||
return this.channel.send(`${this.author.mention}, ${text}`, option)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete (): Promise<void> {
|
async delete (): Promise<void> {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { MessagesManager } from "../../mod.ts"
|
import { MessagesManager } from "../../mod.ts"
|
||||||
import { Client } from '../models/client.ts'
|
import { Client } from '../models/client.ts'
|
||||||
import { GuildTextChannelPayload, MessageOption, Overwrite, TextChannelPayload } from '../types/channel.ts'
|
import { GuildTextChannelPayload, MessageOption, MessageReference, Overwrite, TextChannelPayload } from '../types/channel.ts'
|
||||||
import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts'
|
import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts'
|
||||||
import { Channel } from './channel.ts'
|
import { Channel } from './channel.ts'
|
||||||
import { Embed } from './embed.ts'
|
import { Embed } from './embed.ts'
|
||||||
|
@ -27,7 +27,7 @@ export class TextChannel extends Channel {
|
||||||
this.lastPinTimestamp = data.last_pin_timestamp ?? this.lastPinTimestamp
|
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") {
|
if (typeof text === "object") {
|
||||||
option = text
|
option = text
|
||||||
text = undefined
|
text = undefined
|
||||||
|
@ -39,13 +39,24 @@ export class TextChannel extends Channel {
|
||||||
embed: option
|
embed: option
|
||||||
}
|
}
|
||||||
|
|
||||||
const resp = await this.client.rest.post(CHANNEL_MESSAGES(this.id), {
|
const payload: any = {
|
||||||
content: text,
|
content: text,
|
||||||
embed: option?.embed,
|
embed: option?.embed,
|
||||||
file: option?.file,
|
file: option?.file,
|
||||||
tts: option?.tts,
|
tts: option?.tts,
|
||||||
allowed_mentions: option?.allowedMention
|
allowed_mentions: option?.allowedMention
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if (reply !== undefined) {
|
||||||
|
const reference: MessageReference = {
|
||||||
|
message_id: reply.id,
|
||||||
|
channel_id: reply.channel.id,
|
||||||
|
guild_id: reply.guild?.id,
|
||||||
|
}
|
||||||
|
payload.message_reference = reference
|
||||||
|
}
|
||||||
|
|
||||||
|
const resp = await this.client.rest.post(CHANNEL_MESSAGES(this.id), payload)
|
||||||
|
|
||||||
const res = new Message(this.client, resp, this, this.client.user as any)
|
const res = new Message(this.client, resp, this, this.client.user as any)
|
||||||
await res.mentions.fromPayload(resp)
|
await res.mentions.fromPayload(resp)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue