Skip to content

Commit

Permalink
[1.7] Improved the cache
Browse files Browse the repository at this point in the history
- Improved the cache
- Fixed some issues with the cache
- Fixed some asynchronous file managing
  • Loading branch information
PetyXbron committed Nov 26, 2024
1 parent e536c2b commit 0c3bb35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "advancedreactions",
"version": "1.6",
"version": "1.7",
"description": "AdvancedReactions is a Discord bot with an advanced reaction roles system.",
"main": "index.js",
"type": "module",
Expand Down
34 changes: 19 additions & 15 deletions src/events/messageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import fsp from "fs/promises";
import path from "path";
import userCache from "../functions/cache.js";

Expand All @@ -7,7 +8,7 @@ export default async function (bot, r, u) {

if (userCache.has(u.id)) return r.users.remove(u.id);

userCache.set(u.id, true); //Add the user to the cacheF
userCache.set(u.id, true); //Add the user to the cache

let { message, emoji } = r;
const { guild } = message;
Expand All @@ -34,28 +35,30 @@ export default async function (bot, r, u) {
if (member.roles.cache.has(setup.roleID)) {
if (!db[db.indexOf(setup)].reacted.includes(member.id))
db[db.indexOf(setup)].reacted.push(u.id);
return fs.writeFileSync(
fs.writeFileSync(
guildPath,
JSON.stringify(db, null, 4)
);
return userCache.delete(u.id);
}

if (db[db.indexOf(setup)].reacted.includes(member.id)) {
const role = await guild.roles.fetch(setup.roleID, { cache: true, force: false });
if (!role || !role.editable) {
userCache.delete(u.id);
return r.users.remove(u.id); //Can the bot add/manage this role?
await r.users.remove(u.id); //Can the bot add/manage this role?
return userCache.delete(u.id);
}

return member.roles.add(
await member.roles.add(
role,
`${member.user.tag} reacted with ${emojiName} in ${message.channel.name}.`
);
return userCache.delete(u.id);
}

if (setup.limit && reacted.filter(u => ![setup.adminID, bot.user.id].includes(u)).length >= setup.limit) {//Ignore an admin's and/or a bot's reaction
userCache.delete(u.id);
return r.users.remove(u.id); //Has been the limit of reactions reached?
if (setup.limit && (reacted.filter(u => ![setup.adminID, bot.user.id].includes(u)).length >= setup.limit)) {//Ignore an admin's and/or a bot's reaction
await r.users.remove(u.id); //Has been the limit of reactions reached?
return userCache.delete(u.id);
}

if (setup.maxClaims) {
Expand All @@ -64,26 +67,27 @@ export default async function (bot, r, u) {
const { reacted } = oneSetupReaction;
if (reacted.includes(u.id)) alreadyClaimed++; //Did user react on this one?
if (alreadyClaimed >= setup.maxClaims) {
userCache.delete(u.id);
return r.users.remove(u.id);
await r.users.remove(u.id);
return userCache.delete(u.id);
}
}
}

const role = await guild.roles.fetch(setup.roleID, { cache: true, force: false });
if (!role || !role.editable) {
userCache.delete(u.id);
return r.users.remove(u.id); //Can the bot add/manage this role?
await r.users.remove(u.id); //Can the bot add/manage this role?
return userCache.delete(u.id);
}

await member.roles.add(
role,
`${member.user.tag} reacted with ${emojiName} in ${message.channel.name}.`
);

console.log("5")
if (!db[db.indexOf(setup)].reacted.includes(member.id))
db[db.indexOf(setup)].reacted.push(u.id);
fs.writeFileSync(
await fsp.writeFile(
guildPath,
JSON.stringify(db, null, 4)
);
Expand All @@ -109,7 +113,7 @@ export default async function (bot, r, u) {
} catch { }
}

userCache.delete(u.id); //Delete the user from the cache
return console.log(u.tag, "from", guild.name, "reacted with", emojiName);
console.log(u.tag, "from", guild.name, "reacted with", emojiName);
return userCache.delete(u.id);
}
}

0 comments on commit 0c3bb35

Please sign in to comment.