Skip to content

Commit

Permalink
Feature/scoring (#24)
Browse files Browse the repository at this point in the history
* first pass

* Fixed

* Lint fixes
  • Loading branch information
zippy1981 committed Oct 17, 2023
1 parent f67e39e commit 1f7defc
Show file tree
Hide file tree
Showing 13 changed files with 543 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Expand Up @@ -12,6 +12,7 @@
"ecmaVersion": 2021
},
"rules": {
"semi": [2, "always"]
"semi": [2, "always"],
"quotes": [2, "single", { "avoidEscape": true }]
}
}
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
# score database
score.db3*

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node

Expand Down
13 changes: 7 additions & 6 deletions __tests__/config.test.js
Expand Up @@ -21,12 +21,13 @@ describe('Tests the config module', () => {
const config = require('../config');

expect(config).toEqual( {
"AllowConfigDump": false,
"MessageFetchCount": 50,
"OneBlockedPercent": 1,
"RealestOneBlockedPercent": 5,
"TheRealests": [
"kerouac5",
'AllowConfigDump': false,
'MessageFetchCount': 50,
'OneBlockedPercent': 1,
'ScoreDatabase': './score.db3',
'RealestOneBlockedPercent': 5,
'TheRealests': [
'kerouac5',
]
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/replacer.test.js
Expand Up @@ -9,7 +9,7 @@ describe('Tests the replacer module', () => {
'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'
].map(content => ({
content,
author: "author"
author: 'author'
}));

const channel = {
Expand Down
50 changes: 50 additions & 0 deletions __tests__/scoring.test.js
@@ -0,0 +1,50 @@
describe('Tests for the scoring module', () => {
let oldEnv;

beforeAll(() => {
oldEnv = process.env;
});

beforeEach(() => {
process.env.SCORE_DATABASE = ':memory:';
jest.resetModules();
});

afterAll(() => {
process.enve = oldEnv;
});

it('keeps score', async () => {
const { processScores, getScore } = require('../scoring');
await getScore('urch', score => {
expect(score).toBe(0);
});
[
'urch++',
'urch++',
'poly--',
'urch++',
'urch--'
].forEach(async (phrase) => {
await processScores({ content: phrase });
});
await getScore('urch', score => {
expect(score).toBe(2);
});
});

it('handles multiline scores', async () => {
const { processScores, getScore } = require('../scoring');
await getScore('urch', score => {
expect(score).toBe(0);
});

await processScores({ content: 'urch++\rpotato++\nurch++\r\nurch++' });
await getScore('urch', score => {
expect(score).toBe(3);
});
await getScore('potato', score => {
expect(score).toBe(1);
});
});
});
5 changes: 5 additions & 0 deletions config.js
Expand Up @@ -19,6 +19,11 @@ module.exports = {
* Number of messages to fetch when retrieving history.
*/
MessageFetchCount: (messesageFetchCount >= 0) ? messesageFetchCount : 50,

/**
* Path to the scoring database.
*/
ScoreDatabase: process.env.SCORE_DATABASE ?? './score.db3',

/**
* Percentage change of getting "who is one blocked message" for plebians who should be so lucky to be in
Expand Down
12 changes: 12 additions & 0 deletions index.js
@@ -1,6 +1,7 @@
const { Client, GatewayIntentBits } = require('discord.js');
const config = require('./config');
const { replaceFirstMessage } = require('./replacer');
const { processScores, getScore } = require('./scoring');


/**
Expand Down Expand Up @@ -63,6 +64,17 @@ client.on('messageCreate', async (initialQuery) => {
initialQuery.channel.send(initialQuery.author.toString() + ' nobody said that, dumb ass');
}
}
else if (initialQuery.content.indexOf('!score ') == 0)
{
const phrase = initialQuery.content.replace(/^!score/, '').trim();
getScore(phrase, score => {
initialQuery.channel.send(`Score *${phrase}*: ${score}`);
});
}
else
{
processScores(initialQuery);
}
});


Expand Down
25 changes: 0 additions & 25 deletions index.js.old

This file was deleted.

8 changes: 4 additions & 4 deletions jest.config.json
Expand Up @@ -8,10 +8,10 @@
"clearMocks": true,
"coverageThreshold": {
"global": {
"branches": 26,
"functions": 40,
"lines": 35,
"statements": 33
"branches": 43,
"functions": 53,
"lines": 55,
"statements": 53
}
}
}

0 comments on commit 1f7defc

Please sign in to comment.