Primitive banned words blocking

This commit is contained in:
Madeline 2022-09-11 23:19:55 +10:00
parent 4d23c4836d
commit b504edae92
8 changed files with 56 additions and 18 deletions

0
bundle/bannedWords Normal file
View File

View File

@ -7,7 +7,7 @@ import * as Gateway from "@fosscord/gateway";
import { CDNServer } from "@fosscord/cdn";
import express from "express";
import { green, bold, yellow } from "picocolors";
import { Config, initDatabase } from "@fosscord/util";
import { Config, initDatabase, BannedWords } from "@fosscord/util";
import * as Sentry from "@sentry/node";
import * as Tracing from "@sentry/tracing";
@ -35,6 +35,7 @@ process.on('SIGTERM', () => {
async function main() {
await initDatabase();
await Config.init();
await BannedWords.init();
// only set endpointPublic, if not already set
await Config.set({
cdn: {

View File

@ -1,6 +1,5 @@
import { PublicUser, User } from "./User";
import { Message } from "./Message";
import { BaseClass } from "./BaseClass";
import {
Column,
Entity,
@ -13,7 +12,7 @@ import {
RelationId,
} from "typeorm";
import { Guild } from "./Guild";
import { Config, emitEvent } from "../util";
import { Config, emitEvent, BannedWords, FieldErrors } from "../util";
import {
GuildCreateEvent,
GuildDeleteEvent,
@ -21,6 +20,7 @@ import {
GuildMemberRemoveEvent,
GuildMemberUpdateEvent,
MessageCreateEvent,
} from "../interfaces";
import { HTTPError } from "lambert-server";
import { Role } from "./Role";
@ -73,6 +73,11 @@ export class Member extends BaseClassWithoutId {
@Column({ nullable: true })
nick?: string;
setNick(val: string) {
if (BannedWords.find(val)) throw FieldErrors({ nick: { message: "Bad nickname", code: "INVALID_NICKNAME" } });
this.nick = val;
}
@JoinTable({
name: "member_roles",
joinColumn: { name: "index", referencedColumnName: "index" },

View File

@ -8,7 +8,6 @@ import {
Column,
CreateDateColumn,
Entity,
FindConditions,
Index,
JoinColumn,
JoinTable,
@ -16,14 +15,14 @@ import {
ManyToOne,
OneToMany,
RelationId,
RemoveOptions,
UpdateDateColumn,
} from "typeorm";
import { BaseClass } from "./BaseClass";
import { Guild } from "./Guild";
import { Webhook } from "./Webhook";
import { Sticker } from "./Sticker";
import { Attachment } from "./Attachment";
import { BannedWords } from "../util";
import { HTTPError } from "lambert-server";
export enum MessageType {
DEFAULT = 0,
@ -117,6 +116,11 @@ export class Message extends BaseClass {
@Column({ nullable: true, type: process.env.PRODUCTION ? "longtext" : undefined })
content?: string;
setContent(val: string) {
if (BannedWords.find(val)) throw new HTTPError("Message was blocked by automatic moderation", 200000);
this.content = val;
}
@Column()
@CreateDateColumn()
timestamp: Date;

View File

@ -3,9 +3,8 @@ import { BaseClass } from "./BaseClass";
import { BitField } from "../util/BitField";
import { Relationship } from "./Relationship";
import { ConnectedAccount } from "./ConnectedAccount";
import { Config, FieldErrors, Snowflake, trimSpecial } from "..";
import { Config, FieldErrors, Snowflake, trimSpecial, BannedWords } from "..";
import { Member, Session } from ".";
import { Note } from "./Note";
export enum PublicUserEnum {
username,
@ -60,6 +59,11 @@ export class User extends BaseClass {
@Column()
username: string; // username max length 32, min 2 (should be configurable)
setUsername(val: string) {
if (BannedWords.find(val)) throw FieldErrors({ username: { message: "Bad username", code: "INVALID_USERNAME" } });
this.username = val;
}
@Column()
discriminator: string; // opaque string: 4 digits on discord.com
@ -366,7 +370,7 @@ export interface UserSettings {
disable_games_tab: boolean;
enable_tts_command: boolean;
explicit_content_filter: number;
friend_source_flags: { all: boolean };
friend_source_flags: { all: boolean; };
gateway_connected: boolean;
gif_auto_play: boolean;
// every top guild is displayed as a "folder"

View File

@ -0,0 +1,23 @@
import fs from "fs/promises";
import path from "path";
var words: string[];
export const BannedWords = {
init: async function init() {
if (words) return words;
const file = (await fs.readFile(path.join(process.cwd(), "bannedWords"))).toString();
if (!file) {
words = [];
return [];
}
words = file.trim().split("\n");
return words;
},
get: () => words,
find: (val: string) => {
return words.some(x => val.indexOf(x) != -1);
}
};

View File

@ -20,3 +20,4 @@ export * from "./String";
export * from "./Array";
export * from "./TraverseDirectory";
export * from "./InvisibleCharacters";
export * from "./BannedWords";