2020-10-22 15:50:47 +00:00
|
|
|
import { Base } from './base.ts'
|
|
|
|
import {
|
|
|
|
Attachment,
|
|
|
|
ChannelMention,
|
|
|
|
MessageActivity,
|
|
|
|
MessageApplication,
|
2020-10-25 17:03:53 +00:00
|
|
|
MessageOption,
|
2020-10-22 15:50:47 +00:00
|
|
|
MessagePayload,
|
|
|
|
MessageReference,
|
|
|
|
Reaction
|
|
|
|
} from '../types/channelTypes.ts'
|
|
|
|
import { Client } from '../models/client.ts'
|
2020-10-25 17:03:53 +00:00
|
|
|
import { User } from './user.ts'
|
|
|
|
import { Member } from './member.ts'
|
|
|
|
import { Embed } from './embed.ts'
|
|
|
|
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
2020-10-29 14:43:27 +00:00
|
|
|
import cache from '../models/cache.ts'
|
2020-10-22 15:50:47 +00:00
|
|
|
|
2020-10-24 18:11:27 +00:00
|
|
|
export class Message extends Base {
|
2020-10-25 17:03:53 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly
|
|
|
|
private data: MessagePayload
|
2020-10-22 15:50:47 +00:00
|
|
|
id: string
|
2020-10-23 16:11:00 +00:00
|
|
|
channelID: string
|
|
|
|
guildID?: string
|
2020-10-25 17:03:53 +00:00
|
|
|
author: User
|
2020-10-29 14:43:27 +00:00
|
|
|
member?: Member
|
2020-10-22 15:50:47 +00:00
|
|
|
content: string
|
|
|
|
timestamp: string
|
2020-10-23 16:11:00 +00:00
|
|
|
editedTimestamp?: string
|
2020-10-22 15:50:47 +00:00
|
|
|
tts: boolean
|
2020-10-23 16:11:00 +00:00
|
|
|
mentionEveryone: boolean
|
2020-10-29 14:43:27 +00:00
|
|
|
mentions: User[]
|
|
|
|
mentionRoles: string[]
|
2020-10-23 16:11:00 +00:00
|
|
|
mentionChannels?: ChannelMention[]
|
2020-10-22 15:50:47 +00:00
|
|
|
attachments: Attachment[]
|
2020-10-25 17:03:53 +00:00
|
|
|
embeds: Embed[]
|
2020-10-23 16:11:00 +00:00
|
|
|
reactions?: Reaction[]
|
|
|
|
nonce?: string | number
|
2020-10-22 15:50:47 +00:00
|
|
|
pinned: boolean
|
2020-10-29 14:43:27 +00:00
|
|
|
webhookID?: string
|
2020-10-22 15:50:47 +00:00
|
|
|
type: number
|
|
|
|
activity?: MessageActivity
|
|
|
|
application?: MessageApplication
|
2020-10-23 16:11:00 +00:00
|
|
|
messageReference?: MessageReference
|
|
|
|
flags?: number
|
2020-10-22 15:50:47 +00:00
|
|
|
|
|
|
|
constructor (client: Client, data: MessagePayload) {
|
|
|
|
super(client)
|
2020-10-25 17:03:53 +00:00
|
|
|
this.data = data
|
2020-10-22 15:50:47 +00:00
|
|
|
this.id = data.id
|
2020-10-23 16:11:00 +00:00
|
|
|
this.channelID = data.channel_id
|
|
|
|
this.guildID = data.guild_id
|
2020-10-29 14:43:27 +00:00
|
|
|
this.author =
|
|
|
|
cache.get('user', data.author.id) ?? new User(this.client, data.author)
|
2020-10-22 15:50:47 +00:00
|
|
|
this.content = data.content
|
|
|
|
this.timestamp = data.timestamp
|
2020-10-23 16:11:00 +00:00
|
|
|
this.editedTimestamp = data.edited_timestamp
|
2020-10-22 15:50:47 +00:00
|
|
|
this.tts = data.tts
|
2020-10-23 16:11:00 +00:00
|
|
|
this.mentionEveryone = data.mention_everyone
|
2020-10-29 14:43:27 +00:00
|
|
|
this.mentions = data.mentions.map(
|
|
|
|
v => cache.get('user', v.id) ?? new User(client, v)
|
|
|
|
)
|
|
|
|
this.mentionRoles = data.mention_roles
|
|
|
|
this.mentionChannels = data.mention_channels
|
2020-10-22 15:50:47 +00:00
|
|
|
this.attachments = data.attachments
|
2020-10-29 14:43:27 +00:00
|
|
|
this.embeds = data.embeds.map(v => new Embed(v))
|
2020-10-22 15:50:47 +00:00
|
|
|
this.reactions = data.reactions
|
|
|
|
this.nonce = data.nonce
|
|
|
|
this.pinned = data.pinned
|
2020-10-29 14:43:27 +00:00
|
|
|
this.webhookID = data.webhook_id
|
2020-10-22 15:50:47 +00:00
|
|
|
this.type = data.type
|
|
|
|
this.activity = data.activity
|
|
|
|
this.application = data.application
|
2020-10-23 16:11:00 +00:00
|
|
|
this.messageReference = data.message_reference
|
2020-10-22 15:50:47 +00:00
|
|
|
this.flags = data.flags
|
2020-10-29 14:43:27 +00:00
|
|
|
cache.set('message', this.id, this)
|
2020-10-22 15:50:47 +00:00
|
|
|
}
|
2020-10-25 17:03:53 +00:00
|
|
|
|
2020-10-30 14:51:40 +00:00
|
|
|
protected readFromData (data: MessagePayload): void {
|
2020-10-29 14:43:27 +00:00
|
|
|
super.readFromData(data)
|
|
|
|
this.channelID = data.channel_id ?? this.channelID
|
|
|
|
this.guildID = data.guild_id ?? this.guildID
|
|
|
|
this.author =
|
|
|
|
cache.get('user', data.author.id) ??
|
2020-10-30 14:51:40 +00:00
|
|
|
this.author ??
|
|
|
|
new User(this.client, data.author)
|
2020-10-29 14:43:27 +00:00
|
|
|
this.content = data.content ?? this.content
|
|
|
|
this.timestamp = data.timestamp ?? this.timestamp
|
|
|
|
this.editedTimestamp = data.edited_timestamp ?? this.editedTimestamp
|
|
|
|
this.tts = data.tts ?? this.tts
|
|
|
|
this.mentionEveryone = data.mention_everyone ?? this.mentionEveryone
|
|
|
|
this.mentions =
|
|
|
|
data.mentions.map(
|
|
|
|
v => cache.get('user', v.id) ?? new User(this.client, v)
|
|
|
|
) ?? this.mentions
|
|
|
|
this.mentionRoles = data.mention_roles ?? this.mentionRoles
|
|
|
|
this.mentionChannels = data.mention_channels ?? this.mentionChannels
|
|
|
|
this.attachments = data.attachments ?? this.attachments
|
|
|
|
this.embeds = data.embeds.map(v => new Embed(v)) ?? this.embeds
|
|
|
|
this.reactions = data.reactions ?? this.reactions
|
|
|
|
this.nonce = data.nonce ?? this.nonce
|
|
|
|
this.pinned = data.pinned ?? this.pinned
|
|
|
|
this.webhookID = data.webhook_id ?? this.webhookID
|
|
|
|
this.type = data.type ?? this.type
|
|
|
|
this.activity = data.activity ?? this.activity
|
|
|
|
this.application = data.application ?? this.application
|
|
|
|
this.messageReference = data.message_reference ?? this.messageReference
|
|
|
|
this.flags = data.flags ?? this.flags
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We have to seperate fetch()
|
2020-10-25 17:03:53 +00:00
|
|
|
async editMessage (text?: string, option?: MessageOption): Promise<Message> {
|
|
|
|
if (text !== undefined && option !== undefined) {
|
|
|
|
throw new Error('Either text or option is necessary.')
|
|
|
|
}
|
|
|
|
const resp = await fetch(CHANNEL_MESSAGE(this.channelID, this.id), {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${this.client.token}`,
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
method: 'PATCH',
|
|
|
|
body: JSON.stringify({
|
|
|
|
content: text,
|
2020-10-29 14:43:27 +00:00
|
|
|
embed: option?.embed.toJSON(),
|
2020-10-25 17:03:53 +00:00
|
|
|
file: option?.file,
|
|
|
|
tts: option?.tts,
|
|
|
|
allowed_mentions: option?.allowedMention
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Message(this.client, await resp.json())
|
|
|
|
}
|
2020-10-29 14:43:27 +00:00
|
|
|
|
|
|
|
// TODO: We have to seperate fetch()
|
|
|
|
async delete (): Promise<void> {
|
|
|
|
const resp = await fetch(CHANNEL_MESSAGE(this.channelID, this.id), {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bot ${this.client.token}`
|
|
|
|
},
|
|
|
|
method: 'DELETE'
|
|
|
|
})
|
|
|
|
|
|
|
|
// TODO: improve Error and Promise
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
if (resp.status !== 204) {
|
|
|
|
reject(new Error())
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
}
|
2020-10-22 15:50:47 +00:00
|
|
|
}
|