-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
80 lines (61 loc) · 2.32 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
72
73
74
75
76
77
78
79
80
require('dotenv/config');
const fs = require('fs');
const Discord = require('discord.js');
const conversation = require('./util/conversation');
const { isAdmin } = require('./util/permissions');
const { prefix } = require('./cfg.json');
const { ianModule } = require('./modules/ian');
/**
* @type {Discord.Client & { commands: Discord.Collection }}
*/
const client = Object.assign(new Discord.Client({ ws: { intents: [ 'GUILD_MEMBERS', Discord.Intents.NON_PRIVILEGED ] } }), {
commands: new Discord.Collection()
});
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js') && !file.endsWith('.map.js'));
for (const file of commandFiles) {
if (!file.endsWith('.js')) {
continue;
}
const command = require(`./commands/${file}`);
if (!command || !command.execute || typeof command.execute !== 'function') {
continue;
}
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Online.');
});
/**
* @param {typeof client} self
* @param {Discord.Message} msg
* @returns {Promise<*>}
*/
const handleMessage = async (self, msg) => {
if (msg.content.startsWith(prefix)) {
const argv = msg.content.slice(prefix.length).trim().split(' ');
const commandName = argv.shift().toLowerCase();
if (!commandName) {
return conversation.failure(msg, 'Please provide a command!', false);
}
if (!client.commands.has(commandName)) {
return conversation.failure(msg, `I don't know how to do that! (Unrecognized command)`, false);
}
const command = await client.commands.get(commandName);
if (command.isAdmin && !isAdmin(msg.member)) {
return conversation.failure(msg, 'Sorry, but you do not have permission to run this command.');
}
try {
await command.execute(self, msg, argv);
} catch (err) {
console.error(`Error while handling command '${commandName}'`, err);
return conversation.failure(msg, `Unable to execute command ):`);
}
}
}
client.on('message', msg => {
handleMessage(client, msg)
.catch(err => console.error('Error while handling message:', err))
});
ianModule(client);
client.login(process.env.token)
.catch(err => console.error('Could not log into discord:', err));