/
Messages.ts
175 lines (153 loc) · 6.71 KB
/
Messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { APIInteractionResponseCallbackData, APIMessageReferenceSend, RESTGetAPIChannelMessageReactionUsersQuery, RESTGetAPIChannelMessageReactionUsersResult, RESTGetAPIChannelMessageResult, RESTPatchAPIChannelMessageResult, RESTPostAPIChannelMessageCrosspostResult, RESTPostAPIChannelMessageJSONBody, RESTPostAPIChannelMessageResult, RESTPostAPIWebhookWithTokenJSONBody, RESTPutAPIChannelMessageReactionResult, Snowflake } from 'discord-api-types'
import { Embed } from '../../structures/Embed'
import { RestManager } from '../Manager'
import { resolveString } from '../../utils/UtilityFunctions'
import FormData from 'form-data'
/**
* ID of custom emoji or unicode emoji
*/
export type Emoji = string | Snowflake
type StringifiedMessageTypes = string | Function | bigint | number | symbol | undefined
export type MessageTypes = RESTPostAPIChannelMessageJSONBody | RESTPostAPIWebhookWithTokenJSONBody | StringifiedMessageTypes | Embed<any>
/**
* Message resource
*/
export class MessagesResource {
constructor (private readonly rest: RestManager) {}
static _formMessage (message: MessageTypes, webhook?: boolean): RESTPostAPIWebhookWithTokenJSONBody | RESTPostAPIChannelMessageJSONBody | APIInteractionResponseCallbackData {
if (message instanceof Embed) {
message = webhook
? {
embeds: [message.render()]
}
: {
embed: message.render()
}
} else if (['bigint', 'function', 'number', 'string', 'symbol', 'undefined'].includes(typeof message)) {
message = {
content: resolveString(message as StringifiedMessageTypes)
}
}
return message as RESTPostAPIWebhookWithTokenJSONBody | RESTPostAPIChannelMessageJSONBody
}
/**
* Sends a message to a channel
* @param channelId ID of channel
* @param data Message data
*/
async send (channelId: Snowflake, data: MessageTypes, reply?: APIMessageReferenceSend): Promise<RESTPostAPIChannelMessageResult> {
const msg = MessagesResource._formMessage(data) as RESTPostAPIChannelMessageJSONBody
if (reply) msg.message_reference = reply
return await this.rest.request('POST', `/channels/${channelId}/messages`, {
body: msg
})
}
/**
* Sends a file to a channel
* @param channelId ID of channel
* @param data File Buffer and name
* @param extra Extra message data
*/
async sendFile (channelId: Snowflake, data: { name: string, buffer: Buffer }, extra?: MessageTypes): Promise<RESTPostAPIChannelMessageResult> {
const formData = new FormData()
formData.append('file', data.buffer, data.name || 'file')
if (extra) formData.append('payload_json', JSON.stringify(MessagesResource._formMessage(extra)))
return await this.rest.request('POST', `/channels/${channelId}/messages`, {
body: formData,
headers: formData.getHeaders(),
parser: (_) => _
})
}
/**
* Gets a message
* @param channelId ID of channel
* @param messageId ID of message
*/
async get (channelId: Snowflake, messageId: Snowflake): Promise<RESTGetAPIChannelMessageResult> {
return await this.rest.request('GET', `/channels/${channelId}/messages/${messageId}`)
}
/**
* Deletes a message
* @param channelId ID of channel
* @param messageId ID of message
*/
async delete (channelId: Snowflake, messageId: Snowflake): Promise<never> {
return this.rest.request('DELETE', `/channels/${channelId}/messages/${messageId}`) as never
}
/**
* Deletes multiple messages
* @param channelId ID of channel
* @param messageIds ID of messages
*/
async bulkDelete (channelId: Snowflake, messageIds: Snowflake[]): Promise<never> {
if (messageIds.length < 2) return await this.delete(channelId, messageIds[0])
return this.rest.request('POST', `/channels/${channelId}/messages/bulk-delete`, {
body: {
messages: messageIds
}
}) as never
}
/**
* Edits a message
* @param channelId ID of channel
* @param messageId ID of message
* @param data New message data
*/
async edit (channelId: Snowflake, messageId: Snowflake, data: MessageTypes): Promise<RESTPatchAPIChannelMessageResult> {
return await this.rest.request('PATCH', `/channels/${channelId}/messages/${messageId}`, {
body: MessagesResource._formMessage(data)
})
}
/**
* Publishes a message in a news channel
* @param channelId ID of channel
* @param messageId ID of message
*/
async crosspost (channelId: Snowflake, messageId: Snowflake): Promise<RESTPostAPIChannelMessageCrosspostResult> {
return await this.rest.request('POST', `/channels/${channelId}/messages/${messageId}/crosspost`)
}
private _parseEmoji (emoji: Emoji): string {
if (emoji.match(/^[0-9]+$/)) return `unknown:${emoji}`
return encodeURIComponent(emoji)
}
/**
* Gets users who've reacted with a specific emoji
* @param channelId ID of channel
* @param messageId ID of message
* @param emoji ID or unicode for emoji
* @param query Query for fetching
*/
async getReactions (channelId: Snowflake, messageId: Snowflake, emoji: Emoji, query?: RESTGetAPIChannelMessageReactionUsersQuery): Promise<RESTGetAPIChannelMessageReactionUsersResult> {
return await this.rest.request('GET', `/channels/${channelId}/messages/${messageId}/reactions/${this._parseEmoji(emoji)}`, {
query
}) as RESTGetAPIChannelMessageReactionUsersResult
}
/**
* Adds a reaction to a message
* @param channelId ID of channel
* @param messageId ID of message
* @param emoji ID or unicode for emoji
*/
async react (channelId: Snowflake, messageId: Snowflake, emoji: Emoji): Promise<RESTPutAPIChannelMessageReactionResult> {
return this.rest.request('PUT', `/channels/${channelId}/messages/${messageId}/reactions/${this._parseEmoji(emoji)}/@me`) as RESTPutAPIChannelMessageReactionResult
}
/**
* Removes one reaction for a specific user
* @param channelId ID of channel
* @param messageId ID of message
* @param emoji ID or unicode for emoji
* @param user Users or leave blank to remove your own
*/
async deleteReaction (channelId: Snowflake, messageId: Snowflake, emoji: Emoji, user: Snowflake | '@me' = '@me'): Promise<never> {
return this.rest.request('DELETE', `/channels/${channelId}/messages/${messageId}/reactions/${this._parseEmoji(emoji)}/${user}`) as never
}
/**
* Deletes multiple reactions from a message
* @param channelId ID of channel
* @param messageId ID of message
* @param emoji Emoji ID or unicode, or leave blank to remove all reactions
*/
async deleteAllReactions (channelId: Snowflake, messageId: Snowflake, emoji?: Emoji): Promise<never> {
return this.rest.request('DELETE', `/channels/${channelId}/messages/${messageId}/reactions${emoji ? `/${this._parseEmoji(emoji)}` : ''}`) as never
}
}