harmony/src/structures/textChannel.ts

178 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-12-02 12:29:52 +00:00
import { MessagesManager } from '../managers/messages.ts'
import { Client } from '../models/client.ts'
import {
MessageOptions,
2020-12-22 10:10:19 +00:00
MessagePayload,
2020-12-02 12:29:52 +00:00
TextChannelPayload
} from '../types/channel.ts'
2020-12-20 09:11:37 +00:00
import {
MESSAGE_REACTION_ME,
MESSAGE_REACTION_USER
2020-12-20 09:11:37 +00:00
} from '../types/endpoint.ts'
2020-12-22 10:10:19 +00:00
import { Collection } from '../utils/collection.ts'
2020-12-02 12:29:52 +00:00
import { Channel } from './channel.ts'
import { Embed } from './embed.ts'
2020-12-24 00:37:57 +00:00
import { Emoji } from './emoji.ts'
import { Member } from './member.ts'
2020-12-02 12:29:52 +00:00
import { Message } from './message.ts'
import { User } from './user.ts'
2020-12-02 12:29:52 +00:00
export type AllMessageOptions = MessageOptions | Embed
2020-12-02 12:29:52 +00:00
/** Channel object for Text Channel type */
2020-12-02 12:29:52 +00:00
export class TextChannel extends Channel {
lastMessageID?: string
lastPinTimestamp?: string
messages: MessagesManager
constructor(client: Client, data: TextChannelPayload) {
super(client, data)
this.messages = new MessagesManager(this.client, this)
this.lastMessageID = data.last_message_id
this.lastPinTimestamp = data.last_pin_timestamp
}
2020-12-03 05:28:20 +00:00
readFromData(data: TextChannelPayload): void {
2020-12-02 12:29:52 +00:00
super.readFromData(data)
this.lastMessageID = data.last_message_id ?? this.lastMessageID
this.lastPinTimestamp = data.last_pin_timestamp ?? this.lastPinTimestamp
}
/**
*
2020-12-25 13:09:29 +00:00
* @param content Text content of the Message to send.
2020-12-02 12:29:52 +00:00
* @param option Various other Message options.
* @param reply Reference to a Message object to reply-to.
*/
async send(
2020-12-25 13:09:29 +00:00
content?: string | AllMessageOptions,
2020-12-02 12:29:52 +00:00
option?: AllMessageOptions,
reply?: Message
): Promise<Message> {
2021-03-19 11:18:11 +00:00
return this.client.channels.sendMessage(
this,
content,
Object.assign(option ?? {}, { reply })
)
2020-12-02 12:29:52 +00:00
}
/**
*
* @param message Message to edit. ID or the Message object itself.
* @param text New text contents of the Message.
* @param option Other options to edit the message.
*/
async editMessage(
message: Message | string,
text?: string,
option?: MessageOptions
2020-12-02 12:29:52 +00:00
): Promise<Message> {
2021-03-19 10:50:16 +00:00
return this.client.channels.editMessage(this, message, text, option)
2020-12-02 12:29:52 +00:00
}
2020-12-24 00:37:57 +00:00
/** Add a reaction to a Message in this Channel */
2020-12-24 00:37:57 +00:00
async addReaction(
message: Message | string,
emoji: Emoji | string
): Promise<void> {
if (emoji instanceof Emoji) {
2021-03-08 10:36:30 +00:00
emoji = `${emoji.name}:${emoji.id}`
} else if (emoji.length > 4) {
if (!isNaN(Number(emoji))) {
const findEmoji = await this.client.emojis.get(emoji)
if (findEmoji !== undefined) emoji = `${findEmoji.name}:${findEmoji.id}`
else throw new Error(`Emoji not found: ${emoji}`)
}
2020-12-24 00:37:57 +00:00
}
2021-03-08 10:36:30 +00:00
if (message instanceof Message) message = message.id
2020-12-24 00:37:57 +00:00
const encodedEmoji = encodeURI(emoji)
await this.client.rest.put(
MESSAGE_REACTION_ME(this.id, message, encodedEmoji)
)
}
/** Remove Reaction from a Message in this Channel */
2020-12-24 00:37:57 +00:00
async removeReaction(
message: Message | string,
emoji: Emoji | string,
user?: User | Member | string
2020-12-24 00:37:57 +00:00
): Promise<void> {
if (emoji instanceof Emoji) {
2021-03-08 10:36:30 +00:00
emoji = `${emoji.name}:${emoji.id}`
} else if (emoji.length > 4) {
if (!isNaN(Number(emoji))) {
const findEmoji = await this.client.emojis.get(emoji)
if (findEmoji !== undefined) emoji = `${findEmoji.name}:${findEmoji.id}`
else throw new Error(`Emoji not found: ${emoji}`)
}
2020-12-24 00:37:57 +00:00
}
2021-03-08 10:36:30 +00:00
if (message instanceof Message) message = message.id
if (user !== undefined) {
if (typeof user !== 'string') {
user = user.id
}
}
2020-12-24 00:37:57 +00:00
const encodedEmoji = encodeURI(emoji)
if (user === undefined) {
await this.client.rest.delete(
MESSAGE_REACTION_ME(this.id, message, encodedEmoji)
)
} else {
await this.client.rest.delete(
MESSAGE_REACTION_USER(this.id, message, encodedEmoji, user)
)
}
2020-12-26 03:07:15 +00:00
}
2020-12-26 03:04:59 +00:00
2020-12-22 10:10:19 +00:00
/**
* Fetch Messages of a Channel
* @param options Options to configure fetching Messages
*/
async fetchMessages(options?: {
limit?: number
around?: Message | string
before?: Message | string
after?: Message | string
}): Promise<Collection<string, Message>> {
const res = new Collection<string, Message>()
const raws = (await this.client.rest.api.channels[this.id].messages.get({
limit: options?.limit ?? 50,
around:
options?.around === undefined
? undefined
: typeof options.around === 'string'
? options.around
: options.around.id,
before:
options?.before === undefined
? undefined
: typeof options.before === 'string'
? options.before
: options.before.id,
after:
options?.after === undefined
? undefined
: typeof options.after === 'string'
? options.after
: options.after.id
})) as MessagePayload[]
for (const raw of raws) {
await this.messages.set(raw.id, raw)
const msg = ((await this.messages.get(raw.id)) as unknown) as Message
res.set(msg.id, msg)
}
return res
2020-12-24 00:37:57 +00:00
}
/** Trigger the typing indicator. NOT recommended to be used by bots unless you really want to. */
async triggerTyping(): Promise<TextChannel> {
2021-01-25 13:26:32 +00:00
await this.client.rest.api.channels[this.id].typing.post()
return this
}
2020-12-02 12:29:52 +00:00
}