zappyzep/backend/src/data/GuildAntiraidLevels.ts
Dragory 84135b201b
Add anti-raid levels to automod. Large refactor of spam detection. Add member_join and member_join_spam triggers.
Anti-raid levels don't by themselves do anything, but they can be
used in overrides to activate specific automod items.

Spam detection should now be more reliable and also combine further
spam messages after the initial detection into the archive.

Messages deleted by automod no longer create the normal deletion log
entry. Instead, the AUTOMOD_ACTION log entry contains the deleted
message or an archive if there are multiple (i.e. spam).
2020-01-26 19:54:32 +02:00

42 lines
1.1 KiB
TypeScript

import { BaseGuildRepository } from "./BaseGuildRepository";
import { getRepository, Repository } from "typeorm";
import { AntiraidLevel } from "./entities/AntiraidLevel";
export class GuildAntiraidLevels extends BaseGuildRepository {
protected antiraidLevels: Repository<AntiraidLevel>;
constructor(guildId: string) {
super(guildId);
this.antiraidLevels = getRepository(AntiraidLevel);
}
async get() {
const row = await this.antiraidLevels.findOne({
where: {
guild_id: this.guildId,
},
});
return row?.level ?? null;
}
async set(level: string | null) {
if (level === null) {
await this.antiraidLevels.delete({
guild_id: this.guildId,
});
} else {
// Upsert: https://stackoverflow.com/a/47064558/316944
await this.antiraidLevels
.createQueryBuilder()
.insert()
.values({
guild_id: this.guildId,
level,
})
.onConflict('("guild_id") DO UPDATE SET "guild_id" = :guildId')
.setParameter("guildId", this.guildId)
.execute();
}
}
}