silvanus/slash-commands/compare.js

77 lines
3.3 KiB
JavaScript

const { SlashCommandBuilder } = require('discord.js');
const dbfn = require('../modules/dbfn.js');
const fn = require('../modules/functions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('compare')
.setDescription('See how your tree compares to other trees!'),
async execute(interaction) {
try {
await interaction.deferReply();
// Get the guildInfo from the database
dbfn.getGuildInfo(interaction.guildId).then(async getGuildInfoResponse => {
let guildInfo = getGuildInfoResponse.data;
// Find the most recent tree and leaderboard messages in their respective channels
const findMessagesResponse = await fn.messages.find(interaction, guildInfo);
if (findMessagesResponse.code == 1) {
guildInfo = findMessagesResponse.data;
// Parse the leaderboard message
await fn.rankings.parse(interaction, guildInfo);
// Build the string that shows the comparison // TODO Move the string building section to fn.builders?
const comparedRankings = await fn.rankings.compare(interaction, guildInfo);
// Build the Action Row that will contain the Refresh and Reset Ping buttons
const compareActionRow = await fn.builders.refreshAction(interaction.guildId);
const embed = fn.builders.comparisonEmbed(comparedRankings, compareActionRow);
await interaction.editReply(embed);
} else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status));
}
}).catch(async err => { // If we fail to fetch the guild's info from the database
// If the error is because the guild hasn't been setup yet, set it up
if (err === "There is no database entry for your guild yet. Try running /setup") {
// Create a basic guildInfo with blank data
let guildInfo = {
guildId: `${interaction.guildId}`,
treeName: "",
treeHeight: 0,
treeMessageId: "",
treeChannelId: `${interaction.channelId}`, // Use this interaction channel for the initial channel IDs
leaderboardMessageId: "",
leaderboardChannelId: `${interaction.channelId}`,
reminderMessage: "",
reminderChannelId: "",
remindedStatus: 0,
reminderOptIn: 0,
}
// Using the above guildInfo, try to find the Grow A Tree messages
const findMessagesResponse = await fn.messages.find(interaction, guildInfo);
guildInfo = findMessagesResponse.data;
if (findMessagesResponse.code == 1) {
// Build the string that shows the comparison // TODO Move the string building section to fn.builders?
const comparedRankings = await fn.rankings.compare(interaction, guildInfo);
// Build the Action Row that will contain the Refresh and Reset Ping buttons
const compareActionRow = await fn.builders.refreshAction(interaction.guildId);
const embed = fn.builders.comparisonEmbed(comparedRankings, compareActionRow);
await interaction.editReply(embed);
} else {
await interaction.editReply(fn.builders.errorEmbed(findMessagesResponse.status));
}
} else {
await interaction.editReply(fn.builders.errorEmbed("An unknown error occurred while running the compare command."));
console.error(err);
}
});
} catch (err) {
interaction.editReply(fn.builders.errorEmbed(err)).catch(err => {
console.error(err);
});
console.error(err);
}
},
};