Added redis caching to guild top command
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const { getUUID, getPlayer, getGuild, getIGN } = require("../../utils/utils.js")
|
const { getUUID, getPlayer, getGuild, getIGN } = require("../../utils/utils.js")
|
||||||
const { color, devMessage } = require("../../../config/options.json")
|
const { color, devMessage } = require("../../../config/options.json")
|
||||||
const { admin } = require("../../../config/roles.json")
|
|
||||||
const { ChannelType } = require("discord.js")
|
const { ChannelType } = require("discord.js")
|
||||||
|
const { redis } = require("../../utils/redis.js")
|
||||||
|
|
||||||
/** @param { import('discord.js').ChatInputCommandInteraction } interaction */
|
/** @param { import('discord.js').ChatInputCommandInteraction } interaction */
|
||||||
|
|
||||||
@@ -15,19 +15,7 @@ async function guildTop(interaction) {
|
|||||||
if (interaction.channel.type === ChannelType.DM) {
|
if (interaction.channel.type === ChannelType.DM) {
|
||||||
interaction.editReply({
|
interaction.editReply({
|
||||||
embeds: [{
|
embeds: [{
|
||||||
description: "You can't use this command in DMs!\n" +
|
description: "You can't use this command in DMs!",
|
||||||
"While taken checks will this rate limit the bot",
|
|
||||||
color: embedColor
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!interaction.member.roles.cache.has(admin)) {
|
|
||||||
await interaction.editReply({
|
|
||||||
embeds: [{
|
|
||||||
description: "Command temporarily disabled\n" +
|
|
||||||
"While taken checks will this rate limit the bot",
|
|
||||||
color: embedColor
|
color: embedColor
|
||||||
}]
|
}]
|
||||||
})
|
})
|
||||||
@@ -128,6 +116,9 @@ async function guildTop(interaction) {
|
|||||||
|
|
||||||
const guildName = guild.name
|
const guildName = guild.name
|
||||||
const guildMembers = guild.members
|
const guildMembers = guild.members
|
||||||
|
const guildId = guild._id
|
||||||
|
|
||||||
|
const cachedData = await redis.get("guildTop+" + guildId)
|
||||||
|
|
||||||
const gexpTodayUnformatted = guildMembers.map((member) => {
|
const gexpTodayUnformatted = guildMembers.map((member) => {
|
||||||
return member.expHistory[Object.keys(member.expHistory)[0]]
|
return member.expHistory[Object.keys(member.expHistory)[0]]
|
||||||
@@ -152,22 +143,49 @@ async function guildTop(interaction) {
|
|||||||
amount = 1
|
amount = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
await interaction.editReply({
|
let cacheStatus
|
||||||
embeds: [{
|
let guildData = []
|
||||||
description: "Fetching the top " + amount + " members of " + guildName + "...",
|
|
||||||
color: embedColor
|
|
||||||
}]
|
|
||||||
})
|
|
||||||
|
|
||||||
const fieldsValueRaw = []
|
const fieldsValueRaw = []
|
||||||
const allMembersSorted = allMembersDailyGEXP.sort((a, b) => b.gexp - a.gexp)
|
const allMembersSorted = allMembersDailyGEXP.sort((a, b) => b.gexp - a.gexp)
|
||||||
const topMembers = allMembersSorted.slice(0, amount)
|
|
||||||
|
if (!cachedData) {
|
||||||
|
cacheStatus = false
|
||||||
|
await interaction.editReply({
|
||||||
|
embeds: [{
|
||||||
|
description: "Fetching the top " + amount + " members of " + guildName + "...",
|
||||||
|
color: embedColor
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
|
||||||
|
for (let i = 0; i < allMembersSorted.length; i++) {
|
||||||
|
const ign = await getIGN(allMembersSorted[i].uuid)
|
||||||
|
const gexpUnformatted = allMembersSorted[i].gexp
|
||||||
|
const gexp = new Intl.NumberFormat("en-US").format(gexpUnformatted)
|
||||||
|
|
||||||
|
guildData.push({
|
||||||
|
ign: ign,
|
||||||
|
gexp: gexp,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await redis.set("guildTop+" + guildId, JSON.stringify(guildData), "EX", 60 * 30)
|
||||||
|
} else {
|
||||||
|
cacheStatus = true
|
||||||
|
await interaction.editReply({
|
||||||
|
embeds: [{
|
||||||
|
description: "Fetching the top " + amount + " members of " + guildName + "using cache...",
|
||||||
|
color: embedColor,
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
guildData = JSON.parse(cachedData)
|
||||||
|
}
|
||||||
|
|
||||||
|
const topMembers = guildData.slice(0, amount)
|
||||||
const sliceSize = amount / 3
|
const sliceSize = amount / 3
|
||||||
|
|
||||||
for (let i = 0; i < amount; i++) {
|
for (let i = 0; i < amount; i++) {
|
||||||
const ign = await getIGN(topMembers[i].uuid)
|
const ign = topMembers[i].ign
|
||||||
const gexpUnformatted = topMembers[i].gexp
|
const gexp = topMembers[i].gexp
|
||||||
const gexp = new Intl.NumberFormat("en-US").format(gexpUnformatted)
|
|
||||||
|
|
||||||
const position = i + 1
|
const position = i + 1
|
||||||
|
|
||||||
@@ -189,6 +207,7 @@ async function guildTop(interaction) {
|
|||||||
|
|
||||||
const footerText = interaction.guild ? interaction.guild.name : interaction.user.username
|
const footerText = interaction.guild ? interaction.guild.name : interaction.user.username
|
||||||
const footerIcon = interaction.guild ? interaction.guild.iconURL({ dynamic: true }) : interaction.user.avatarURL({ dynamic: true })
|
const footerIcon = interaction.guild ? interaction.guild.iconURL({ dynamic: true }) : interaction.user.avatarURL({ dynamic: true })
|
||||||
|
const cacheStatusText = cacheStatus ? " | [Cache]" : ""
|
||||||
|
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [{
|
embeds: [{
|
||||||
@@ -198,7 +217,7 @@ async function guildTop(interaction) {
|
|||||||
color: embedColor,
|
color: embedColor,
|
||||||
fields: newList,
|
fields: newList,
|
||||||
footer: {
|
footer: {
|
||||||
text: footerText + " | " + devMessage,
|
text: footerText + " | " + devMessage + cacheStatusText,
|
||||||
icon_url: footerIcon
|
icon_url: footerIcon
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
|||||||
Reference in New Issue
Block a user