Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature nameCleansing #201

Merged
merged 5 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import memberUpdateLogger from './memberUpdateLogger'
import messagePruner from './messagePruner'
import messageReporter from './messageReporter'
import nameChecker from './nameChecker'
import nameCleansing from './nameCleansing'
import nominations from './nominations'
import ping from './ping'
import preventEmojiSpam from './preventEmojiSpam'
Expand All @@ -20,6 +21,7 @@ export default [
messagePruner,
messageReporter,
nameChecker,
nameCleansing,
nominations,
ping,
preventEmojiSpam,
Expand Down
30 changes: 30 additions & 0 deletions src/features/nameCleansing/checkName.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, test } from 'vitest'

import {
checkDehoisted,
checkExclamationMark,
checkNameAgainstPatterns,
checkZalgo,
} from './checkName'

test('zalgo', async () => {
expect(checkZalgo('T̴̤̓e̴͎͗ś̶̖t̴͈̓')).toBeTruthy()
})

test('zalgo with whitespace', async () => {
expect(checkZalgo('T̴͍̱̑̕ ̵̹̠͋ḛ̷͝ ̴̗̅͠s̵͖̤̀̍ ̵̗̉͗ṫ̴̙̚')).toBeTruthy()
})

test('dehoisted', async () => {
expect(checkDehoisted('! Test')).toBeTruthy()
})

test('exclamation mark', async () => {
expect(checkExclamationMark('!')).toBeTruthy()
})

test('Bad Name', async () => {
expect(
checkNameAgainstPatterns('examplebadname', [{ regexp: 'examplebadname' }]),
).toBeTruthy()
})
45 changes: 45 additions & 0 deletions src/features/nameCleansing/checkName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { clean, isZalgo } from 'unzalgo'

import { Logger } from '@/types/Logger'
import { RuntimeConfigurationSchema } from '@/utils/RuntimeConfigurationSchema'

import { compiled } from './nameCleansing'

type Pattern = RuntimeConfigurationSchema['nameChecker']['patterns'][number]

export function checkNameAgainstPatterns(
name: string,
patterns: Pattern[],
log: Logger = console,
): boolean | undefined {
for (const pattern of patterns) {
try {
let regexp = compiled.get(pattern.regexp)
if (!regexp) {
regexp = new RegExp(pattern.regexp, 'i')
compiled.set(pattern.regexp, regexp)
}
if (regexp.test(name) || regexp.test(name.replaceAll(/\s+/g, ''))) {
return true
}
} catch (error) {
log.error(
`Unable to process pattern "${pattern}" against name "${name}"`,
error,
)
}
}
return false
}

export function checkZalgo(name: string) {
return isZalgo(name.trim())
}

export function checkDehoisted(name: string) {
return clean(name.trim()).startsWith('!')
}

export function checkExclamationMark(name: string) {
return clean(name.trim()) === '!'
}
leomotors marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions src/features/nameCleansing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Events } from 'discord.js'

import { definePlugin } from '@/types/definePlugin'

import { checkName } from './nameCleansing'

export default definePlugin({
name: 'nameCleansing',
setup: (pluginContext) => {
pluginContext.addEventHandler({
eventName: Events.GuildMemberAdd,
execute: async (botContext, member) => {
await checkName(member, botContext)
},
})

pluginContext.addEventHandler({
eventName: Events.GuildMemberUpdate,
execute: async (botContext, oldMember, newMember) => {
await checkName(newMember, botContext)
},
})

pluginContext.addEventHandler({
eventName: Events.MessageCreate,
execute: async (botContext, message) => {
if (!message.member) return
await checkName(message.member, botContext)
},
})
},
})
71 changes: 71 additions & 0 deletions src/features/nameCleansing/nameCleansing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { GuildMember } from 'discord.js'

import { clean } from 'unzalgo'

import { Environment } from '@/config'
import { BotContext } from '@/types/BotContext'

import {
checkDehoisted,
checkExclamationMark,
checkNameAgainstPatterns,
checkZalgo,
} from './checkName'

export const compiled = new Map<string, RegExp>()

export async function checkName(member: GuildMember, botContext: BotContext) {
const { log, runtimeConfiguration } = botContext
const config = runtimeConfiguration.data.nameCleansing
const configNameList = runtimeConfiguration.data.nameChecker
if (member.guild.id !== Environment.GUILD_ID) return

const {
enabled,
enabledCheckZalgo,
enabledCheckDehoisted,
enabledCheckExclamationMark,
enabledCheckBadName,
} = config
if (!enabled) {
return
}

const { patterns } = configNameList
if (!patterns) {
return
}

if (checkZalgo(member.displayName) && enabledCheckZalgo) {
member
.setNickname(clean(member.displayName), 'Zalgo Name Detacted')
.catch(() => {
log.info(`Can't change ${member.displayName}'s name`)
})
LagSeeN marked this conversation as resolved.
Show resolved Hide resolved
}

if (checkDehoisted(member.displayName) && enabledCheckDehoisted) {
member
.setNickname(member.user.username, 'Dehoisted Name Detacted')
.catch(() => {
log.info(`Can't change ${member.displayName}'s name`)
})
}

if (checkExclamationMark(member.displayName) && enabledCheckExclamationMark) {
member
.setNickname(member.user.username, 'Exclamation Mark Name Detacted')
.catch(() => {
log.info(`Can't change ${member.displayName}'s name`)
})
}

if (
checkNameAgainstPatterns(member.displayName, patterns, log) &&
enabledCheckBadName
) {
member.setNickname(member.user.username, 'Bad Name Detacted').catch(() => {
log.info(`Can't change ${member.displayName}'s name`)
})
}
LagSeeN marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions src/utils/RuntimeConfigurationSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ export const RuntimeConfigurationSchema = z
})
.default({}),

nameCleansing: z
.object({
enabled: z.boolean().default(false),
enabledCheckZalgo: z.boolean().default(false),
enabledCheckDehoisted: z.boolean().default(false),
enabledCheckExclamationMark: z.boolean().default(false),
enabledCheckBadName: z.boolean().default(false),
})
.default({}),

nominations: z
.object({
enabledRoles: z
Expand Down