obama-bot/util/karma.js
2021-02-07 16:37:03 +01:00

153 lines
5.5 KiB
JavaScript

const { client, logger } = require('../index');
const Enmap = require('enmap');
// Stores the karma and coins of users
const karma = new Enmap({name: "karma"});
const coins = new Enmap({name: "coins"});
module.exports.karma = karma;
module.exports.coins = coins;
const upvoteID = '719181283393142786';
const downvoteID = '719181283774955540';
// Awards
const awards = {
"731192828415443116": {
name: "Silver",
cost: 100,
give: 0,
role: "733407208536277113"
},
"731192829262692372": {
name: "Gold",
cost: 500,
give: 100,
role: "733407216199008416"
},
"731192942080950333":{
name: "Platinum",
cost: 1800,
give: 700,
role: "733407213900660807"
},
"731508866273247252": {
name: "Argentium",
cost: 20000,
give: 2500,
role: "733407211178557583"
},
"739266335128944683": {
name: "Argentobamium",
cost: 100000,
give: 69420,
role: "739266563038904421"
}
}
let timeouts = {}
module.exports.run = () => {
client.on('messageReactionAdd', async function(reaction, user) {
// Fetch reaction and user if they are partial
if (reaction.partial) reaction = await reaction.fetch();
if (user.partial) reaction = await user.fetch();
if (reaction.message.author.bot) return; // Dont give karma to bots
await addToDatabase(reaction.message.author.id); // Add the user to the database
if (!reaction.emoji.id) return; // Return when emoji has no ID (is a "default" emoji)
if (user.bot) return; // Don't accept reactions from bots.
if (!reaction.message.guild) return; // Don't accept reactions from DMs
const message = reaction.message; // The message object the reaction was added to
if (reaction.emoji.id == upvoteID) {
karma.inc(message.author.id);
return;
}
else if (reaction.emoji.id == downvoteID) {
karma.dec(message.author.id);
return;
} else if (awards[reaction.emoji.id]) {
let award = awards[reaction.emoji.id];
let noTimeout = (!timeouts[user.id] || timeouts[user.id] < Date.now());
let perms = message.channel.permissionsFor(user.id);
if (!perms.has('SEND_MESSAGES') || !perms.has('ADD_REACTIONS')) {
logger.warn(`${user.tag} => Ignoring reaction in #${message.channel.name}: User is missing permission`);
reaction.users.remove(user.id);
return;
}
if (coins.get(user.id) < award.cost) {
if (noTimeout) message.channel.send(`${user}, you don't have sufficient coins to use this award.`);
reaction.users.remove(user.id);
timeouts[user.id] = Date.now() + 5000;
return;
}
if (message.author.id == user.id) {
reaction.users.remove(user.id);
if (noTimeout) message.channel.send(`${user}, why would you give an award to yourself? smh my head`);
timeouts[user.id] = Date.now() + 5000;
return;
}
coins.set(user.id, (coins.get(user.id) - award.cost));
coins.set(message.author.id, (coins.get(message.author.id) + award.give));
message.channel.send(`${user} just gave the ${client.emojis.cache.get(reaction.emoji.id).toString()} ${award.name} award (Price: ${award.cost} coins) to ${message.author}${award.give > 0 ? `. ${message.author.username} has received ${award.give} coins` : ''}!`);
require('./karma').updateRoles(user.id);
require('./karma').updateRoles(message.author.id);
}
});
client.on('messageReactionRemove', async function(reaction, user) {
// Fetch reaction and user if they are partial
if (reaction.partial) reaction = await reaction.fetch();
if (user.partial) reaction = await user.fetch();
await addToDatabase(reaction.message.author.id); // Add the user to the database
if (!reaction.emoji.id) return; // Return when emoji has no ID (is a "default" emoji)
if (user.bot) return; // Don't accept reactions from bots.
if (!reaction.message.guild) return; // Don't accept reactions from DMs
const message = reaction.message; // The message object the reaction was added to
if (reaction.emoji.id == upvoteID) {
karma.dec(message.author.id);
return;
}
else if (reaction.emoji.id == downvoteID) {
karma.dec(message.author.id);
return;
}
});
}
// Milestones for reaching certain karma values
function milestone(id) {
}
// Reset a user's coin/karma count when its not of type number
async function addToDatabase(id) {
if (!id) return;
if (typeof karma.get(id) != 'number') karma.set(id, 0);
if (typeof coins.get(id) != 'number') coins.set(id, 100);
return;
}
module.exports.updateRoles = function(id) {
let member = client.guilds.cache.get('653568812578373643').members.cache.get(id);
let membercoins = coins.get(member.id);
Object.keys(awards).forEach(awardid => {
let award = awards[awardid];
if (member.roles.cache.has(award.role)) {
if (membercoins < award.cost) member.roles.remove(award.role);
} else {
if (membercoins >= award.cost) member.roles.add(award.role);
}
});
}