Skip to content

Commit cf71f3f

Browse files
Allow invite links based on guild rather than URL
1 parent 96a62be commit cf71f3f

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

src/components/anti-invite-links.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type allowed_invite_entry = {
2727
};
2828

2929
export default class AntiInviteLinks extends BotComponent {
30-
private allowed_invites = new Set<string>();
30+
private allowed_guilds = new Set<string>();
3131

3232
private staff_flag_log!: Discord.TextChannel;
3333

@@ -74,7 +74,7 @@ export default class AntiInviteLinks extends BotComponent {
7474
}
7575

7676
override async on_ready() {
77-
this.allowed_invites = new Set((await this.database.allowed_invites.find().toArray()).map(e => e.code));
77+
this.allowed_guilds = new Set((await this.database.allowed_invites.find().toArray()).map(e => e.guild_id));
7878
}
7979

8080
private static build_guild_embed(entry: allowed_invite_entry) {
@@ -87,25 +87,18 @@ export default class AntiInviteLinks extends BotComponent {
8787
.setFooter({ text: entry.code });
8888
}
8989

90-
private async handle_add(command: TextBasedCommand, code: string) {
90+
private async handle_add(command: TextBasedCommand, resolvable: Discord.InviteResolvable) {
9191
try {
92-
if (this.allowed_invites.has(code)) {
93-
await command.react("🤷", true);
94-
return;
95-
}
96-
M.log("Adding ", code, " to allowed invites");
97-
const invite = await this.wheatley.client.fetchInvite(code);
92+
M.log("Adding ", resolvable, " to allowed invites");
93+
const invite = await this.wheatley.client.fetchInvite(resolvable);
9894
if (invite.guild == null) {
9995
throw Error("not a Guild invite");
10096
}
101-
if (invite.expiresAt != null) {
102-
throw Error("not a permanent invite");
103-
}
10497
const res = await this.database.allowed_invites.findOneAndUpdate(
105-
{ code: code },
98+
{ code: invite.code },
10699
{
107100
$set: {
108-
code: code,
101+
code: invite.code,
109102
url: invite.url,
110103
guild_id: invite.guild.id,
111104
guild_name: invite.guild.name,
@@ -115,9 +108,10 @@ export default class AntiInviteLinks extends BotComponent {
115108
{ upsert: true, returnDocument: "after" },
116109
);
117110
if (res == null) {
118-
throw Error("database update failed");
111+
await command.react("🤷", true);
112+
return;
119113
}
120-
this.allowed_invites.add(code);
114+
this.allowed_guilds.add(res.guild_id);
121115
await command.replyOrFollowUp({
122116
embeds: [AntiInviteLinks.build_guild_embed(res)],
123117
});
@@ -127,17 +121,13 @@ export default class AntiInviteLinks extends BotComponent {
127121
}
128122

129123
private async handle_remove(command: TextBasedCommand, code: string) {
130-
if (!this.allowed_invites.has(code)) {
131-
await command.react("🤷", true);
132-
return;
133-
}
134124
M.log("Removing ", code, " from allowed invites");
135125
const res = await this.database.allowed_invites.findOneAndDelete({ code: code });
136126
if (res == null) {
137-
await command.replyOrFollowUp(`${this.wheatley.emoji.error} database update failed`, true);
127+
await command.react("🤷", true);
138128
return;
139129
}
140-
this.allowed_invites.delete(code);
130+
this.allowed_guilds.delete(res.guild_id);
141131
await command.react(this.wheatley.emoji.success, true);
142132
}
143133

@@ -154,6 +144,18 @@ export default class AntiInviteLinks extends BotComponent {
154144
);
155145
}
156146

147+
private async is_allowed(code: Discord.InviteResolvable) {
148+
try {
149+
const invite = await this.wheatley.client.fetchInvite(code);
150+
if (invite.guild == null) {
151+
return false;
152+
}
153+
return this.allowed_guilds.has(invite.guild.id);
154+
} catch {
155+
return false;
156+
}
157+
}
158+
157159
async member_is_proficient_or_higher(member: Discord.GuildMember | null) {
158160
if (!member) {
159161
return false;
@@ -175,7 +177,7 @@ export default class AntiInviteLinks extends BotComponent {
175177
return;
176178
}
177179
const match = match_invite(message.content);
178-
if (match && !this.allowed_invites.has(match) && !(await this.member_is_proficient_or_higher(message.member))) {
180+
if (match && !(await this.is_allowed(match)) && !(await this.member_is_proficient_or_higher(message.member))) {
179181
const quote = await this.utilities.make_quote_embeds([message]);
180182
await message.delete();
181183
assert(!(message.channel instanceof Discord.PartialGroupDMChannel));

0 commit comments

Comments
 (0)