-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (41 loc) · 2.2 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
const { Client, Intents, Collection, Interaction } = require("discord.js")
const botConfig = require("./botConfig.json")
const fs = require("fs");
const discord = require("discord.js")
require("dotenv").config();
const prefix = botConfig.prefix
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
client.commands = new Collection();
client.aliases = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.help.name, command);
command.help.aliases.forEach(alias => {
client.aliases.set(alias, command.help.name);
})
console.log(`${command.help.name}.js has loaded.`);
}
client.once("ready", async () => {
console.log(`${client.user.username} is online.`);
client.user.setActivity(`Discord.JS bot example!`, { type: "PLAYING" }); //The Discord "status" of the bot.
});
client.on("messageCreate", async message => { //Runs the handler when a message is sent.
if (message.author.bot) return; //if the message is a bot, it stops the code.
if(message.mentions.users.first()) {
if(message.mentions.users.first().id === 'BOT_ID_HERE') return message.channel.send(`The server prefix is: ${prefix}`) // Sends a msg saying what the prefix is, when the bot is mentioned.
}
var messageArray = message.content.split(" ");
var command = messageArray[0];
if (!message.content.startsWith(prefix)) return;
const commandData = client.commands.get(command.slice(prefix.length)) || client.commands.get(client.aliases.get(command.slice(prefix.length)));
if (!commandData) return;
var arguments = messageArray.slice(1);
try {
await commandData.run(client, message, arguments);
} catch (error) {
console.log(error);
await message.reply("There was a problem handling the command."); //If an error occurs in the handler code, it catches the error and sends a msg.
}
});
client.login(botConfig.token)