Skip to content

Commit a4b4963

Browse files
committed
Added event handler
1 parent 649cf85 commit a4b4963

File tree

9 files changed

+67
-26
lines changed

9 files changed

+67
-26
lines changed

.eslintrc.cjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
// eslint-disable-next-line no-undef
12
module.exports = {
23
'env': {
3-
'browser': false,
4-
'es2021': true
4+
'node': true,
5+
'commonjs': true,
56
},
67
'extends': 'eslint:recommended',
78
'overrides': [
9+
{
10+
'files': ['src/**/*'],
11+
}
812
],
913
'parserOptions': {
1014
'ecmaVersion': 'latest',
1115
'sourceType': 'module'
1216
},
1317
'rules': {
14-
'no-console': 'true',
1518
'indent': [
1619
'error',
1720
'tab'

src/commands/helloWorld.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
name: 'hello',
3+
description: 'Hello world.',
4+
aliases: ['hey', 'hi'],
5+
6+
async execute(message) {
7+
try {
8+
message.reply('Hello World!');
9+
} catch (error) {
10+
console.log(error);
11+
}
12+
}
13+
};

src/commands/ping.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/commands/ping2.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/events/messageCreate.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
event: 'messageCreate',
3+
async execute(client, message) {
4+
let prefix = '!'; //await getprefix(message.guild.id); // MySQL WIP
5+
6+
if (message.content.startsWith(prefix)) {
7+
const args = message.content.substring(prefix.length).split(/ +/);
8+
const command = client.commands.find(cmd => cmd.name == args[0] || cmd.aliases.includes(args[0]));
9+
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!
10+
command.execute(message, args, client);
11+
} else {
12+
// Here you can add commands that are not have a prefix.
13+
// like when somebody pings the bot.
14+
}
15+
}
16+
};

src/events/ready.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default {
2+
event: 'ready',
3+
async execute(client) {
4+
try {
5+
console.log(`Logged in as ${client.user.tag}`);
6+
} catch (error) {
7+
return console.log(error);
8+
}
9+
}
10+
};

src/handlers/commands.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import fs from 'fs';
1+
import { readdirSync } from 'fs';
22

33
async function loadCommands(client) {
4-
const commandFiles = fs.readdirSync('./src/commands').filter(file => file.endsWith('.js'));
4+
const commandFiles = readdirSync('./src/commands').filter(file => file.endsWith('.js'));
55
for (let i = 0; i < commandFiles.length; i++) {
6-
const file = commandFiles[i];
7-
const cmd = await import(`../commands/${file}`);
6+
const cmd = await import(`../commands/${commandFiles[i]}`);
87
client.commands.set(cmd.default.name, cmd.default);
98

109
if (cmd.default.aliases) {

src/handlers/events.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
import { readdirSync } from 'fs';
2+
13
async function loadEvents(client) {
2-
4+
const eventFiles = readdirSync('./src/events').filter(file => file.endsWith('.js'));
5+
for (let i = 0; i < eventFiles.length; i++) {
6+
const event = await import(`../events/${eventFiles[i]}`);
7+
client.on(event.default.event, event.default.execute.bind(null, client));
8+
}
39
}
410

5-
export default {}
11+
export default { loadEvents };

src/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ import eventHandler from './handlers/events.js';
66

77
console.clear();
88

9-
const client = new Discord.Client({intents: 3276799});
9+
const client = new Discord.Client({
10+
intents: 3276799,
11+
allowedMentions: {
12+
repliedUser: true
13+
},
14+
});
1015

1116
client.commands = new Discord.Collection();
1217
client.aliases = new Discord.Collection();
1318

1419
await commandHanderler.loadCommands(client);
20+
await eventHandler.loadEvents(client);
21+
22+
process.on('uncaughtException', function (err) {
23+
console.error(err);
24+
});
1525

1626
client.login(`${config.token}`);

0 commit comments

Comments
 (0)