From fa4a844dcb2e73d0485297aeb7b69ae2791c2996 Mon Sep 17 00:00:00 2001 From: xHyroM Date: Fri, 25 Aug 2023 07:55:31 +0200 Subject: [PATCH] feat: nickcname moderation --- src/listeners/index.ts | 1 + src/listeners/nickname_moderation.ts | 25 +++++++++++++++++++++++++ src/util.ts | 6 ++++++ 3 files changed, 32 insertions(+) create mode 100644 src/listeners/nickname_moderation.ts diff --git a/src/listeners/index.ts b/src/listeners/index.ts index b3df2dd..29e628b 100644 --- a/src/listeners/index.ts +++ b/src/listeners/index.ts @@ -1,3 +1,4 @@ import "./ready.ts"; import "./interaction_create.ts"; import "./message_create.ts"; +import "./nickname_moderation.ts"; diff --git a/src/listeners/nickname_moderation.ts b/src/listeners/nickname_moderation.ts new file mode 100644 index 0000000..678afac --- /dev/null +++ b/src/listeners/nickname_moderation.ts @@ -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" + })); + } +} diff --git a/src/util.ts b/src/util.ts index 4d0777b..8a6b01d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,9 @@ export function safeSlice(array: T[], length: number) { return array.length > length ? array.slice(0, length) : array; } + +export async function silently(value: Promise) { + try { + await value; + } catch {} +}