Skip to content

Commit

Permalink
added anti-self-karma spamming check;
Browse files Browse the repository at this point in the history
fixed a few bugs in the karma module and service;
  • Loading branch information
Pwuts committed May 27, 2020
1 parent 6425733 commit 73e766b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
4 changes: 1 addition & 3 deletions modules/hackerspace-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ module.exports = function HackerspaceModule(commandRouter)
else if (typeof space.open == 'boolean') {
msg.reply(`${space.name} is ${space.open ? '**open**' : '**dicht**'}`);
}
else {
msg.reply(`status van ${space.name} onbekend :(`);
}
else msg.reply(`status van ${space.name} onbekend :(`);
});

// Returns a list of all hackerspaces in NL
Expand Down
81 changes: 53 additions & 28 deletions modules/karma-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const KarmaService = require('../services/karma-service');
const isAdmin = require('../util/is-admin');
const Embed = require('discord.js').MessageEmbed;

module.exports = function KarmaModule(commandRouter, discord)
{
Expand All @@ -16,17 +17,31 @@ module.exports = function KarmaModule(commandRouter, discord)
const karmaCommand = Object.assign({}, karmaMessageMatch.groups);

const count = karmaCommand.count;
if (count == '__' && !isAdmin(message.author.id)) return; // only admins can reset karma
if (count == '__' && !isAdmin(message.author.id)) return;

let subject;
let subject, subjectUserId;
if (karmaCommand.emoji) {
subject = karmaCommand.emoji;
} else if (karmaCommand.user) {
subject = message.mentions.users.get(/\<@!?(?<id>\d{18})\>/.exec(karmaCommand.user).groups.id);
} else {
}
else if (karmaCommand.user) {
subjectUserId = /\<@!?(?<id>\d{18})\>/.exec(karmaCommand.user).groups.id;
subject = message.mentions.users.get(subjectUserId);
}
else {
subject = karmaCommand.subject.trim();
if (!subject.length) return;

const stored = KarmaService.get(subject);
if (stored && stored.userId) subject = `<@${stored.userId}>`;
if (stored.userId) {
subjectUserId = stored.userId;
subject = `<@${stored.userId}>`;
}
}

if (subjectUserId && count != '__' && subjectUserId == message.author.id) {
message.reply('Je kunt jezelf geen karma geven.')
.then(message => message.delete({ timeout: 10e3 }));
return;
}

const updated = count != '__' ? KarmaService.increment(subject, count == '++' ? 1 : -1) : KarmaService.reset(subject);
Expand All @@ -48,7 +63,8 @@ module.exports = function KarmaModule(commandRouter, discord)
const subject = command.args.trim();
const storedKarma = KarmaService.get(subject);
if (storedKarma.userId) {
message.reply(`<@${storedKarma.userId}> heeft ${storedKarma.karma} karma`);
message.reply((storedKarma.userId != message.author.id ?
`<@${storedKarma.userId}> heeft` : 'je hebt') + ` ${storedKarma.karma} karma`);
} else {
message.reply(`karma voor ${subject}: ${storedKarma.karma}`);
}
Expand All @@ -57,27 +73,36 @@ module.exports = function KarmaModule(commandRouter, discord)
// Returns the karma scoreboard
commandRouter.handler('karmalist', message => {
const karmaList = KarmaService.list();
message.reply({
embed: {
title: 'Karma scorebord',
fields: [
{
name: 'Gebruikers',
value: karmaList.users.map(entry => `${entry.name}: ${entry.karma}`).join('\n'),
inline: true
},
{
name: '\u200b',
value: '\u200b',
inline: true
},
{
name: 'Dingen',
value: karmaList.things.map(entry => `${entry.name}: ${entry.karma}`).join('\n'),
inline: true
}
]

if (karmaList.users.length || karmaList.things.length) {
const embed = new Embed({
title: 'Karma scorebord'
});

if (karmaList.users.length) {
embed.addField(
'Gebruikers',
karmaList.users.map(entry => `${entry.name}: ${entry.karma}`).join('\n'),
true
);

if (karmaList.things.length) {
embed.addField('\u200b', '\u200b', true); // horizontal spacer
}
}

if (karmaList.things.length) {
embed.addField(
'Dingen',
karmaList.things.map(entry => `${entry.name}: ${entry.karma}`).join('\n'),
true
);
}
});

message.reply({ embed });
} else {
message.reply('De karmalijst is nog leeg')
.then(message => message.delete({ timeout: 10e3 }));
}
});
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "https://pwuts.nl/"
},
"license": "MIT",
"version": "1.0.0",
"version": "1.1.0",
"repository": {
"type": "git",
"url": "git://github.com/Pwuts/discord-bot-template.git"
Expand All @@ -23,5 +23,7 @@
"mqtt": "^4.1.0",
"nodemon": "^2.0.4"
},
"scripts": {}
"scripts": {
"start": "nodemon --ignore karma.json bot.js"
}
}
4 changes: 4 additions & 0 deletions services/karma-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ const KarmaService = {
// Resets (and by default, removes) entry in karmaStore
reset(subject, rm = true)
{
if (typeof subject == 'object') {
subject = `<@${subject.id}>`;
}

const index = karmaStore.findIndex(entry => `<@${entry.userId}>` == subject || entry.name.toLowerCase() == subject.toLowerCase());

if (index > -1) {
Expand Down

0 comments on commit 73e766b

Please sign in to comment.