@@ -5,124 +5,101 @@ import { commands } from "../components/utils/cmd/loader";
55export const info = {
66 command : "help" ,
77 description : "List available commands and their usage." ,
8- usage : "help [page| role|command]" ,
9- example : "help admin" ,
8+ usage : "help [-- role] [page |command]" ,
9+ example : "help -- admin 2 " ,
1010 role : "user" ,
1111 cooldown : 5000 ,
1212} ;
1313
1414type CommandType = {
1515 command : string ;
1616 role : string ;
17+ description ?: string ;
18+ usage ?: string ;
19+ example ?: string ;
20+ cooldown ?: number ;
1721} ;
1822
1923const PAGE_SIZE = 20 ;
24+ const validRoles = [ "user" , "admin" , "super-admin" , "legal" ] ;
2025
2126function paginate ( items : string [ ] , page : number , pageSize : number ) : string [ ] {
2227 const start = ( page - 1 ) * pageSize ;
2328 return items . slice ( start , start + pageSize ) ;
2429}
2530
26- function buildUserPage (
27- userCommands : string [ ] ,
31+ function buildRoleHelpPage (
32+ commandsForRole : string [ ] ,
2833 page : number ,
2934 totalPages : number ,
35+ role : string ,
3036) : string {
3137 let response = `
32- \`Help ${ page } \`
33- help [command] for more details on a specific command.
38+ \`Help ${ role . charAt ( 0 ) . toUpperCase ( ) + role . slice ( 1 ) } \`
39+ Use: * help [command]* for more details
3440
35- | • ${ userCommands . join ( "\n | • " ) || "_None_" }
41+ | • ${ commandsForRole . join ( "\n| • " ) || "_None_" }
3642
3743 \`Page ${ page } of ${ totalPages } \`
3844 ` ;
3945 return response ;
4046}
4147
42- function buildAdminPage ( type : string , adminCommands : string [ ] ) : string {
43- let response = `
44- \`Help ${ type } \`
45- help [command] for more details on a specific command.
46-
47- | • ${ adminCommands . join ( "\n | • " ) || "_None_" }
48- ` ;
49- return response ;
50- }
51-
5248export default async function ( msg : Message ) : Promise < void > {
53- const match = / ^ h e l p (?: \s + ( a d m i n | s u p e r - a d m i n | \d + ) ) ? $ / i. exec ( msg . body . trim ( ) ) ;
54- if ( ! match ) return ;
55-
56- const query = msg . body
57- . replace ( / ^ h e l p \b \s * / i, "" )
58- . trim ( )
59- . toLowerCase ( ) ;
60-
61- /*
62- * will show help for a specific command
63- */
64- const matchCommands = commands [ query ] ;
65- if ( matchCommands ) {
49+ const query = msg . body . replace ( / ^ h e l p / i, "" ) . trim ( ) ;
50+
51+ // check if the query matches a specific command first
52+ const possibleCommand = query . split ( / \s + / ) [ 0 ] ;
53+ if ( commands [ possibleCommand ] ) {
54+ const cmd = commands [ possibleCommand ] as CommandType ;
6655 const response = `
67- \`${ matchCommands . command } \`
68- ${ matchCommands . description || "No description" }
56+ \`${ cmd . command } \`
57+ ${ cmd . description || "No description" }
6958
70- *Usage:* ${ matchCommands . usage || "No usage " }
71- *Example:* ${ matchCommands . example || "No example " }
72- *Role:* ${ matchCommands . role || "User" }
73- *Cooldown:* ${ matchCommands . cooldown || 5000 } ms
59+ *Usage:* ${ cmd . usage || "N/A " }
60+ *Example:* ${ cmd . example || "N/A " }
61+ *Role:* ${ cmd . role }
62+ *Cooldown:* ${ cmd . cooldown || 5000 } ms
7463 ` ;
7564 await msg . reply ( response ) ;
7665 return ;
7766 }
7867
79- // help admin (group admins)
80- if ( / ^ a d m i n $ / i. test ( query ) ) {
81- const adminCommands = Object . values ( commands )
82- . filter ( ( cmd : CommandType ) => cmd . role === "admin" )
83- . map ( ( cmd : CommandType ) => cmd . command )
84- . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
85- await msg . reply ( buildAdminPage ( "Admin" , adminCommands ) ) ;
86- return ;
87- }
88-
89- // help super-admin (bot owner)
90- if ( / ^ s u p e r - a d m i n $ / i. test ( query ) ) {
91- const superCommands = Object . values ( commands )
92- . filter ( ( cmd : CommandType ) => cmd . role === "super-admin" )
93- . map ( ( cmd : CommandType ) => cmd . command )
94- . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
95- await msg . reply ( buildAdminPage ( "Super Admin" , superCommands ) ) ;
96- return ;
97- }
98-
99- if ( ! / ^ [ 1 - 9 ] \d * $ / . test ( query ) && query != "" ) {
100- await msg . reply ( "Please type a valid page number." ) ;
101- return ;
68+ // extract role and page
69+ const args = query . split ( / \s + / ) . filter ( Boolean ) ;
70+ let role = "user" ;
71+ let page = 1 ;
72+
73+ for ( const arg of args ) {
74+ if ( arg . startsWith ( "--" ) ) {
75+ const roleCandidate = arg . replace ( / ^ - - / , "" ) . toLowerCase ( ) ;
76+ if ( validRoles . includes ( roleCandidate ) ) {
77+ role = roleCandidate ;
78+ }
79+ } else if ( / ^ \d + $ / . test ( arg ) ) {
80+ page = parseInt ( arg , 10 ) ;
81+ }
10282 }
10383
104- // help [page]
105- const matchPage = query . match ( / ( \d + ) ? / i) ;
106- const page = Math . max (
107- 1 ,
108- matchPage && matchPage [ 1 ] ? parseInt ( matchPage [ 1 ] , 10 ) : 1 ,
109- ) ;
110-
111- const userCommands = Object . values ( commands )
112- . filter ( ( cmd : CommandType ) => cmd . role === "user" )
84+ const filteredCommands = Object . values ( commands )
85+ . filter ( ( cmd : CommandType ) => cmd . role === role )
11386 . map ( ( cmd : CommandType ) => cmd . command )
11487 . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
11588
116- userCommands . unshift ( "admin" ) ;
117- userCommands . unshift ( "super-admin" ) ;
118-
119- if ( Object . values ( commands ) . length === 0 ) {
120- await msg . reply ( `The *${ page } * is obviously is not our bot bounds.` ) ;
89+ if ( filteredCommands . length === 0 ) {
90+ await msg . reply ( `No commands found for role: *${ role } *.` ) ;
12191 return ;
12292 }
12393
124- const totalPages = Math . ceil ( userCommands . length / PAGE_SIZE ) ;
125- const userPage = paginate ( userCommands , page , PAGE_SIZE ) ;
94+ const totalPages = Math . ceil ( filteredCommands . length / PAGE_SIZE ) ;
95+ if ( page < 1 || page > totalPages ) {
96+ await msg . reply (
97+ `Page *${ page } * is out of range. Total pages: *${ totalPages } *.` ,
98+ ) ;
99+ return ;
100+ }
126101
127- await msg . reply ( buildUserPage ( userPage , page , totalPages ) ) ;
102+ const paginated = paginate ( filteredCommands , page , PAGE_SIZE ) ;
103+ const response = buildRoleHelpPage ( paginated , page , totalPages , role ) ;
104+ await msg . reply ( response ) ;
128105}
0 commit comments