Skip to content

Commit 5e915d0

Browse files
authored
feat(resolver): make resolveMessage parse channelId-messageId (#292)
1 parent cf2e1c4 commit 5e915d0

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

src/lib/resolvers/message.ts

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { MessageLinkRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
1+
import { ChannelMessageRegex, MessageLinkRegex, SnowflakeRegex } from '@sapphire/discord-utilities';
22
import { GuildBasedChannelTypes, isNewsChannel, isTextChannel, TextBasedChannelTypes } from '@sapphire/discord.js-utilities';
33
import { container } from '@sapphire/pieces';
44
import type { Awaitable } from '@sapphire/utilities';
5-
import { Message, Permissions, Snowflake } from 'discord.js';
5+
import { Message, Permissions, Snowflake, User } from 'discord.js';
66
import { Identifiers } from '../errors/Identifiers';
77
import { err, ok, Result } from '../parsers/Result';
88

@@ -13,7 +13,10 @@ interface MessageResolverOptions {
1313

1414
export async function resolveMessage(parameter: string, options: MessageResolverOptions): Promise<Result<Message, Identifiers.ArgumentMessageError>> {
1515
const channel = options.channel ?? options.message.channel;
16-
const message = (await resolveById(parameter, channel)) ?? (await resolveByLink(parameter, options.message));
16+
const message =
17+
(await resolveById(parameter, channel)) ??
18+
(await resolveByLink(parameter, options.message)) ??
19+
(await resolveByChannelAndMessage(parameter, options.message));
1720
if (message) return ok(message);
1821
return err(Identifiers.ArgumentMessageError);
1922
}
@@ -32,11 +35,22 @@ async function resolveByLink(parameter: string, message: Message): Promise<Messa
3235
const guild = container.client.guilds.cache.get(guildId as Snowflake);
3336
if (guild !== message.guild) return null;
3437

35-
const channel = guild.channels.cache.get(channelId as Snowflake) as GuildBasedChannelTypes;
38+
return getMessageFromChannel(channelId, messageId, message.author);
39+
}
40+
41+
async function resolveByChannelAndMessage(parameter: string, message: Message): Promise<Message | null> {
42+
const result = ChannelMessageRegex.exec(parameter)?.groups;
43+
if (!result) return null;
44+
45+
return getMessageFromChannel(result.channelId, result.messageId, message.author);
46+
}
47+
48+
async function getMessageFromChannel(channelId: Snowflake, messageId: Snowflake, originalAuthor: User): Promise<Message | null> {
49+
const channel = container.client.channels.cache.get(channelId) as GuildBasedChannelTypes;
3650
if (!channel) return null;
3751
if (!(isNewsChannel(channel) || isTextChannel(channel))) return null;
3852
if (!channel.viewable) return null;
39-
if (!channel.permissionsFor(message.author)?.has(Permissions.FLAGS.VIEW_CHANNEL)) return null;
53+
if (!channel.permissionsFor(originalAuthor)?.has(Permissions.FLAGS.VIEW_CHANNEL)) return null;
4054

41-
return channel.messages.fetch(messageId as Snowflake);
55+
return channel.messages.fetch(messageId);
4256
}

yarn.lock

+21-26
Original file line numberDiff line numberDiff line change
@@ -511,29 +511,29 @@
511511
strip-json-comments "^3.1.1"
512512

513513
"@favware/npm-deprecate@^1.0.3":
514-
version "1.0.3"
515-
resolved "https://registry.yarnpkg.com/@favware/npm-deprecate/-/npm-deprecate-1.0.3.tgz#a3bda3e62e0f637605639ec5eed4ef60f41b91fd"
516-
integrity sha512-4WmO+wXilPm6/QWPLdtLZMU2oQzO07rGHxz+TBAWuyEnR6RhGxmVpgaKJZI1ntRW6VP9X7o/Hmgp6/Lf0L9xnw==
514+
version "1.0.4"
515+
resolved "https://registry.yarnpkg.com/@favware/npm-deprecate/-/npm-deprecate-1.0.4.tgz#fe73ed7ddb8f8b96ba1436b9cb047eeda2d2497c"
516+
integrity sha512-eAU1Wq7jybH735/S5EWiNfRhBuaXFF9aAr6BQ27yV2oF5AAXUAIk88fnCUpLYJFE/053Ky6C+Do7aW8zuDrnfg==
517517
dependencies:
518-
"@sapphire/fetch" next
519-
"@sapphire/utilities" "^2.0.1"
520-
colorette "^2.0.12"
518+
"@sapphire/fetch" "^2.0.0"
519+
"@sapphire/utilities" "^3.0.1"
520+
colorette "^2.0.14"
521521
commander "^8.2.0"
522522
js-yaml "^4.1.0"
523523
micromatch "^4.0.4"
524524
npm-package-arg "^8.1.5"
525525
npm-registry-fetch "^11.0.0"
526526

527527
"@favware/rollup-type-bundler@^1.0.4":
528-
version "1.0.4"
529-
resolved "https://registry.yarnpkg.com/@favware/rollup-type-bundler/-/rollup-type-bundler-1.0.4.tgz#bfbc535f508d73a1faf63f5a8fb62a71053b6707"
530-
integrity sha512-Zy8qlL7hIUF7FucEyLixSBaMC37gGmXHifV975dipTtitpsMuc2WmRq0IMskaBQOhOs0+aASysP79oXDcZxFHA==
528+
version "1.0.5"
529+
resolved "https://registry.yarnpkg.com/@favware/rollup-type-bundler/-/rollup-type-bundler-1.0.5.tgz#40b95838673eb748bd288b1a684aa6458585e3ac"
530+
integrity sha512-XghOOsel/pDUp+hFWSjDHyFGF6xSe7WcMJwE82yANr8MwxNS5/l/N0LsiAD3CzbRpttLsVbPuBH+6/P2LEhPxQ==
531531
dependencies:
532-
"@sapphire/utilities" "^2.0.1"
533-
colorette "^2.0.12"
532+
"@sapphire/utilities" "^3.0.1"
533+
colorette "^2.0.14"
534534
commander "^8.2.0"
535535
js-yaml "^4.1.0"
536-
rollup "^2.57.0"
536+
rollup "^2.58.0"
537537
rollup-plugin-dts "^4.0.0"
538538

539539
"@gar/promisify@^1.0.1":
@@ -815,10 +815,10 @@
815815
prettier "^2.4.1"
816816
typescript "^4.4.3"
817817

818-
"@sapphire/fetch@next":
819-
version "2.0.0-next.ae4f5935.0"
820-
resolved "https://registry.yarnpkg.com/@sapphire/fetch/-/fetch-2.0.0-next.ae4f5935.0.tgz#82bc1e6cde443fad26bd13b5e265dd4e74402edf"
821-
integrity sha512-u2t0xB0K8FSrQsbEZ084+iww/uWfqC8Gs3Y1rLqGCtP5Xsxfa+4lrKe2dNLhm7Zc2CqLHi6v2UFo0oK1OdtvIQ==
818+
"@sapphire/fetch@^2.0.0":
819+
version "2.0.0"
820+
resolved "https://registry.yarnpkg.com/@sapphire/fetch/-/fetch-2.0.0.tgz#24b18328cb767d3fa8ed5bac4909f8a1494324ab"
821+
integrity sha512-SPuvU3OTcJMMTmongQQWfeg9QnHpOEAYQ1NGuRI760r68BjnGCU5rw7xR4U4i6izYIir732aMwY8Q9kQp1a2dw==
822822
dependencies:
823823
cross-fetch "^3.1.4"
824824

@@ -860,11 +860,6 @@
860860
tslib "^2.3.1"
861861
typescript "^4.4.3"
862862

863-
"@sapphire/utilities@^2.0.1":
864-
version "2.0.1"
865-
resolved "https://registry.yarnpkg.com/@sapphire/utilities/-/utilities-2.0.1.tgz#c436c5b2b72679c89a40abe46d2f9301d03e63e2"
866-
integrity sha512-KgJsgQ1cnfVKqscrMX4GH29mIVhVQkF8kSTIRCB+hgCCRfIvUU5peE4g/cegnesPS5eMiQqV+NWk7cNwhjextQ==
867-
868863
"@sapphire/utilities@^3.0.1":
869864
version "3.0.1"
870865
resolved "https://registry.yarnpkg.com/@sapphire/utilities/-/utilities-3.0.1.tgz#0b9d57e9411dde6298fbaf0ed7892575a52ae86e"
@@ -1602,7 +1597,7 @@ colorette@^1.4.0:
16021597
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.4.0.tgz#5190fbb87276259a86ad700bff2c6d6faa3fca40"
16031598
integrity sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==
16041599

1605-
colorette@^2.0.12:
1600+
colorette@^2.0.14:
16061601
version "2.0.14"
16071602
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.14.tgz#1629bb27a13cd719ff37d66bc341234af564122e"
16081603
integrity sha512-TLcu0rCLNjDIdKGLGqMtPEAOAZmavC1QCX4mEs3P0mrA/DDoU/tA+Y4UQK/862FkX2TTlbyVIkREZNbf7Y9YwA==
@@ -2050,9 +2045,9 @@ dotgitignore@^2.1.0:
20502045
minimatch "^3.0.4"
20512046

20522047
electron-to-chromium@^1.3.857:
2053-
version "1.3.857"
2054-
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.857.tgz#dcc239ff8a12b6e4b501e6a5ad20fd0d5a3210f9"
2055-
integrity sha512-a5kIr2lajm4bJ5E4D3fp8Y/BRB0Dx2VOcCRE5Gtb679mXIME/OFhWler8Gy2ksrf8gFX+EFCSIGA33FB3gqYpg==
2048+
version "1.3.858"
2049+
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.858.tgz#49eb5be51faa85fc85d153e8be527eef7ff99121"
2050+
integrity sha512-EZ0dTXxTX+rgZC8YDVqKNMyp1Ty3hi1LyQwVBBioV7iiYYjFakokumRsIUhNz5ho+QlNKf7iNY5KLdpdH3yqBw==
20562051

20572052
emittery@^0.8.1:
20582053
version "0.8.1"
@@ -4550,7 +4545,7 @@ rollup-plugin-version-injector@^1.3.3:
45504545
dateformat "^4.2.1"
45514546
lodash "^4.17.20"
45524547

4553-
rollup@^2.57.0, rollup@^2.58.0:
4548+
rollup@^2.58.0:
45544549
version "2.58.0"
45554550
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.58.0.tgz#a643983365e7bf7f5b7c62a8331b983b7c4c67fb"
45564551
integrity sha512-NOXpusKnaRpbS7ZVSzcEXqxcLDOagN6iFS8p45RkoiMqPHDLwJm758UF05KlMoCRbLBTZsPOIa887gZJ1AiXvw==

0 commit comments

Comments
 (0)