feat: nickcname moderation

This commit is contained in:
Jozef Steinhübl 2023-08-25 07:55:31 +02:00
parent 47a186fd7d
commit fa4a844dcb
3 changed files with 32 additions and 0 deletions

View file

@ -1,3 +1,4 @@
import "./ready.ts"; import "./ready.ts";
import "./interaction_create.ts"; import "./interaction_create.ts";
import "./message_create.ts"; import "./message_create.ts";
import "./nickname_moderation.ts";

View file

@ -0,0 +1,25 @@
import { Events, GuildMember } from "discord.js";
import { defineListener } from "../loaders/listeners.ts";
import { silently } from "../util.ts";
defineListener({
event: Events.GuildMemberAdd,
run: (member: GuildMember) => moderateNick(member)
});
defineListener({
event: Events.GuildMemberUpdate,
run: (_: GuildMember, newMember: GuildMember) => moderateNick(newMember)
});
async function moderateNick(member: GuildMember) {
const name = member.displayName;
const normalizedName = name.normalize("NFKC").replace(/^[!$#@%^`&*()]+/, "");
if (name !== normalizedName) {
silently(member.edit({
nick: normalizedName,
reason: "lame username"
}));
}
}

View file

@ -1,3 +1,9 @@
export function safeSlice<T>(array: T[], length: number) { export function safeSlice<T>(array: T[], length: number) {
return array.length > length ? array.slice(0, length) : array; return array.length > length ? array.slice(0, length) : array;
} }
export async function silently<T>(value: Promise<T>) {
try {
await value;
} catch {}
}