Skip to content

Commit edd052a

Browse files
use client.fetchInvite()
1 parent 56b85ac commit edd052a

1 file changed

Lines changed: 38 additions & 35 deletions

File tree

src/components/anti-invite-links.ts

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ export function match_invite(content: string): string | null {
2020

2121
type allowed_invite_entry = {
2222
code: string;
23+
url: string;
2324
guild_id: string;
2425
guild_name: string;
25-
guild_icon: string;
26+
icon_url?: string;
2627
};
2728

2829
export default class AntiInviteLinks extends BotComponent {
@@ -79,47 +80,49 @@ export default class AntiInviteLinks extends BotComponent {
7980
return new Discord.EmbedBuilder()
8081
.setAuthor({
8182
name: entry.guild_name,
82-
url: `https://discord.gg/${entry.code}`,
83-
iconURL: `https://cdn.discordapp.com/icons/${entry.guild_id}/${entry.guild_icon}.png`,
83+
url: entry.url,
84+
iconURL: entry.icon_url,
8485
})
8586
.setFooter({ text: entry.code });
8687
}
8788

8889
private async handle_add(command: TextBasedCommand, code: string) {
89-
if (this.allowed_invites.has(code)) {
90-
await command.react("🤷", true);
91-
return;
92-
}
93-
M.log("Adding ", code, " to allowed invites");
94-
const server_info = await (await fetch(`https://discord.com/api/v10/invites/${code}`)).json();
95-
if (server_info.code != code) {
96-
await command.replyOrFollowUp(`${this.wheatley.emoji.error} not a valid invite`, true);
97-
return;
98-
}
99-
if (server_info.expires != null) {
100-
await command.replyOrFollowUp(`${this.wheatley.emoji.error} not a permanent invite`, true);
101-
return;
102-
}
103-
const res = await this.database.allowed_invites.findOneAndUpdate(
104-
{ code: code },
105-
{
106-
$set: {
107-
code: code,
108-
guild_id: server_info.guild_id,
109-
guild_name: server_info.guild.name,
110-
guild_icon: server_info.guild.icon,
90+
try {
91+
if (this.allowed_invites.has(code)) {
92+
await command.react("🤷", true);
93+
return;
94+
}
95+
M.log("Adding ", code, " to allowed invites");
96+
const invite = await this.wheatley.client.fetchInvite(code);
97+
if (invite.guild == null) {
98+
throw Error("not a Guild invite");
99+
}
100+
if (invite.expiresAt != null) {
101+
throw Error("not a permanent invite");
102+
}
103+
const res = await this.database.allowed_invites.findOneAndUpdate(
104+
{ code: code },
105+
{
106+
$set: {
107+
code: code,
108+
url: invite.url,
109+
guild_id: invite.guild.id,
110+
guild_name: invite.guild.name,
111+
icon_url: invite.guild.iconURL() ?? undefined,
112+
},
111113
},
112-
},
113-
{ upsert: true, returnDocument: "after" },
114-
);
115-
if (res == null) {
116-
await command.replyOrFollowUp(`${this.wheatley.emoji.error} database update failed`, true);
117-
return;
114+
{ upsert: true, returnDocument: "after" },
115+
);
116+
if (res == null) {
117+
throw Error("database update failed");
118+
}
119+
this.allowed_invites.add(code);
120+
await command.replyOrFollowUp({
121+
embeds: [AntiInviteLinks.build_guild_embed(res)],
122+
});
123+
} catch (e) {
124+
await command.replyOrFollowUp(`${this.wheatley.emoji.error} ${e}`, true);
118125
}
119-
this.allowed_invites.add(code);
120-
await command.replyOrFollowUp({
121-
embeds: [AntiInviteLinks.build_guild_embed(res)],
122-
});
123126
}
124127

125128
private async handle_remove(command: TextBasedCommand, code: string) {

0 commit comments

Comments
 (0)