Skip to content

Commit 373de56

Browse files
committed
Added Slashcommands
1 parent 533f0d7 commit 373de56

File tree

7 files changed

+92
-7
lines changed

7 files changed

+92
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",

src/events/interactionCreate.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
};

src/events/ready.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import 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

54
export 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
}

src/handlers/slashCommands.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 };

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import mysql from 'mysql2';
66

77
import commandHandler from './handlers/commands.js';
88
import eventHandler from './handlers/events.js';
9+
import slashCommandHandler from './handlers/slashCommands.js';
910

1011
console.clear();
1112

@@ -39,15 +40,16 @@ const client = new Discord.Client({
3940
repliedUser: true
4041
}
4142
});
43+
await client.login(`${config.bot.token}`);
4244

4345
client.commands = new Discord.Collection();
4446
client.aliases = new Discord.Collection();
47+
client.slashCommands = new Discord.Collection();
4548

4649
await commandHandler.loadCommands(client);
4750
await eventHandler.loadEvents(client);
51+
await slashCommandHandler.loadSlashCommands(client);
4852

4953
process.on('uncaughtException', function (err) {
5054
console.error(err);
51-
});
52-
53-
client.login(`${config.bot.token}`);
55+
});

src/slashCommands/eval.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
};

src/slashCommands/ping.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
};

0 commit comments

Comments
 (0)