Skip to content

Commit ddfe1ec

Browse files
committed
feat: search user command
1 parent 6f4d4e7 commit ddfe1ec

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { EmbedBuilder, ApplicationCommandOptionType } = require("discord.js");
2+
const SlashCommand = require("../../../structures/base/BaseSlashCommand");
3+
4+
class SearchUserCommand extends SlashCommand {
5+
/**
6+
* @param {import("../../index.js")} client HackRUBot's Discord Client.
7+
*/
8+
constructor(client) {
9+
super(client, {
10+
name: "search-user",
11+
category: "team",
12+
guildRequired: true,
13+
cooldown: 3,
14+
commandData: {
15+
description: "Search for users from HackRU's database.",
16+
options: [
17+
{
18+
name: "text",
19+
type: ApplicationCommandOptionType.String,
20+
description: "Enter text to search HackRU's user database.",
21+
required: true,
22+
},
23+
],
24+
},
25+
});
26+
}
27+
28+
/**
29+
* @param {import("discord.js").ChatInputCommandInteraction} interaction
30+
*/
31+
async run(interaction) {
32+
await interaction.deferReply();
33+
34+
const users = await this.HackRUBot.db.getCollection("users");
35+
const search = users.find({ $text: { $search: interaction.options.getString("text") } }, { projection: { first_name: 1, last_name: 1, email: 1 } });
36+
let results = await search.toArray();
37+
38+
if (!results.length) return interaction.editReply({ embeds: [this.HackRUBot.util.errorEmbed("No user found with the specified search.")] });
39+
40+
const infoEmbed = new EmbedBuilder()
41+
.setAuthor({ name: `HackRU Users Search - ${results.length} results found`, iconURL: interaction.guild.iconURL() })
42+
.setColor("Blurple")
43+
.setFooter({ text: "Data as of" })
44+
.setTimestamp();
45+
46+
results = results.slice(0, 25);
47+
48+
for (const res of results)
49+
infoEmbed.addFields({ name: `${res.first_name} ${res.last_name}`, value: res.email, inline: true });
50+
51+
interaction.editReply({ embeds: [infoEmbed] });
52+
53+
return;
54+
}
55+
}
56+
57+
module.exports = SearchUserCommand;

0 commit comments

Comments
 (0)