-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
71 lines (58 loc) · 1.93 KB
/
bot.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
require('dotenv').config()
//importing essentials
const fs = require('fs')
const Discord = require('discord.js')
const bot = new Discord.Client()
bot.commands = new Discord.Collection()
//Here we read through all the files in the command folder ending with .js with a fs module.
const commandFiles = fs
.readdirSync('./commands')
.filter((file) => file.endsWith('.js'))
//In this we add files we read through fs module into the collection declared above
for (const file of commandFiles) {
const command = require(`./commands/${file}`)
bot.commands.set(command.name, command)
}
bot.once('ready', () => {
console.log('Ready!')
})
bot.on('message', (message) => {
//checks if the message is intended for bot or not
if (!message.content.startsWith(process.env.PREFIX) || message.author.bot)
return
//seperates the arguments and the command from message
const mess = message
let args = message.content.slice(process.env.PREFIX.length).trim().split(/ +/)
const commandName = args.shift().toLowerCase()
if (commandName == 'job') {
args = mess.content.slice(process.env.PREFIX.length).trim().split('|')
}
//Checks if a particular command exist or not
if (!bot.commands.has(commandName)) return
//from the collection picks out the command with commandName
const command = bot.commands.get(commandName)
//Checks if command require arguments
//if true then it will check if arguments are present or not
if (command.args && !args.length) {
try {
message.channel.send(
`You didn't provide any arguments, ${message.author}!`
)
} catch (error) {
console.log(error)
return
}
}
//execute the command
//logs for error on console by checking them
try {
command.execute(message, args)
} catch (error) {
console.error(error)
message.reply('there was an error trying to execute that command!')
}
})
// (async () => {
// await try
// })();
bot.login(process.env.TOKEN)