/
message.ts
135 lines (113 loc) · 3.95 KB
/
message.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
import { ChannelMessageRegex, MessageLinkRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
import {
isAnyInteraction,
isGuildBasedChannel,
isNewsChannel,
isStageChannel,
isTextBasedChannel,
isTextChannel,
type AnyInteraction,
type GuildBasedChannelTypes,
type TextBasedChannelTypes
} from '@sapphire/discord.js-utilities';
import { container } from '@sapphire/pieces';
import { Result } from '@sapphire/result';
import type { Awaitable } from '@sapphire/utilities';
import { PermissionFlagsBits, type Message, type Snowflake, type User } from 'discord.js';
import { Identifiers } from '../errors/Identifiers';
/**
* Options to resolve a message from a string, given a certain context.
*/
export interface MessageResolverOptions {
/**
* Channel to resolve the message in.
* @default message.channel
*/
channel?: TextBasedChannelTypes;
/**
* Base {@link Message} or {@link AnyInteraction} to resolve the message from (e.g. pick the channel if not given).
*/
messageOrInteraction: Message | AnyInteraction;
/**
* Whether to scan the entire guild cache for the message.
* If channel is given with this option, this option is ignored.
* @default false
*/
scan?: boolean;
}
export async function resolveMessage(parameter: string, options: MessageResolverOptions): Promise<Result<Message, Identifiers.ArgumentMessageError>> {
const message =
(await resolveById(parameter, options)) ??
(await resolveByLink(parameter, options)) ??
(await resolveByChannelAndMessage(parameter, options));
if (message) {
return Result.ok(message);
}
return Result.err(Identifiers.ArgumentMessageError);
}
function resolveById(parameter: string, options: MessageResolverOptions): Awaitable<Message | null> {
if (!SnowflakeRegex.test(parameter) || isStageChannel(options.messageOrInteraction.channel)) {
return null;
}
if (options.channel && !isStageChannel(options.channel)) {
return options.channel.messages.fetch(parameter as Snowflake);
}
if (options.scan && isGuildBasedChannel(options.messageOrInteraction.channel)) {
for (const channel of options.messageOrInteraction.channel.guild.channels.cache.values()) {
if (!isTextBasedChannel(channel) || isStageChannel(channel)) {
continue;
}
const message = channel.messages.cache.get(parameter);
if (message) {
return message;
}
}
}
return options.messageOrInteraction.channel?.messages.fetch(parameter as Snowflake) ?? null;
}
async function resolveByLink(parameter: string, options: MessageResolverOptions): Promise<Message | null> {
if (!options.messageOrInteraction.guild) {
return null;
}
const matches = MessageLinkRegex.exec(parameter);
if (!matches) {
return null;
}
const [, guildId, channelId, messageId] = matches;
const guild = container.client.guilds.cache.get(guildId as Snowflake);
if (guild !== options.messageOrInteraction.guild) {
return null;
}
return getMessageFromChannel(
channelId,
messageId,
isAnyInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
);
}
async function resolveByChannelAndMessage(parameter: string, options: MessageResolverOptions): Promise<Message | null> {
const result = ChannelMessageRegex.exec(parameter)?.groups;
if (!result) {
return null;
}
return getMessageFromChannel(
result.channelId,
result.messageId,
isAnyInteraction(options.messageOrInteraction) ? options.messageOrInteraction.user : options.messageOrInteraction.author
);
}
async function getMessageFromChannel(channelId: Snowflake, messageId: Snowflake, originalAuthor: User): Promise<Message | null> {
const channel = container.client.channels.cache.get(channelId) as GuildBasedChannelTypes;
if (!channel) {
return null;
}
if (!(isNewsChannel(channel) || isTextChannel(channel))) {
return null;
}
if (!channel.viewable) {
return null;
}
if (!channel.permissionsFor(originalAuthor)?.has(PermissionFlagsBits.ViewChannel)) {
return null;
}
return channel.messages.fetch(messageId);
}