-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
MessageManager.ts
160 lines (144 loc) · 5.61 KB
/
MessageManager.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
import { Collection } from "@discordjs/collection";
import type { ChatService } from "@guildedjs/api";
import type { CollectorOptions, CollectorReturnValue } from "../../structures";
import { MessageCollector } from "../../structures";
import { Message } from "../../structures/Message";
import type { MaybePromise, MessageContent, OptionQuery } from "../../typings";
import { resolveContentToData } from "../../util";
import { CacheableStructManager } from "./CacheableStructManager";
/**
* Manager for handling caching and interactions for Messages
*/
export class GlobalMessageManager extends CacheableStructManager<string, Message> {
/**
* Whether or not messages should be cached.
*/
get shouldCacheMessage(): boolean {
return this.client.options.cache?.cacheMessages !== false;
}
/**
* Fetches multiple messages from a channel.
*
* @param channelId The ID of the channel to fetch messages from.
* @param options Additional options for the fetch.
* @returns A promise that resolves with a collection of messages.
*/
async fetchMany(channelId: string, options: Omit<OptionQuery<ChatService["channelMessageReadMany"]>, "channelId">): Promise<Collection<string, Message>> {
const data = await this.client.rest.router.chat.channelMessageReadMany({
channelId,
...options,
});
const messages = new Collection<string, Message>();
for (const message of data.messages) {
const newMessage = new Message(this.client, message);
messages.set(newMessage.id, newMessage);
}
return messages;
}
/**
* Fetches a message from a channel.
*
* @param channelId The ID of the channel to fetch the message from.
* @param messageId The ID of the message to fetch.
* @param force Whether or not to force the fetch.
* @returns A promise that resolves with the requested message.
*/
async fetch(channelId: string, messageId: string, force?: boolean): Promise<Message> {
if (!force) {
const existingMessage = this.client.messages.cache.get(messageId);
if (existingMessage) return existingMessage;
}
const data = await this.client.rest.router.chat.channelMessageRead({
channelId,
messageId,
});
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
}
/**
* Sends a message to a channel.
*
* @param channelId The ID of the channel to send the message to.
* @param content The content of the message.
* @returns A promise that resolves with the created message.
* @example
* let replyObj = {
* content: 'This is text, supports **markdown**.',
* embeds: [{
* title: 'This is an embed title!',
* description: 'A description may go here'
* }]
* };
* await message.client.messages.send(message.channelId, replyObj)
*/
async send(channelId: string, content: MessageContent): Promise<Message> {
const data = await this.client.rest.router.chat.channelMessageCreate({
channelId,
requestBody: resolveContentToData(content),
});
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage;
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
}
/**
* Adds a reaction to a message.
*
* @param channelId The ID of the channel containing the message.
* @param contentId The ID of the message to add a reaction to.
* @param emoteId The ID of the emote to add as a reaction.
* @returns A promise that resolves to nothing when the reaction is added.
*/
async addReaction(channelId: string, contentId: string, emoteId: number): Promise<void> {
await this.client.reactions.create(channelId, contentId, emoteId);
}
/**
* Deletes either a whole reaction emote from a message or a specific user's if a userId is provided.
*
* @param channelId The ID of the channel containing the message.
* @param contentId The ID of the message to delete the reaction from.
* @param emoteId The ID of the emote to delete as a reaction.
* @returns A promise that resolves to nothing when the reaction is deleted.
*/
async deleteReaction(channelId: string, contentId: string, emoteId: number, userId?: string): Promise<void> {
await this.client.reactions.delete(channelId, contentId, emoteId, userId);
}
/**
* Updates a message in a channel.
*
* @param channelId The ID of the channel containing the message.
* @param messageId The ID of the message to update.
* @param content The new content of the message.
* @returns A promise that resolves with the updated message.
*/
async update(channelId: string, messageId: string, content: MessageContent): Promise<Message> {
const data = await this.client.rest.router.chat.channelMessageUpdate({
channelId,
messageId,
requestBody: resolveContentToData(content),
});
const existingMessage = this.client.messages.cache.get(data.message.id);
if (existingMessage) return existingMessage._update(data.message);
const newMessage = new Message(this.client, data.message);
this.client.messages.cache.set(newMessage.id, newMessage);
return newMessage;
}
/** Delete a channel message. */
async delete(channelId: string, messageId: string): Promise<void> {
await this.client.rest.router.chat.channelMessageDelete({
channelId,
messageId,
});
}
async awaitMessages(channelId: string, options: CollectorOptions<Message>): Promise<CollectorReturnValue<Message>> {
return new MessageCollector(this.client, {
...options,
filter: (item): MaybePromise<boolean> => {
if (item.channelId !== channelId) return false;
return options.filter?.(item) ?? true;
},
}).start();
}
}