From 397ebde3e5ffb72cc93317ba1ccf50383544cb1f Mon Sep 17 00:00:00 2001 From: Minzkraut Date: Sun, 12 Mar 2023 20:35:14 +0100 Subject: [PATCH] UserUtil: Add getPatreonPerks helper and patreon debug command. --- commands/debug.js | 11 +++++++++++ config/constants.js | 3 ++- util/users.js | 27 +++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/commands/debug.js b/commands/debug.js index 2c1dc53..52e987f 100644 --- a/commands/debug.js +++ b/commands/debug.js @@ -2,6 +2,7 @@ const { SlashCommandBuilder, ComponentType, ActionRowBuilder, ButtonBuilder, But const { customAlphabet } = require("nanoid"); const { Card, User } = require("../models"); const { UserUtils, CardUtils, GeneralUtils } = require("../util"); +const { PATREON } = require("../config/constants"); const stores = require("../stores"); require('dotenv').config(); @@ -26,6 +27,7 @@ module.exports = { { name: 'add_secondary', value: 'add_secondary' }, { name: 'toggle_maintenance', value: 'toggle_maintenance' }, { name: 'store', value: 'store' }, + { name: 'patreon', value: 'patreon' } ) ) .addStringOption((option) => @@ -152,6 +154,15 @@ module.exports = { ephemeral: false }); break; + case "patreon": + interaction.editReply({ + content: `${JSON.stringify(user)}`, + ephemeral: false + }); + + let patreon = await UserUtils.getPatreonPerks(interaction.client, interaction.member); + interaction.channel.send(JSON.stringify(patreon)); + break; default: interaction.editReply({ content: `Your permission level is ${await UserUtils.getPermissionLevel(interaction.member)}`, diff --git a/config/constants.js b/config/constants.js index b2966d1..0a04b51 100644 --- a/config/constants.js +++ b/config/constants.js @@ -63,7 +63,8 @@ const QUALITY_VALUES = { } const PATREON = { - tiers: { + roleServer : '441300798819794944', + tiers : { 1 : { modifiers: { drops: 1, diff --git a/util/users.js b/util/users.js index 85b962a..3e863d8 100644 --- a/util/users.js +++ b/util/users.js @@ -1,5 +1,6 @@ const { User, Guild } = require("../models"); const GeneralUtils = require("./general"); +const { PATREON } = require("../config/constants"); module.exports = { name: "UserUtils", @@ -102,5 +103,31 @@ module.exports = { //Regular User return 0; + }, + + getPatreonPerks: async function(client, member) { + /** Returns the users highest Patreon tier and its associated perks + * client: Discord client instance available via interaction.client + * member: Discord member instance + * + * returns + * tier: 0 if not subscribed + * 1-n as per role mapping in the DB (e.g. {"1083018874263453868":1,"1083018984921759744":2}) + * perks: modifiers associated with the tier + */ + + let patreonRoles = await GeneralUtils.getBotProperty("patreonTierRoles"); + patreonRoles = JSON.parse(patreonRoles); + const guild = await client.guilds.fetch(PATREON.roleServer); + const guildMember = await guild.members.fetch(member.id); + let highestRole = 0; + for (const [role, tier] of Object.entries(patreonRoles)) { + const matchedRole = guildMember.roles.cache.get(role); + if(matchedRole) { + highestRole = Math.max(highestRole, tier); + } + } + let perks = PATREON.tiers[highestRole]; + return { "tier": highestRole, "perks": perks }; } }