import { Injectable } from '@nestjs/common' ; import { Message , MessageEmbed } from 'discord.js' ; import * as alex from 'alex' ; import config from '../config' ; const { defaultEmbed , alexWhitelist , preventWords } = config ; @ Injectable ( ) export class AlexService { private stripSpecialCharacters ( str ) { str = str . replace ( /[.,/\\#!$%?&*;:{}=\-_'"“”~()]/g , ' ' ) ; return str . replace ( /\s{2,}/g , ' ' ) ; } public check ( message ) { const messageText = [ ... new Set ( this . stripSpecialCharacters ( message . content ) . split ( ' ' ) ) , ] . join ( ' ' ) ; const alexMatch = alex . markdown ( messageText , alexWhitelist as alex . Config ) . messages ; const notifications = [ ] ; if ( alexMatch . length && ! config . allowedWords . includes ( alexMatch [ 0 ] . actual as string ) ) { const embed = defaultEmbed ( config . colors . alerts ) . setTitle ( `You used the word "${ alexMatch [ 0 ] . actual }"` ) . setDescription ( 'This might not be inclusive or welcoming language. Please update / edit your message with the following suggestions instead:' ) . setAuthor ( message . author . username , message . author . displayAvatarURL ( ) ) ; alexMatch . forEach ( ( suggestion ) => { const field = suggestion . note ? suggestion . note : 'See above ^^' ; return embed . addField ( suggestion . reason , field ) ; } ) ; notifications . push ( embed ) ; } const splitMessage = messageText . split ( ' ' ) ; if ( ! alexMatch . length ) { splitMessage . forEach ( ( word ) => { if ( preventWords . includes ( word . toLowerCase ( ) ) ) { const embed = defaultEmbed ( config . colors . alerts ) . setTitle ( `You used the word "${ word }"` ) . setDescription ( 'This might not be inclusive or welcoming language' ) . setAuthor ( message . author . username , message . author . displayAvatarURL ( ) ) ; notifications . push ( embed ) ; } } ) ; } return notifications ; } }