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 { Role } from './role.ts'
|
|
|
|
import { CHANNEL_MESSAGE } from '../types/endpoint.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-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-25 17:03:53 +00:00
|
|
|
|
|
|
|
get member (): Member | undefined {
|
|
|
|
if (this.data.member !== undefined) {
|
|
|
|
return new Member(this.client, this.data.member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:11:00 +00:00
|
|
|
mentionEveryone: boolean
|
2020-10-25 17:03:53 +00:00
|
|
|
mentions: Member[]
|
|
|
|
mentionRoles: Role[]
|
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-23 16:11:00 +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-25 17:03:53 +00:00
|
|
|
this.author = new User(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-25 17:03:53 +00:00
|
|
|
this.mentions = data.mentions.map(v => new Member(client, v))
|
|
|
|
this.mentionRoles = data.mention_roles.map(v => new Role(client, v))
|
2020-10-22 15:50:47 +00:00
|
|
|
this.attachments = data.attachments
|
2020-10-25 17:03:53 +00:00
|
|
|
this.embeds = data.embeds.map(v => new Embed(client, v))
|
2020-10-22 15:50:47 +00:00
|
|
|
this.reactions = data.reactions
|
|
|
|
this.nonce = data.nonce
|
|
|
|
this.pinned = data.pinned
|
2020-10-23 16:11:00 +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-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,
|
|
|
|
embed: option?.embed,
|
|
|
|
file: option?.file,
|
|
|
|
tts: option?.tts,
|
|
|
|
allowed_mentions: option?.allowedMention
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Message(this.client, await resp.json())
|
|
|
|
}
|
2020-10-22 15:50:47 +00:00
|
|
|
}
|