Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
feat: alert removal (#339) (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiejaoude authored Jun 11, 2021
1 parent 4a869c0 commit b65c1f4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 14 deletions.
86 changes: 84 additions & 2 deletions src/alexjs/alex.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,94 @@ import { On } from 'discord-nestjs';
import { Message } from 'discord.js';
import { AlexService } from './alex.service';

interface Notifications {
messageId: string;
channelId: string;
notificationId: string;
}

@Injectable()
export class AlexHandler {
private savedNotifications: Notifications[] = [];

constructor(private readonly alexService: AlexService) {}

@On({ event: 'message' })
onMessage(message: Message) {
return this.alexService.checkAlex(message);
async onMessage(message: Message) {
if (message.author.bot) {
return;
}

const notifications = this.alexService.check(message);

if (notifications.length) {
const sent = await message.channel.send(notifications[0]);
this.savedNotifications.push({
messageId: message.id,
channelId: message.channel.id,
notificationId: sent.id,
});
}

return;
}

@On({ event: 'messageUpdate' })
async onMessageUpdate(oldMessage: Message, newMessage: Message) {
if (newMessage.author.bot) {
return;
}

const notifications = this.alexService.check(newMessage);

// get the notification from the array
const targetNotification = this.savedNotifications.find(
(el) =>
el.messageId === newMessage.id &&
el.channelId === newMessage.channel.id,
);

// when edit results in new notification, but not old
if (!targetNotification && notifications.length) {
const sent = await newMessage.channel.send(notifications[0]);
this.savedNotifications.push({
messageId: newMessage.id,
channelId: newMessage.channel.id,
notificationId: sent.id,
});
return;
}

// when edit results in no new notification, but yes old
if (targetNotification && !notifications.length) {
const notificationMessage = await newMessage.channel.messages.fetch(
targetNotification.notificationId,
);
if (notificationMessage && notificationMessage.deletable) {
await notificationMessage.delete();

this.savedNotifications = this.savedNotifications.filter(
(el) =>
el.messageId !== newMessage.id ||
el.channelId !== newMessage.channel.id,
);

return;
}
}

// when edit results in new notification AND old notification
if (targetNotification && notifications.length) {
const notificationMessage = await newMessage.channel.messages.fetch(
targetNotification.notificationId,
);
if (notificationMessage) {
await notificationMessage.edit(notifications[0]);
return;
}
}

// when edit results in no new and no old
return;
}
}
25 changes: 13 additions & 12 deletions src/alexjs/alex.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { Message } from 'discord.js';
import { Message, MessageEmbed } from 'discord.js';
import * as alex from 'alex';
import config from '../config';

Expand All @@ -14,31 +14,29 @@ export class AlexService {
return str.replace(/\s{2,}/g, ' ');
}

public checkAlex(message: Message) {
if (message.author.bot) {
return;
}

public check(message: Message): MessageEmbed[] {
// Modify text by removing redundancy and special characters
const messageText = [
...new Set(this.stripSpecialCharacters(message.content).split(' ')),
].join(' ');
const alexMatch = alex.markdown(messageText, alexWhitelist as alex.Config)
.messages;

const notifications: MessageEmbed[] = [];
if (alexMatch.length) {
const embed = defaultEmbed(config.colors.alerts)
.setTitle(`You used the word "${alexMatch[0].actual}"`)
.setDescription(
'This might not be inclusive or welcoming language. Please consider 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);
});

return message.channel.send(embed);
notifications.push(embed);
}

const splitMessage = messageText.split(' ');
Expand All @@ -48,14 +46,17 @@ export class AlexService {
if (preventWords.includes(word.toLocaleLowerCase())) {
const embed = defaultEmbed(config.colors.alerts)
.setTitle(`You used the word "${word}"`)
.setDescription(
'This might not be inclusive or welcoming language',
.setDescription('This might not be inclusive or welcoming language')
.setAuthor(
message.author.username,
message.author.displayAvatarURL(),
);
return message.channel.send(embed);

notifications.push(embed);
}
});
}

return;
return notifications;
}
}

0 comments on commit b65c1f4

Please sign in to comment.