Files
toho-miku/util/users.js
2023-03-12 20:35:14 +01:00

134 lines
4.8 KiB
JavaScript

const { User, Guild } = require("../models");
const GeneralUtils = require("./general");
const { PATREON } = require("../config/constants");
module.exports = {
name: "UserUtils",
getUserByDiscordId: async function(discordId) {
return await User.findOne({
where: {
discordId: discordId
}
});
},
registrationCheck: async function(interaction) {
let user = await this.getUserByDiscordId(interaction.member.id);
if (user) {
return true;
}
if (!interaction.isButton() && interaction.commandName === "register") {
return true;
}
interaction.reply({
content: `${interaction.member} You are not registered, use the /register command`,
ephemeral: false
});
return false;
},
getCooldowns: async function(user) {
/* Returns an object with the following properties:
* now: the current time in milliseconds
--- For each key in cooldownKeys ---
* nextPullTimestamp: the next time the user can pull a card in milliseconds
* pullCooldown: time in milliseconds until the user can pull again
* pullCooldownFormatted: print friendly version of pullCooldown in hours and minutes
*/
const cooldownKeys = ["Pull", "Drop", "Daily"]
let reply = {
now: new Date().getTime()
};
for (key of cooldownKeys) {
reply[`next${key}Timestamp`] = user[`next${key}`].getTime();
let cooldown = Math.max(reply[`next${key}Timestamp`] - reply['now'], 0);
reply[`${key.toLowerCase()}Cooldown`] = cooldown;
let hours = Math.floor(cooldown / 3600000);
let minutes = Math.floor((cooldown % 3600000) / 60000);
let seconds = Math.floor((cooldown % 60000) / 1000);
if (cooldown > 0) {
reply[`${key.toLowerCase()}CooldownFormatted`] = `Next ${key} in ` + (hours >= 1 ? `${hours} hour${hours > 1 ? 's' : ''} ` : '') +
`${minutes} minute${minutes > 1 ? 's' : ''} ` +
`${seconds} second${seconds > 1 ? 's' : ''}`;
} else {
reply[`${key.toLowerCase()}CooldownFormatted`] = `${key} Ready!`;
}
}
return reply;
},
setCooldown: async function(user, cooldownType, cooldown) {
/* cooldownType: "pull", "drop", "daily"
* cooldown: time in milliseconds
*/
let newCooldown = new Date(new Date().getTime() + cooldown);
user[`next${cooldownType[0].toUpperCase() + cooldownType.slice(1)}`] = newCooldown;
await user.save();
},
getPermissionLevel: async function(user) {
/* THIS FUNCTION EXPECTS A DISCORD USER INSTANCE!
* Returns the permission level of the user
* 0 - no permissions
* 1 - guild permissions
* 2 - admin permissions
*/
let guild = await Guild.findOne({
where: {
guildId: user.guild.id
}
});
//Global Admin
let adminIDs = await GeneralUtils.getBotProperty("adminIDs");
if (adminIDs.includes(user.id)) {
return 2;
}
//Guild Admin if role is present
if(user._roles.includes(String(guild.adminRoleId))) {
return 1;
}
//or if user is owner
if(user.guild.ownerId === user.id) {
return 1;
}
//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 };
}
}