Skip to content

Commit 5abb82b

Browse files
committed
fix: Suppress embeds correctly
1 parent bd1aa26 commit 5abb82b

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/index.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import Discord, {
1212
Events,
1313
GatewayIntentBits,
1414
type Interaction,
15+
Message,
1516
Partials,
1617
} from "discord.js";
1718
import CommandHandler from "@/commandHandler";
1819
import { env } from "@/config/env";
20+
import { suppressGitHubUnfurl } from "./util/suppressGitHubUnfurl";
1921

2022
process.on("unhandledRejection", reason => {
2123
console.log("Unhandled Rejection:", reason);
@@ -24,8 +26,9 @@ process.on("unhandledRejection", reason => {
2426
const client = new Discord.Client({
2527
intents: [
2628
GatewayIntentBits.Guilds,
27-
GatewayIntentBits.GuildMessages,
2829
GatewayIntentBits.GuildEmojisAndStickers,
30+
GatewayIntentBits.GuildMessages,
31+
GatewayIntentBits.MessageContent,
2932
],
3033
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
3134
});
@@ -61,24 +64,15 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
6164
client.on(Events.Error, e => {
6265
console.error("Discord client error!", e);
6366
});
64-
client.on(Events.MessageCreate, async message => {
65-
if (message.author.bot) return;
66-
67-
message = await message.fetch();
6867

69-
for (const embed of message.embeds) {
70-
if (!embed.url) continue;
71-
72-
const url = new URL(embed.url);
73-
if (url.host !== "github.com") continue;
68+
// Message updates contain full data. Typings are corrected in a newer discord.js version.
69+
client.on(Events.MessageUpdate, async (_, newMessage) => {
70+
await suppressGitHubUnfurl(newMessage as Message);
71+
});
7472

75-
const segments = url.pathname.split("/");
76-
const githubUrlType: string | undefined = segments[3];
77-
if (githubUrlType === "tree" || githubUrlType === "blob") {
78-
await message.suppressEmbeds();
79-
return;
80-
}
81-
}
73+
client.on(Events.MessageCreate, async (message: Message<boolean>) => {
74+
if (message.author.bot) return;
75+
await suppressGitHubUnfurl(message);
8276
});
8377

8478
client.login(env.DISCORD_TOKEN);

src/util/suppressGitHubUnfurl.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Message, MessageFlags } from "discord.js";
2+
3+
export async function suppressGitHubUnfurl(message: Message) {
4+
if (message.flags.has(MessageFlags.SuppressEmbeds)) return;
5+
6+
for (const embed of message.embeds) {
7+
if (!embed.url) continue;
8+
const url = new URL(embed.url);
9+
if (url.host !== "github.com") continue;
10+
const segments = url.pathname.split("/");
11+
const githubUrlType: string | undefined = segments[3];
12+
13+
if (githubUrlType === "tree" || githubUrlType === "blob") {
14+
await message.edit({ flags: message.flags.bitfield | MessageFlags.SuppressEmbeds });
15+
return;
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)