Skip to content

Commit

Permalink
Update invite link logic to whitelist four servers and also to better…
Browse files Browse the repository at this point in the history
… handle .gg
  • Loading branch information
jeremy-rifkin committed Oct 5, 2024
1 parent ea238cf commit 9ce986e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
20 changes: 15 additions & 5 deletions src/components/anti-invite-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import * as Discord from "discord.js";
import { BotComponent } from "../bot-component.js";
import { departialize } from "../utils/discord.js";

const INVITE_RE = /(?:(discord(app)?|disboard)\.(gg|(com|org|me)\/(invite|server\/join))|gg)\/\S+/i;

export function should_block(content: string) {
return INVITE_RE.test(content);
const INVITE_RE =
/(?:(?:discord(?:app)?|disboard)\.(?:gg|(?:com|org|me)\/(?:invite|server\/join))|(?<!\w)\.gg)\/(\S+)/i;

const whitelist = [
"python",
"csharp",
"bVTPVpYVcv", // cuda
"Eb7P3wH", // graphics
];

export function match_invite(content: string): string | null {
const match = content.match(INVITE_RE);
return match ? match[1] : null;
}

export default class AntiInviteLinks extends BotComponent {
Expand All @@ -20,7 +29,8 @@ export default class AntiInviteLinks extends BotComponent {
if (this.wheatley.is_authorized_mod(message.author)) {
return;
}
if (should_block(message.content)) {
const match = match_invite(message.content);
if (match && !whitelist.includes(match)) {
const quote = await this.wheatley.make_quote_embeds([message]);
await message.delete();
assert(!(message.channel instanceof Discord.PartialGroupDMChannel));
Expand Down
52 changes: 27 additions & 25 deletions test/invite-links.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { describe, expect, it } from "vitest";

import { should_block } from "../src/components/anti-invite-links.js";
import { match_invite } from "../src/components/anti-invite-links.js";

describe("invite link tests", () => {
it("should block invite links", () => {
expect(should_block("discord.gg/foo")).to.equal(true);
expect(should_block("discord.com/invite/foo")).to.equal(true);
expect(should_block("discordapp.com/invite/foo")).to.equal(true);
expect(should_block("disboard.org/server/join/foo")).to.equal(true);
expect(should_block("discord.me/server/join/foo")).to.equal(true);
expect(should_block("discord.gg/f")).to.equal(true);
expect(should_block("foobar https://discord.gg/randomserver foobar")).to.equal(true);
expect(should_block("foobar discord.gg/randomserver foobar")).to.equal(true);
expect(should_block("foobar discord.gg/randomserver foobar")).to.equal(true);
expect(should_block("foobar discord.gg/T897FfR foobar")).to.equal(true);
expect(should_block("foobar discord.GG/random foobar")).to.equal(true);
expect(should_block("foobar discord.gg/12*(*^&^)asdggascn foobar")).to.equal(true);
expect(should_block("discord.gg/1")).to.equal(true);
expect(match_invite("discord.gg/foo")).to.equal("foo");
expect(match_invite("discord.com/invite/foo")).to.equal("foo");
expect(match_invite("discordapp.com/invite/foo")).to.equal("foo");
expect(match_invite("disboard.org/server/join/foo")).to.equal("foo");
expect(match_invite("discord.me/server/join/foo")).to.equal("foo");
expect(match_invite("discord.gg/f")).to.equal("f");
expect(match_invite("foo .gg/bar")).to.equal("bar");
expect(match_invite("foobar https://discord.gg/randomserver foobar")).to.equal("randomserver");
expect(match_invite("foobar discord.gg/randomserver foobar")).to.equal("randomserver");
expect(match_invite("foobar discord.gg/randomserver foobar")).to.equal("randomserver");
expect(match_invite("foobar discord.gg/T897FfR foobar")).to.equal("T897FfR");
expect(match_invite("foobar discord.GG/random foobar")).to.equal("random");
expect(match_invite("foobar discord.gg/12*(*^&^)asdggascn foobar")).to.equal("12*(*^&^)asdggascn");
expect(match_invite("discord.gg/1")).to.equal("1");
expect(
should_block(`
match_invite(`
!wp
# What Is Template Instantiation?
Expand Down Expand Up @@ -59,13 +60,13 @@ argument to \`print\`.
\`\`\`
42 Hello
\`\`\``),
).to.equal(true);
).to.equal("randomserver");
});

it("should not block normal messages", () => {
expect(should_block("foobar")).to.equal(false);
expect(match_invite("foobar")).to.equal(null);
expect(
should_block(`
match_invite(`
!wp
# What Is Template Instantiation?
Expand Down Expand Up @@ -105,13 +106,14 @@ argument to \`print\`.
\`\`\`
42 Hello
\`\`\``),
).to.equal(false);
expect(should_block(".gg/")).to.equal(false);
expect(should_block("foo .gg/ bar")).to.equal(false);
expect(should_block(`Check out https://stunlock.gg/posts/emscripten_with_cmake/ sounds like ...`)).to.equal(
false,
).to.equal(null);
expect(match_invite(".gg/")).to.equal(null);
expect(match_invite("foo .gg/ bar")).to.equal(null);
expect(match_invite("foo gg/bar")).to.equal(null);
expect(match_invite(`Check out https://stunlock.gg/posts/emscripten_with_cmake/ sounds like ...`)).to.equal(
null,
);
expect(should_block(`paste.gg/p/foobar`)).to.equal(false);
expect(should_block(`https://redirect.compiler.gg/foobar`)).to.equal(false);
expect(match_invite(`paste.gg/p/foobar`)).to.equal(null);
expect(match_invite(`https://redirect.compiler.gg/foobar`)).to.equal(null);
});
});

0 comments on commit 9ce986e

Please sign in to comment.