Skip to content

Commit 12d948e

Browse files
committed
fix: Suppress embeds correctly
1 parent bd1aa26 commit 12d948e

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/index.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
import Discord, {
1111
ActivityType,
12+
Client,
1213
Events,
1314
GatewayIntentBits,
1415
type Interaction,
16+
Message,
1517
Partials,
1618
} from "discord.js";
1719
import CommandHandler from "@/commandHandler";
1820
import { env } from "@/config/env";
21+
import { suppressGitHubUnfurl } from "./util/suppressGitHubUnfurl";
1922

2023
process.on("unhandledRejection", reason => {
2124
console.log("Unhandled Rejection:", reason);
@@ -24,8 +27,9 @@ process.on("unhandledRejection", reason => {
2427
const client = new Discord.Client({
2528
intents: [
2629
GatewayIntentBits.Guilds,
27-
GatewayIntentBits.GuildMessages,
2830
GatewayIntentBits.GuildEmojisAndStickers,
31+
GatewayIntentBits.GuildMessages,
32+
GatewayIntentBits.MessageContent,
2933
],
3034
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
3135
});
@@ -61,24 +65,15 @@ client.on(Events.InteractionCreate, async (interaction: Interaction) => {
6165
client.on(Events.Error, e => {
6266
console.error("Discord client error!", e);
6367
});
64-
client.on(Events.MessageCreate, async message => {
65-
if (message.author.bot) return;
66-
67-
message = await message.fetch();
6868

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;
69+
// Message updates contain full data. Typings are corrected in a newer discord.js version.
70+
client.on(Events.MessageUpdate, async (_, newMessage) => {
71+
await suppressGitHubUnfurl(newMessage as Message);
72+
});
7473

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-
}
74+
client.on(Events.MessageCreate, async (message: Message<boolean>) => {
75+
if (message.author.bot) return;
76+
await suppressGitHubUnfurl(message);
8277
});
8378

8479
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)