Skip to content

Commit

Permalink
feat: moderate users with links disabled
Browse files Browse the repository at this point in the history
This commit will add a new rule to the antispam service that will reject
any message coming from members having the links-disabled role, no
matter where they point to.
  • Loading branch information
danirod committed Dec 6, 2020
1 parent c7c2423 commit 7bdd695
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 6 deletions.
211 changes: 207 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"discord.js-commando": "git://github.com/discordjs/Commando.git#198d7604e3725ee88dceab9b5f296edb1b7580a5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"get-urls": "^10.0.0",
"log4js": "^6.3.0",
"sqlite": "^2.9.3",
"uws": "^200.0.0",
Expand Down
25 changes: 23 additions & 2 deletions src/hooks/antispam.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Message } from "discord.js";
import getUrls from "get-urls";

import Member from "../lib/member";
import { WastebinModlogEvent } from "../lib/modlog";
import Server from "../lib/server";
Expand All @@ -19,13 +21,32 @@ const ruleset: { [reason: string]: RegExp[] } = {
],
};

function matches(message: string): string | undefined {
const disabledLinksReason = "el origen está retenido, un mod debería revisar los logs";

function matchesUrlInRuleset(message: string): string | undefined {
return Object.keys(ruleset).find((rule) => {
const regexps = ruleset[rule];
return regexps.some((regexp) => regexp.test(message));
});
}

function messageContainsURL(message: string): boolean {
return getUrls(message).size > 0;
}

function testModeration(message: Message): string | undefined {
const member = new Member(message.member);
const text = normalizeMessageContent(message);

if (!member.canPostLinks && messageContainsURL(text)) {
/* The member cannot post links. */
return disabledLinksReason;
} else {
/* The message may or may not contain a link to an invalid site. */
return matchesUrlInRuleset(text);
}
}

function isAllowed(message: Message): boolean {
if (message.author.bot) {
return true;
Expand Down Expand Up @@ -66,7 +87,7 @@ export default class AntispamService implements Hook {
if (isAllowed(message)) {
return; /* trusted member or bot */
}
const match = matches(normalizeMessageContent(message));
const match = testModeration(message);
if (match) {
const server = new Server(message.guild);

Expand Down
4 changes: 4 additions & 0 deletions src/lib/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export default class Member {
return this.hasRole(this.server.helperRole);
}

get canPostLinks(): boolean {
return !this.hasRole(this.server.linksDisabledRole);
}

get cooldown(): boolean {
if (!this.guildMember.joinedAt) {
/* TODO: Investigate why THIS happens. */
Expand Down
5 changes: 5 additions & 0 deletions src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ export default class Server {
return this.getRoleByName(trustedRoleName);
}

get linksDisabledRole(): Role {
const linksDisabledRole = process.env.LINKS_DISABLE_ROLE || "links-disabled";
return this.getRoleByName(linksDisabledRole);
}

get publicModlogChannel(): TextChannel {
const modlogChannelName = process.env.PUBLIC_MODLOG_CHANNEL || "public-modlog";
return this.getTextChannelByName(modlogChannelName);
Expand Down

0 comments on commit 7bdd695

Please sign in to comment.