harmony/src/structures/textChannel.ts
2020-11-08 13:27:24 +05:30

84 lines
2.5 KiB
TypeScript

import { Client } from '../models/client.ts'
import { MessageOption, TextChannelPayload } from '../types/channel.ts'
import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts'
import { Channel } from './channel.ts'
import { Embed } from './embed.ts'
import { Message } from './message.ts'
import { MessageMentions } from './messageMentions.ts'
type AllMessageOptions = MessageOption | Embed
export class TextChannel extends Channel {
lastMessageID?: string
lastPinTimestamp?: string
constructor (client: Client, data: TextChannelPayload) {
super(client, data)
this.lastMessageID = data.last_message_id
this.lastPinTimestamp = data.last_pin_timestamp
// TODO: Cache in Gateway Event Code
// cache.set('textchannel', this.id, this)
}
protected readFromData (data: TextChannelPayload): void {
super.readFromData(data)
this.lastMessageID = data.last_message_id ?? this.lastMessageID
this.lastPinTimestamp = data.last_pin_timestamp ?? this.lastPinTimestamp
}
async send (text?: string | AllMessageOptions, option?: AllMessageOptions): Promise<Message> {
if (typeof text === "object") {
option = text
text = undefined
}
if (text === undefined && option === undefined) {
throw new Error('Either text or option is necessary.')
}
if (option instanceof Embed) option = {
embed: option
}
const resp = await this.client.rest.post(CHANNEL_MESSAGES(this.id), {
content: text,
embed: option?.embed,
file: option?.file,
tts: option?.tts,
allowed_mentions: option?.allowedMention
})
return new Message(this.client, resp as any, this, this.client.user as any, new MessageMentions())
}
async edit (
message: Message | string,
text?: string,
option?: MessageOption
): Promise<Message> {
if (text === undefined && option === undefined) {
throw new Error('Either text or option is necessary.')
}
if (this.client.user === undefined) {
throw new Error('Client user has not initialized.')
}
const newMsg = await this.client.rest.patch(
CHANNEL_MESSAGE(
this.id,
typeof message === 'string' ? message : message.id
),
{
content: text,
embed: option?.embed.toJSON(),
file: option?.file,
tts: option?.tts,
allowed_mentions: option?.allowedMention
}
)
// TODO: Actually construct this object
const mentions = new MessageMentions()
return new Message(this.client, newMsg, this, this.client.user, mentions)
}
}