Skip to content

Commit 40f6e39

Browse files
committed
feat: download user data button
1 parent ddfe1ec commit 40f6e39

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { AttachmentBuilder } = require("discord.js");
2+
const Component = require("../../../structures/base/BaseComponent");
3+
const { ObjectId } = require("mongodb");
4+
5+
class DownloadDataButton extends Component {
6+
constructor(client) {
7+
super(client, {
8+
name: "download-data",
9+
category: "team",
10+
cooldown: 5,
11+
});
12+
}
13+
14+
/**
15+
* @param {import("discord.js").ButtonInteraction} interaction
16+
*/
17+
async run(interaction) {
18+
19+
await interaction.deferReply({ ephemeral: true });
20+
21+
const userID = interaction.message.embeds[0].author.name.split(" | ")[1];
22+
23+
const users = await this.HackRUBot.db.getCollection("users");
24+
const user = await users.findOne({ _id: new ObjectId(userID) });
25+
26+
if (!user) return interaction.editReply({ embeds: [this.HackRUBot.util.errorEmbed("No user found with this ID.")] });
27+
28+
const hideProps = (key, value) => {
29+
if (["password", "auth", "token"].some(k => key.includes(k))) return undefined;
30+
else return value;
31+
};
32+
33+
const data = new AttachmentBuilder()
34+
.setFile(Buffer.from(JSON.stringify(user, hideProps, 2)))
35+
.setName(`HackRU-${user.email}.json`);
36+
37+
interaction.editReply({ files: [data] });
38+
39+
return;
40+
41+
}
42+
}
43+
44+
module.exports = DownloadDataButton;

bot/interactions/slashcommands/get-user.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js");
1+
const { EmbedBuilder, ApplicationCommandOptionType, ButtonBuilder, ActionRowBuilder } = require("discord.js");
22
const SlashCommand = require("../../../structures/base/BaseSlashCommand");
33

44
class GetUserCommand extends SlashCommand {
@@ -37,23 +37,23 @@ class GetUserCommand extends SlashCommand {
3737
if (!user) return interaction.editReply({ embeds: [this.HackRUBot.util.errorEmbed("No user found with specified email.")] });
3838

3939
const userEmbed = new EmbedBuilder()
40-
.setAuthor({ name: "HackRU User Information", iconURL: interaction.guild.iconURL() })
40+
.setAuthor({ name: `HackRU User Information | ${user._id}`, iconURL: interaction.guild.iconURL() })
4141
.setTitle(user.first_name + " " + user.last_name)
42-
.setDescription(`**Email:** ${user.email}\n**Phone Number:** ${user.phone_number}`)
42+
.setDescription(`**Email:** ${user.email}\n**Phone Number:** ${user.phone_number || "UNDEFINED"}`)
4343
.setFields([
4444
{ name: "Status:", value: `\`${user.registration_status}\``, inline: true },
4545
{ name: "Created At:", value: user.created_at ? this.HackRUBot.util.createTimestamp(user.created_at) : "UNDEFINED", inline: true },
4646
{ name: "Registered At:", value: user.registered_at ? this.HackRUBot.util.createTimestamp(user.registered_at) : "UNDEFINED", inline: true },
4747
{ name: "Role(s):", value: Object.entries(user.role).filter(r => r[1] == true).map(r => r[0]).join(", ") || "N/A" },
48-
{ name: "Gender:", value: user.gender, inline: true },
49-
{ name: "Ethnicity:", value: user.ethnicity, inline: true },
50-
{ name: "DOB:", value: user.date_of_birth, inline: true },
51-
{ name: "School:", value: user.school },
52-
{ name: "Major:", value: user.major, inline: true },
53-
{ name: "Grad Year:", value: user.grad_year, inline: true },
54-
{ name: "Study Level:", value: user.level_of_study, inline: true },
55-
{ name: "Votes:", value: user.votes?.toString(), inline: true },
56-
{ name: "Shirt Size:", value: user.shirt_size, inline: true },
48+
{ name: "Gender:", value: user.gender || "UNDEFINED", inline: true },
49+
{ name: "Ethnicity:", value: user.ethnicity || "UNDEFINED", inline: true },
50+
{ name: "DOB:", value: user.date_of_birth || "UNDEFINED", inline: true },
51+
{ name: "School:", value: user.school || "UNDEFINED" },
52+
{ name: "Major:", value: user.major || "UNDEFINED", inline: true },
53+
{ name: "Grad Year:", value: user.grad_year || "UNDEFINED", inline: true },
54+
{ name: "Study Level:", value: user.level_of_study || "UNDEFINED", inline: true },
55+
{ name: "Votes:", value: user.votes?.toString() || "UNDEFINED", inline: true },
56+
{ name: "Shirt Size:", value: user.shirt_size || "UNDEFINED", inline: true },
5757
{ name: "GitHub:", value: user.github || "UNDEFINED", inline: true },
5858
{ name: "Dietary Restrictions:", value: user.dietary_restrictions || "N/A", inline: true },
5959
{ name: "Special Needs:", value: user.special_needs || "N/A", inline: true },
@@ -64,7 +64,12 @@ class GetUserCommand extends SlashCommand {
6464
.setFooter({ text: "Data as of" })
6565
.setTimestamp();
6666

67-
interaction.editReply({ embeds: [userEmbed] });
67+
const dataButton = new ButtonBuilder()
68+
.setCustomId("download-data")
69+
.setLabel("Download User Data")
70+
.setStyle("Secondary");
71+
72+
interaction.editReply({ embeds: [userEmbed], components: [new ActionRowBuilder().addComponents(dataButton)] });
6873

6974
return;
7075
}

structures/handlers/interactions.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,20 @@ class InteractionHandler {
4242

4343
});
4444

45-
/*readdir("./bot/interactions/components/", (err, categories) => {
45+
readdir(`./bot/interactions/components/`, (err, files) => {
4646

47-
categories.forEach(category => {
48-
49-
readdir(`./bot/interactions/components/${category}`, (err, files) => {
50-
51-
const cmdFiles = files.filter(f => f.split(".").pop() === "js");
52-
53-
console.log(`INFO | Loading ${cmdFiles.length} ${category} components...`);
54-
55-
for (const f in cmdFiles) {
56-
const cmd = new (require(`../../bot/interactions/components/${category}/${cmdFiles[f]}`))(this.HackRUBot);
57-
this.HackRUBot.components.set(cmd.config.name, cmd);
58-
}
47+
const cmdFiles = files.filter(f => f.split(".").pop() === "js");
5948

60-
console.log(`INFO | Loaded ${cmdFiles.length} ${category} components`);
49+
console.log(`INFO | Loading ${cmdFiles.length} components...`);
6150

62-
});
51+
for (const f in cmdFiles) {
52+
const cmd = new (require(`../../bot/interactions/components/${cmdFiles[f]}`))(this.HackRUBot);
53+
this.HackRUBot.components.set(cmd.config.name, cmd);
54+
}
6355

64-
});
56+
console.log(`INFO | Loaded ${cmdFiles.length} components`);
6557

66-
});*/
58+
});
6759

6860
}
6961
}

0 commit comments

Comments
 (0)