Skip to content

Commit d7d44ea

Browse files
committed
fix(on_message): adjust advisor
- Attempt a DM first, if that fails then do an inline reply with the converted x.com link
1 parent 17238b7 commit d7d44ea

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/events/on_message/_advise.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default async function mutate_content(message: Message) {
2323
) !== -1,
2424
);
2525
if (!caughtLinks) return;
26+
2627
const hasXLinks: boolean = caughtLinks.some((item) =>
2728
item.startsWith('https://x.com'),
2829
);
@@ -32,17 +33,18 @@ export default async function mutate_content(message: Message) {
3233
caughtLinks.length > 1
3334
? 'Here are the updated links:'
3435
: 'Here is the updated link:';
35-
const updatedLinks = caughtLinks
36-
.map((link) => {
37-
const replaceIdx = replacementMap.findIndex((item) =>
38-
link.startsWith(item.test),
39-
);
40-
return link.replace(
41-
replacementMap[replaceIdx].test,
42-
replacementMap[replaceIdx].replace,
43-
);
44-
})
45-
.join('\n');
36+
37+
const updatedLinks = caughtLinks.map((link) => {
38+
const replaceIdx = replacementMap.findIndex((item) =>
39+
link.startsWith(item.test),
40+
);
41+
return link.replace(
42+
replacementMap[replaceIdx].test,
43+
replacementMap[replaceIdx].replace,
44+
);
45+
});
46+
47+
const updatedLinkList = updatedLinks.map((link) => `- ${link}\n`).join('');
4648

4749
const updatedMessage: string = (function () {
4850
let newContent = message.content;
@@ -53,7 +55,22 @@ export default async function mutate_content(message: Message) {
5355
return newContent;
5456
})();
5557

56-
await message.author.send(
57-
`Re: ${message.url}\n\nI see you've provided a link to \`x.com\`. Please consider posting a new message having \`x.com\` replaced with \`xcancel.com\`, that way server members may view the message and thread without requiring an account.\n\n${updatedPhrase}\`\`\`${updatedLinks}\`\`\`\n\nHere is your entire message with adjusted links:\n\`\`\`${updatedMessage}\`\`\``,
58-
);
58+
try {
59+
await message.author.send(
60+
`Re: ${message.url}\n\nI see you've provided a link to \`x.com\`. Please consider posting a new message having \`x.com\` replaced with \`xcancel.com\`, that way server members may view the message and thread without requiring an account.\n\n${updatedPhrase}\`\`\`${updatedLinkList}\`\`\`\n\nHere is your entire message with adjusted links:\n\`\`\`${updatedLinkList}\`\`\``,
61+
);
62+
63+
return; // mission complete
64+
} catch (e) {
65+
// assume user disabled DMs upon error
66+
}
67+
68+
// Plan B: inline reply
69+
try {
70+
await message.reply(
71+
`I converted your \`x.com\` links to use \`xcancel.com\` so that server members won't require an account to view content and threads.\n\n${updatedLinkList}`,
72+
);
73+
} catch (e) {
74+
// don't handle failures
75+
}
5976
}

0 commit comments

Comments
 (0)