Skip to content

Commit bd5d1eb

Browse files
committed
refactor the code a bit
1 parent 373de56 commit bd5d1eb

File tree

13 files changed

+91
-54
lines changed

13 files changed

+91
-54
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
"dependencies": {
2424
"chalk": "^5.1.2",
2525
"discord.js": "^14.6.0",
26-
"fs": "^0.0.1-security",
27-
"mysql2": "^2.3.3",
28-
"util": "^0.12.5"
26+
"mysql2": "^2.3.3"
2927
}
3028
}

src/commands/helloWorld.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ export default {
44
aliases: ['hey', 'hi'],
55

66
async execute(message) {
7-
try {
8-
message.reply('Hello World!');
9-
} catch (error) {
10-
console.log(error);
11-
}
7+
message.reply('Hello World!');
128
}
139
};

src/commands/reload.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import commandHandler from '../handlers/commands.js';
2+
import slashCommandHandler from '../handlers/slashCommands.js';
23
import config from '../data/config.js';
34

45
export default {
@@ -7,12 +8,9 @@ export default {
78
aliases: ['rl'],
89

910
async execute(message, args, client) {
10-
try {
11-
if (!message.member.id == `${config.bot.eval}`) return message.reply('You do not have the permission to use this command!');
12-
let cmd = await commandHandler.reloadCommands(client);
13-
message.reply(`${cmd} commands have been reloaded!`);
14-
} catch (error) {
15-
console.log(error);
16-
}
11+
if (!message.member.id == config.bot.owner) return message.reply('You do not have the permission to use this command!');
12+
let cmd = await commandHandler.reloadCommands(client);
13+
let cmds = await slashCommandHandler.reloadSlashCommands(client);
14+
message.reply(`Reloaded ${cmd} commands and ${cmds} slash commands!`);
1715
}
1816
};

src/commands/setprefix.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,19 @@ export default {
1919
aliases: [],
2020

2121
async execute(message, args) {
22-
try {
23-
if (!message.member.permissions.has('ADMINISTRATOR')) return message.reply('You do not have the permission to use this command!');
22+
if (!message.member.permissions.has('ADMINISTRATOR')) return message.reply('You do not have the permission to use this command!');
2423

25-
let rows = await dbquery(`SELECT * FROM guilds WHERE guildid = '${message.guild.id}'`);
26-
27-
if (!args[1]) return message.reply(`Use ${rows[0].prefix}setprefix <prefix>`);
28-
29-
await dbquery(`UPDATE guilds SET prefix = '${args[1]}' WHERE guildid = '${message.guild.id}'`);
30-
31-
const embed = new Discord.EmbedBuilder()
32-
.setTitle('Prefix changed!')
33-
.setDescription(`The prefix has been changed to ${args[1]}`)
34-
.setColor('149C51');
35-
36-
message.reply({embeds: [embed]});
37-
} catch (error) {
38-
console.log(error);
39-
}
24+
let rows = await dbquery(`SELECT * FROM guilds WHERE guildid = '${message.guild.id}'`);
25+
26+
if (!args[1]) return message.reply(`Use ${rows[0].prefix}setprefix <prefix>`);
27+
28+
await dbquery(`UPDATE guilds SET prefix = '${args[1]}' WHERE guildid = '${message.guild.id}'`);
29+
30+
const embed = new Discord.EmbedBuilder()
31+
.setTitle('Prefix changed!')
32+
.setDescription(`The prefix has been changed to ${args[1]}`)
33+
.setColor('149C51');
34+
35+
message.reply({embeds: [embed]});
4036
}
4137
};

src/data/config.js.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ const config = {
22
bot: {
33
token: 'TOKEN',
44
intents: 'INTENT',
5-
eval: '0',
5+
owner: '0',
66
},
77
mysql: {
88
host: 'HOST',
99
port: 'PORT',
1010
user: 'USER',
1111
password: 'PASSWORD',
12-
database: 'DATABASE'
12+
database: 'DATABASE',
1313
},
1414
};
1515

src/events/interactionCreate.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ export default {
55
if (!interaction.isCommand()) return;
66
const command = client.slashCommands.get(interaction.commandName);
77
if (!command) return;
8-
await command.execute(client, interaction);
8+
try {
9+
command.execute(client, interaction);
10+
} catch (error) {
11+
interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
12+
console.log(error);
13+
}
914
} catch (error) {
1015
return console.log(error);
1116
}

src/events/messageCreate.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ export default {
1818
try {
1919
if (message.author.bot) return;
2020
let prefix = await getPrefix(message.guild.id);
21-
21+
2222
if (message.content.startsWith(prefix)) {
2323
const args = message.content.substring(prefix.length).split(/ +/);
2424
const command = client.commands.find(cmd => cmd.name == args[0] || cmd.aliases.includes(args[0]));
25-
if (!command) return; //message.reply(`${args[0]} is not a valid command!`); //uncomment if you want that the bot replies when the command is not a valid command!
26-
command.execute(message, args, client);
25+
if (!command) return;
26+
try {
27+
command.execute(message, args, client);
28+
} catch (error) {
29+
message.reply('There was an error trying to execute that command!');
30+
console.log(error);
31+
}
2732
} else {
28-
// Here you can add commands that are not have a prefix.
29-
// like when somebody pings the bot.
33+
if (message.mentions.users.first() == client.user) message.channel.send('Hello!');
3034
}
3135
} catch (error) {
3236
console.log(error);

src/events/ready.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import chalk from 'chalk';
2-
import config from '../data/config.js';
2+
import { readFileSync } from 'fs';
3+
let version = JSON.parse(readFileSync('./package.json', 'utf8')).version;
34

45
export default {
56
event: 'ready',
67
async execute(client) {
78
try {
8-
console.log(chalk.yellow(`[LOGIN] logged in as ${client.user.tag} -> Version ${config.version}`));
9+
console.log(chalk.yellow(`[LOGIN] logged in as ${client.user.tag} -> Version ${version}`));
910
} catch (error) {
1011
return console.log(error);
1112
}

src/handlers/commands.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ async function loadCommands(client) {
55
const commandFiles = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
66
for (let i = 0; i < commandFiles.length; i++) {
77
const cmd = await import(`../commands/${commandFiles[i]}`);
8-
client.commands.set(cmd.default.name, cmd.default);
8+
await client.commands.set(cmd.default.name, cmd.default);
99
console.log(chalk.greenBright(`[COMMAND] Loaded ${(chalk.yellow(commandFiles[i]))} with command ${(chalk.yellow(cmd.default.name))} ${(chalk.yellow(`[${cmd.default.aliases}]`))}`));
1010

1111
if (cmd.default.aliases) {
@@ -17,17 +17,17 @@ async function loadCommands(client) {
1717
}
1818

1919
async function reloadCommands(client) {
20-
await client.commands.clear();
21-
await client.aliases.clear();
20+
client.commands.clear();
21+
client.aliases.clear();
2222
const commandFiles = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
2323
for (let i = 0; i < commandFiles.length; i++) {
2424
const cmd = await import(`../commands/${commandFiles[i]}?${Date.now()}`);
25-
await client.commands.set(cmd.default.name, cmd.default);
25+
client.commands.set(cmd.default.name, cmd.default);
2626
console.log(chalk.greenBright(`[COMMAND] Reloaded ${chalk.yellow(commandFiles[i])} with command ${chalk.yellow(cmd.default.name)} ${chalk.yellow(`[${cmd.default.aliases}]`)}`));
2727

2828
if (cmd.default.aliases) {
2929
cmd.default.aliases.forEach(async (alias) => {
30-
await client.aliases.set(alias, cmd.default);
30+
client.aliases.set(alias, cmd.default);
3131
});
3232
}
3333
}

src/handlers/slashCommands.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const rest = new REST({ version: '10' }).setToken(config.bot.token);
88
async function loadSlashCommands(client) {
99
const slashCommands = readdirSync('./src/slashCommands').filter(file => file.endsWith('.js'));
1010
for (let i = 0; i < slashCommands.length; i++) {
11-
const command = await import(`../slashCommands/${slashCommands[i]}`);
11+
const command = await import(`../slashCommands/${slashCommands[i]}?${Date.now()}`);
1212
client.slashCommands.set(command.default.data.toJSON().name, command.default);
1313
console.log(chalk.greenBright(`[SLASHCOMMAND] Loaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
1414
rest.put(
@@ -18,4 +18,19 @@ async function loadSlashCommands(client) {
1818
}
1919
}
2020

21-
export default { loadSlashCommands };
21+
async function reloadSlashCommands(client) {
22+
client.slashCommands.clear();
23+
const slashCommands = readdirSync('./src/slashCommands').filter(file => file.endsWith('.js'));
24+
for (let i = 0; i < slashCommands.length; i++) {
25+
const command = await import(`../slashCommands/${slashCommands[i]}`);
26+
client.slashCommands.set(command.default.data.toJSON().name, command.default);
27+
console.log(chalk.greenBright(`[SLASHCOMMAND] Reloaded ${chalk.yellow(slashCommands[i])} with command ${chalk.yellow(command.default.data.toJSON().name)}`));
28+
rest.put(
29+
Routes.applicationCommands(client.user.id),
30+
{ body: client.slashCommands.map(cmd => cmd.data.toJSON()) },
31+
);
32+
}
33+
return slashCommands.length;
34+
}
35+
36+
export default { loadSlashCommands, reloadSlashCommands };

0 commit comments

Comments
 (0)