-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (66 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// This file contains the bot connected with Discord
'use strict';
require('dotenv').config();
const fs = require('fs');
const { Client, Collection } = require('discord.js');
const logger = require('./Utils/logs');
const client = new Client({ intents: [3276799] });
const prefix = `!`; // Every command must start with it
// Create a collection to store the commands
client.commands = new Collection();
// Read all the .js files in the commands directory
const commandFiles = fs.readdirSync(`${__dirname}/commands`).filter(file => file.endsWith('.js'));
// Import all the commands and add them to the collection
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
/**
* Verify if the bot is online, if yes, it will set its activity as playing Amongus and will inform it in console
*/
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity('Amongus');
logger.info('Bot ready, login correct');
});
/**
* Reads the messages, and when the user sends 'hola', it answers with a message
*/
client.on("messageCreate", async message => {
if (message.content == "hola") {
message.channel.send("Hi :3");
}
});
// Add the "help" command to the collection
client.commands.set("help", {
name: "help",
description: "Show a list of the commands available",
execute(message, args) {
const commandList = client.commands.map(command => {
return `${prefix}${command.name} - ${command.description}`;
}).join('\n');
message.channel.send(`Available commands:\n${commandList}`);
}
});
/**
* When the bot gets a message, it gets the command, verify ha it exists and execute it
*/
client.on("messageCreate", (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName);
if (!command) {
message.channel.send("I don't recognize tha command, you can try !help to get a list of the available commands 😊");
logger.info(`Incorrect command: ${message}`);
return;
}
try {
command.execute(message, args);
logger.info(`Command executed: ${message}`);
} catch(e) {
logger.error(`Executing command: ${message}. ${e}`);
message.reply('Sorry, an unexpected error has happened 😞');
}
});
client.login(process.env.TOKEN);