Skip to content

Commit

Permalink
Fixes #25 (#26)
Browse files Browse the repository at this point in the history
* Fixes #25

* Fix the build and #22

* fix build

* fixes #18
  • Loading branch information
zippy1981 committed Oct 21, 2023
1 parent 1f7defc commit dd24482
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"rules": {
"semi": [2, "always"],
"quotes": [2, "single", { "avoidEscape": true }]
"quotes": [2, "single", { "avoidEscape": false }]
}
}
52 changes: 50 additions & 2 deletions __tests__/replacer.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { replaceFirstMessage } = require('../replacer');
const { replaceFirstMessage, splitReplaceCommand, extractUrls } = require('../replacer');
describe('Tests the replacer module', () => {
const messages = [
'No I used this for my demo so I could justify going hog wild',
'lol is this why you went so hog wild getting the environment set up like a real project instead of brans script kiddy level hackery?',
'!s hog wild/to the prom with another dude',
'!s a real project/your boss made you',
'We never added it officially. I made a hacky one that I never checked in, and before I did zippy made a big refactor and I never integrated',
'I guess I could get back to working on the bot but honestly I haven’t written code for a living since 2006, zippy would run circles around me'
'I guess I could get back to working on the bot but honestly I haven’t written code for a living since 2006, zippy would run circles around me',
'oo https://www.google.com/search?q=aaron+burr&sca_esv=575309331&sxsrf=AM9HkKngs20KZwsuZ8WffUtq81ntoB-7ww%3A1697847658021&source=hp&ei=aRkzZeaKOciGptQPoJy78Ag&iflsig=AO6bgOgAAAAAZTMnet89R6hwn_gqlPxJYlrXn89wh42m&ved=0ahUKEwim46K074WCAxVIg4kEHSDODo4Q4dUDCA0&uact=5&oq=aaron+burr&gs_lp=Egdnd3Mtd2l6IgphYXJvbiBidXJyMgsQLhiABBixAxiDATIFEAAYgAQyCxAAGIAEGLEDGIMBMhEQLhiABBjHARivARiYBRibBTIIEC4YgAQYsQMyBRAAGIAEMgUQLhiABDIFEAAYgAQyBRAAGIAEMgsQLhivARjHARiABEiJGVCoBFi8EXABeACQAQCYAVagAZQFqgECMTC4AQPIAQD4AQGoAgrCAg0QLhjHARjRAxjqAhgnwgIHECMY6gIYJ8ICDRAuGMcBGK8BGOoCGCfCAhAQABgDGI8BGOUCGOoCGIwDwgIREC4YgAQYsQMYgwEYxwEY0QPCAgsQLhiKBRixAxiDAcICDhAuGIAEGLEDGMcBGNEDwgILEAAYigUYsQMYgwHCAgsQLhiABBjHARjRA8ICCBAAGIAEGLEDwgIIEC4YsQMYgAQ&sclient=gws-wiz oo'
].map(content => ({
content,
author: 'author'
Expand All @@ -15,6 +16,34 @@ describe('Tests the replacer module', () => {
const channel = {
send: jest.fn()
};

it('allows the first character of a search to be whitespace', () => {
const sut = splitReplaceCommand('!s a/b');

expect('a'.replace(sut.search, sut.replacement)).toBe('a');
expect(' a'.replace(sut.search, sut.replacement)).toBe('b');
});

it('tests that multiple concurrent replacementsi n asingle string get formatted correctly', () => {
const sut = splitReplaceCommand('!s z/t');
const messages = [{
content: 'nice peppy buzz',
author: 'author'
}];
const expected = 'author nice peppy bu**tt**';
const actual = replaceFirstMessage(messages, sut.search, sut.replacement, channel);

expect(actual).toBe(false);
expect(channel.send).toBeCalledWith(expected);
});

it.each(['', null, undefined, false, 0])('tests the case for no replacement', (emptyIshStringIsh) => {
const regex = new RegExp('hog wild', 'gi');
const actual = replaceFirstMessage(messages, regex, emptyIshStringIsh, channel);

expect(actual).toBe(false);
expect(channel.send).toBeCalledWith('author No I used this for my demo so I could justify going ');
});

it('tests that the first match is what is returned', () => {
const regex = new RegExp('hog wild', 'gi');
Expand All @@ -31,4 +60,23 @@ describe('Tests the replacer module', () => {
expect(actual).toBe(true);
expect(channel.send).not.toBeCalled();
});

it('cleanses string and returns URLs', () => {
const inputString = 'This is a sample string with a URL https://www.example.com and another URL http://www.example.org';
const expectedOutput = {
cleansed: 'This is a sample string with a URL |{|url|}| and another URL |{|url|}|',
urls: ['https://www.example.com', 'http://www.example.org']
};
expect(extractUrls(inputString)).toEqual(expectedOutput);
});

it('does a replacement but ignored a url', () => {
const expected = 'author **aa** https://www.google.com/search?q=aaron+burr&sca_esv=575309331&sxsrf=AM9HkKngs20KZwsuZ8WffUtq81ntoB-7ww%3A1697847658021&source=hp&ei=aRkzZeaKOciGptQPoJy78Ag&iflsig=AO6bgOgAAAAAZTMnet89R6hwn_gqlPxJYlrXn89wh42m&ved=0ahUKEwim46K074WCAxVIg4kEHSDODo4Q4dUDCA0&uact=5&oq=aaron+burr&gs_lp=Egdnd3Mtd2l6IgphYXJvbiBidXJyMgsQLhiABBixAxiDATIFEAAYgAQyCxAAGIAEGLEDGIMBMhEQLhiABBjHARivARiYBRibBTIIEC4YgAQYsQMyBRAAGIAEMgUQLhiABDIFEAAYgAQyBRAAGIAEMgsQLhivARjHARiABEiJGVCoBFi8EXABeACQAQCYAVagAZQFqgECMTC4AQPIAQD4AQGoAgrCAg0QLhjHARjRAxjqAhgnwgIHECMY6gIYJ8ICDRAuGMcBGK8BGOoCGCfCAhAQABgDGI8BGOUCGOoCGIwDwgIREC4YgAQYsQMYgwEYxwEY0QPCAgsQLhiKBRixAxiDAcICDhAuGIAEGLEDGMcBGNEDwgILEAAYigUYsQMYgwHCAgsQLhiABBjHARjRA8ICCBAAGIAEGLEDwgIIEC4YsQMYgAQ&sclient=gws-wiz **aa**';
const sut = splitReplaceCommand('!s oo/aa');
const actual = replaceFirstMessage(messages, sut.search, sut.replacement, channel);

expect(actual).toBe(false);
expect(channel.send).toBeCalledWith(expected);
});

});
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Client, GatewayIntentBits } = require('discord.js');
const config = require('./config');
const { replaceFirstMessage } = require('./replacer');
const { replaceFirstMessage, splitReplaceCommand } = require('./replacer');
const { processScores, getScore } = require('./scoring');


Expand Down Expand Up @@ -50,16 +50,13 @@ client.on('messageCreate', async (initialQuery) => {
{
initialQuery.channel.send(initialQuery.author.toString() + ' who is one blocked message');
return;
}

var response = initialQuery.content.replace(/!s */, '').split('/');
const regex = new RegExp(response[0].unicodeToMerica(), 'gi');
}

let channel = initialQuery.channel;


const messages = await channel.messages.fetch({ limit: config.MessageFetchCount});
const failedToFind = replaceFirstMessage(messages, regex, response[1], channel);
const splitMessage = splitReplaceCommand(initialQuery.content);
const failedToFind = replaceFirstMessage(messages, splitMessage.regex, splitMessage.replacement, channel);
if(failedToFind) {
initialQuery.channel.send(initialQuery.author.toString() + ' nobody said that, dumb ass');
}
Expand Down
8 changes: 4 additions & 4 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"clearMocks": true,
"coverageThreshold": {
"global": {
"branches": 43,
"functions": 53,
"lines": 55,
"statements": 53
"branches": 45,
"functions": 61,
"lines": 60,
"statements": 59
}
}
}
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 47 additions & 6 deletions replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
function cleanseString(strInput) {
return strInput
.replace(/[\u201C\u201D]/g, '"')
.replace(/[\u2018\u2019]/g, "'")
.replace(/[\u2018\u2019]/g, '\'')
.replace(/\u2026/g, '...')
.replace(/\u2013/g, '-')
.replace(/\u2014/g, '--');
Expand All @@ -19,6 +19,21 @@ String.prototype.unicodeToMerica = function () {
return cleanseString(this);
};

/**
* Takes a string as input and outputs an object with two properties: `cleansed` and `urls`.
* The `cleansed` property contains the original string but with all URLs replaced with `|{|url|}|`.
* The `urls` property is an array of all removed URLs.
*
* @param {string} inputString - The input string to cleanse.
* @returns {{cleansed: string, urls: string[]}} - An object with two properties: `cleansed` and `urls`.
*/
function extractUrls(inputString) {
const urlRegex = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
const urls = inputString.match(urlRegex);
const cleansed = inputString.replace(urlRegex, '|{|url|}|');
return { cleansed, urls };
}

/**
* Does a replace on the first message that matches regex
*
Expand All @@ -36,17 +51,25 @@ function replaceFirstMessage(messages, regex, replacement, channel) {

return true;
}
const cleansedMessageText = msg.content.unicodeToMerica();
if(cleansedMessageText.search(regex) > -1) {

const cleansedMessage = extractUrls(msg.content.unicodeToMerica());
if(cleansedMessage.cleansed.search(regex) > -1) {
console.log('Match found for message ' + msg.content);

let replacePhrase = '';
if(replacement.length > 0) {
replacePhrase = cleansedMessageText.replace(regex, '**' + replacement + '**');
if(replacement?.length > 0) {
replacePhrase = cleansedMessage.cleansed
.replace(regex, '\v' + replacement + '\v')
.replace('\v\v', '')
.replace(/\v/g, '**');

}
else {
replacePhrase = msg.content.replace(regex, '');
}
cleansedMessage.urls?.forEach(url => {
replacePhrase = replacePhrase.replace('|{|url|}|', url);
});
channel.send(msg.author.toString() + ' ' + replacePhrase);

return false;
Expand All @@ -61,6 +84,24 @@ function replaceFirstMessage(messages, regex, replacement, channel) {

}

/**
* Takes the replace message and turn it into a searh regex and a replace message
*
* @param {string} replaceCommand
* @returns {{search:RegExp, replacement:string}}
*/
function splitReplaceCommand(replaceCommand) {
var response = replaceCommand.replace(/!s /, '').split('/');
const search = new RegExp(response[0].unicodeToMerica(), 'gi');

return {
search,
replacement: response[1]
};
}

module.exports = {
replaceFirstMessage
extractUrls,
replaceFirstMessage,
splitReplaceCommand
};

0 comments on commit dd24482

Please sign in to comment.