2020-12-02 12:29:52 +00:00
|
|
|
import { MessagesManager } from '../managers/messages.ts'
|
|
|
|
import { Client } from '../models/client.ts'
|
|
|
|
import {
|
|
|
|
GuildTextChannelPayload,
|
|
|
|
MessageOption,
|
|
|
|
MessageReference,
|
2020-12-20 09:11:37 +00:00
|
|
|
ModifyGuildTextChannelOption,
|
|
|
|
ModifyGuildTextChannelPayload,
|
2020-12-02 12:29:52 +00:00
|
|
|
Overwrite,
|
|
|
|
TextChannelPayload
|
|
|
|
} from '../types/channel.ts'
|
2020-12-20 09:11:37 +00:00
|
|
|
import {
|
|
|
|
CHANNEL,
|
|
|
|
CHANNEL_MESSAGE,
|
2020-12-24 00:37:57 +00:00
|
|
|
CHANNEL_MESSAGES,
|
2020-12-25 04:44:42 +00:00
|
|
|
MESSAGE_REACTION_ME,
|
|
|
|
MESSAGE_REACTION_USER
|
2020-12-20 09:11:37 +00:00
|
|
|
} from '../types/endpoint.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'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { Guild } from './guild.ts'
|
2020-12-25 04:44:42 +00:00
|
|
|
import { Member } from './member.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
import { Message } from './message.ts'
|
2020-12-25 04:44:42 +00:00
|
|
|
import { User } from './user.ts'
|
2020-12-02 12:29:52 +00:00
|
|
|
|
|
|
|
export type AllMessageOptions = MessageOption | Embed
|
|
|
|
|
|
|
|
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> {
|
2020-12-25 13:09:29 +00:00
|
|
|
if (typeof content === 'object') {
|
|
|
|
option = content
|
|
|
|
content = undefined
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
2020-12-25 13:09:29 +00:00
|
|
|
if (content === undefined && option === undefined) {
|
2020-12-02 12:29:52 +00:00
|
|
|
throw new Error('Either text or option is necessary.')
|
|
|
|
}
|
2020-12-25 13:09:29 +00:00
|
|
|
if (option instanceof Embed) {
|
2020-12-02 12:29:52 +00:00
|
|
|
option = {
|
|
|
|
embed: option
|
|
|
|
}
|
2020-12-25 13:09:29 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
|
|
|
|
const payload: any = {
|
2020-12-25 13:09:29 +00:00
|
|
|
content: content,
|
2020-12-02 12:29:52 +00:00
|
|
|
embed: option?.embed,
|
|
|
|
file: option?.file,
|
|
|
|
tts: option?.tts,
|
|
|
|
allowed_mentions: option?.allowedMentions
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
await res.mentions.fromPayload(resp)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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?: 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 !== undefined ? option.embed.toJSON() : undefined,
|
|
|
|
// Cannot upload new files with Message
|
|
|
|
// file: option?.file,
|
|
|
|
tts: option?.tts,
|
|
|
|
allowed_mentions: option?.allowedMentions
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const res = new Message(this.client, newMsg, this, this.client.user)
|
|
|
|
await res.mentions.fromPayload(newMsg)
|
|
|
|
return res
|
|
|
|
}
|
2020-12-24 00:37:57 +00:00
|
|
|
|
|
|
|
async addReaction(
|
|
|
|
message: Message | string,
|
|
|
|
emoji: Emoji | string
|
|
|
|
): Promise<void> {
|
|
|
|
if (emoji instanceof Emoji) {
|
|
|
|
emoji = emoji.getEmojiString
|
|
|
|
}
|
|
|
|
if (message instanceof Message) {
|
|
|
|
message = message.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const encodedEmoji = encodeURI(emoji)
|
|
|
|
|
|
|
|
await this.client.rest.put(
|
|
|
|
MESSAGE_REACTION_ME(this.id, message, encodedEmoji)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeReaction(
|
|
|
|
message: Message | string,
|
2020-12-25 04:44:42 +00:00
|
|
|
emoji: Emoji | string,
|
|
|
|
user?: User | Member | string
|
2020-12-24 00:37:57 +00:00
|
|
|
): Promise<void> {
|
|
|
|
if (emoji instanceof Emoji) {
|
|
|
|
emoji = emoji.getEmojiString
|
|
|
|
}
|
|
|
|
if (message instanceof Message) {
|
|
|
|
message = message.id
|
|
|
|
}
|
2020-12-25 04:44:42 +00:00
|
|
|
if (user !== undefined) {
|
|
|
|
if (typeof user !== 'string') {
|
|
|
|
user = user.id
|
|
|
|
}
|
|
|
|
}
|
2020-12-24 00:37:57 +00:00
|
|
|
|
|
|
|
const encodedEmoji = encodeURI(emoji)
|
|
|
|
|
2020-12-25 04:44:42 +00:00
|
|
|
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-24 00:37:57 +00:00
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class GuildTextChannel extends TextChannel {
|
|
|
|
guildID: string
|
|
|
|
name: string
|
|
|
|
position: number
|
|
|
|
permissionOverwrites: Overwrite[]
|
|
|
|
nsfw: boolean
|
|
|
|
parentID?: string
|
|
|
|
rateLimit: number
|
|
|
|
topic?: string
|
|
|
|
guild: Guild
|
|
|
|
|
|
|
|
get mention(): string {
|
|
|
|
return `<#${this.id}>`
|
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.mention
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(client: Client, data: GuildTextChannelPayload, guild: Guild) {
|
|
|
|
super(client, data)
|
|
|
|
this.guildID = data.guild_id
|
|
|
|
this.name = data.name
|
|
|
|
this.guild = guild
|
|
|
|
this.position = data.position
|
|
|
|
this.permissionOverwrites = data.permission_overwrites
|
|
|
|
this.nsfw = data.nsfw
|
|
|
|
this.parentID = data.parent_id
|
|
|
|
this.topic = data.topic
|
|
|
|
this.rateLimit = data.rate_limit_per_user
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:28:20 +00:00
|
|
|
readFromData(data: GuildTextChannelPayload): void {
|
2020-12-02 12:29:52 +00:00
|
|
|
super.readFromData(data)
|
|
|
|
this.guildID = data.guild_id ?? this.guildID
|
|
|
|
this.name = data.name ?? this.name
|
|
|
|
this.position = data.position ?? this.position
|
|
|
|
this.permissionOverwrites =
|
|
|
|
data.permission_overwrites ?? this.permissionOverwrites
|
|
|
|
this.nsfw = data.nsfw ?? this.nsfw
|
|
|
|
this.parentID = data.parent_id ?? this.parentID
|
|
|
|
this.topic = data.topic ?? this.topic
|
|
|
|
this.rateLimit = data.rate_limit_per_user ?? this.rateLimit
|
|
|
|
}
|
2020-12-20 09:11:37 +00:00
|
|
|
|
|
|
|
async edit(
|
|
|
|
options?: ModifyGuildTextChannelOption
|
|
|
|
): Promise<GuildTextChannel> {
|
|
|
|
const body: ModifyGuildTextChannelPayload = {
|
|
|
|
name: options?.name,
|
|
|
|
position: options?.position,
|
|
|
|
permission_overwrites: options?.permissionOverwrites,
|
|
|
|
parent_id: options?.parentID
|
|
|
|
}
|
|
|
|
|
|
|
|
const resp = await this.client.rest.patch(CHANNEL(this.id), body)
|
|
|
|
|
|
|
|
return new GuildTextChannel(this.client, resp, this.guild)
|
|
|
|
}
|
2020-12-02 12:29:52 +00:00
|
|
|
}
|