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