File tree Expand file tree Collapse file tree 7 files changed +92
-7
lines changed Expand file tree Collapse file tree 7 files changed +92
-7
lines changed Original file line number Diff line number Diff line change 11{
22 "name" : " discord.js-advanced-command-handler" ,
3- "version" : " 1.4 .0" ,
3+ "version" : " 2.0 .0" ,
44 "description" : " 🤖 Discord.js V14 Command and Event Handler" ,
55 "main" : " src/index.js" ,
66 "type" : " module" ,
Original file line number Diff line number Diff line change 1+ export default {
2+ event : 'interactionCreate' ,
3+ async execute ( client , interaction ) {
4+ try {
5+ if ( ! interaction . isCommand ( ) ) return ;
6+ const command = client . slashCommands . get ( interaction . commandName ) ;
7+ if ( ! command ) return ;
8+ await command . execute ( client , interaction ) ;
9+ } catch ( error ) {
10+ return console . log ( error ) ;
11+ }
12+ }
13+ } ;
Original file line number Diff line number Diff line change 11import chalk from 'chalk' ;
2- import { readFileSync } from 'fs' ;
3- let version = JSON . parse ( readFileSync ( './package.json' , 'utf8' ) ) . version ;
2+ import config from '../data/config.js' ;
43
54export default {
65 event : 'ready' ,
76 async execute ( client ) {
87 try {
9- console . log ( chalk . yellow ( `[LOGIN] logged in as ${ client . user . tag } -> Version ${ version } ` ) ) ;
8+ console . log ( chalk . yellow ( `[LOGIN] logged in as ${ client . user . tag } -> Version ${ config . version } ` ) ) ;
109 } catch ( error ) {
1110 return console . log ( error ) ;
1211 }
Original file line number Diff line number Diff line change 1+ import { REST , Routes } from 'discord.js' ;
2+ import { readdirSync } from 'fs' ;
3+ import chalk from 'chalk' ;
4+ import config from '../data/config.js' ;
5+
6+ const rest = new REST ( { version : '10' } ) . setToken ( config . bot . token ) ;
7+
8+ async function loadSlashCommands ( client ) {
9+ const slashCommands = readdirSync ( './src/slashCommands' ) . filter ( file => file . endsWith ( '.js' ) ) ;
10+ for ( let i = 0 ; i < slashCommands . length ; i ++ ) {
11+ const command = await import ( `../slashCommands/${ slashCommands [ i ] } ` ) ;
12+ client . slashCommands . set ( command . default . data . toJSON ( ) . name , command . default ) ;
13+ console . log ( chalk . greenBright ( `[SLASHCOMMAND] Loaded ${ chalk . yellow ( slashCommands [ i ] ) } with command ${ chalk . yellow ( command . default . data . toJSON ( ) . name ) } ` ) ) ;
14+ rest . put (
15+ Routes . applicationCommands ( client . user . id ) ,
16+ { body : client . slashCommands . map ( cmd => cmd . data . toJSON ( ) ) } ,
17+ ) ;
18+ }
19+ }
20+
21+ export default { loadSlashCommands } ;
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import mysql from 'mysql2';
66
77import commandHandler from './handlers/commands.js' ;
88import eventHandler from './handlers/events.js' ;
9+ import slashCommandHandler from './handlers/slashCommands.js' ;
910
1011console . clear ( ) ;
1112
@@ -39,15 +40,16 @@ const client = new Discord.Client({
3940 repliedUser : true
4041 }
4142} ) ;
43+ await client . login ( `${ config . bot . token } ` ) ;
4244
4345client . commands = new Discord . Collection ( ) ;
4446client . aliases = new Discord . Collection ( ) ;
47+ client . slashCommands = new Discord . Collection ( ) ;
4548
4649await commandHandler . loadCommands ( client ) ;
4750await eventHandler . loadEvents ( client ) ;
51+ await slashCommandHandler . loadSlashCommands ( client ) ;
4852
4953process . on ( 'uncaughtException' , function ( err ) {
5054 console . error ( err ) ;
51- } ) ;
52-
53- client . login ( `${ config . bot . token } ` ) ;
55+ } ) ;
Original file line number Diff line number Diff line change 1+ import { inspect } from 'util' ;
2+ import { SlashCommandBuilder } from '@discordjs/builders' ;
3+ import config from '../data/config.js' ;
4+
5+ export default {
6+ data : new SlashCommandBuilder ( )
7+ . addStringOption ( option => option . setName ( 'input' ) . setDescription ( 'The input to echo back' ) )
8+ . setName ( 'eval' )
9+ . setDescription ( 'Evaluate code' ) ,
10+ async execute ( client , interaction ) {
11+ try {
12+ if ( interaction . user . id !== config . bot . eval ) return interaction . reply ( 'You do not have permission to use this command.' ) ;
13+ try {
14+ const evaled = eval ( interaction . options . getString ( 'input' ) ) ;
15+ const cleaned = await clean ( evaled ) ;
16+
17+ interaction . reply ( `\`\`\`js\n${ cleaned } \n\`\`\`` ) ;
18+ } catch ( error ) {
19+ interaction . reply ( `\`ERROR\` \`\`\`xl\n${ error } \n\`\`\`` ) ;
20+ }
21+
22+ } catch ( error ) {
23+ console . log ( error ) ;
24+ }
25+ }
26+ } ;
27+
28+ const clean = async ( text ) => {
29+
30+ if ( text && text . constructor . name == 'Promise' )
31+ text = await text ;
32+
33+ if ( typeof text !== 'string' )
34+ text = inspect ( text , { depth : 1 } ) ;
35+
36+ text = text
37+ . replace ( / ` / g, '`' + String . fromCharCode ( 8203 ) )
38+ . replace ( / @ / g, '@' + String . fromCharCode ( 8203 ) ) ;
39+ return text ;
40+ } ;
Original file line number Diff line number Diff line change 1+ import { SlashCommandBuilder } from '@discordjs/builders' ;
2+
3+ export default {
4+ data : new SlashCommandBuilder ( )
5+ . setName ( 'ping' )
6+ . setDescription ( 'Ping!' ) ,
7+ async execute ( client , interaction ) {
8+ interaction . reply ( 'Pong!' ) ;
9+ }
10+ } ;
You can’t perform that action at this time.
0 commit comments