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

Commit

Permalink
Economy #2 - Message Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
theADAMJR committed Jan 26, 2021
1 parent 5e337a4 commit 63094c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\bot.js"
"program": "${workspaceFolder}/bot.js"
}
]
}
7 changes: 2 additions & 5 deletions handlers/events/message-event.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Event = require('./event');
const { handleCommand } = require('../command-handler');
const logs = require('../../data/logs');
const users = require('../../data/users');
const economy = require('../../modules/economy');

module.exports = class extends Event {
on = 'message';
Expand All @@ -12,9 +12,6 @@ module.exports = class extends Event {
return await logs.add(msg.guild.id, 'commands');

await logs.add(msg.guild.id, 'messages');

const savedUser = await users.get(msg.author.id);
savedUser.coins += 5;
await savedUser.save();
await economy.handleMessage(msg);
}
}
30 changes: 30 additions & 0 deletions modules/economy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const users = require('../data/users');

module.exports = new class {
cooldowns = [];

async handleMessage(msg) {
if (this.inCooldown(msg.author.id)) return;
this.handleCooldown(msg.author.id);

const minLength = 5;
if (msg.content.length < minLength) return;

const savedUser = await users.get(msg.author.id);
savedUser.coins += 5;
await savedUser.save();
}

handleCooldown(userId) {
this.cooldowns.push(userId);

setTimeout(() => {
const index = this.cooldowns.indexOf(userId);
this.cooldowns.splice(index, 1);
}, 60 * 1000);
}

inCooldown(userId) {
return this.cooldowns.includes(userId);
}
}

0 comments on commit 63094c7

Please sign in to comment.