-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
142 lines (115 loc) · 4.51 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}
// File system Req
const fs = require('fs');
// Initalise Discord Shit
const Discord = require('discord.js'),
client = new Discord.Client();
// Initalise own shit
const Config = require('./Modules/Config.js');
const Utils = require('./Modules/Utils.js');
const Log = require('./Modules/Logging.js');
const Cmds = require('./Modules/Commands.js');
const Aliases = require('./Modules/Aliases.js');
const Prefixes = require('./Modules/Prefixes.js');
// Setup commands
Log.console("Setting up Commands", "Setup ");
Cmds.setUp();
// Setup aliases
Log.console("Setting up Aliases", "Setup ");
Aliases.setUp();
// Setup Prefixes
Log.console("Setting up Prefixes", "Setup ");
Prefixes.setUp();
// Set Discord Bot Token
const Discord_Token = process.env.DISCORD_TOKEN;
// Array of valid prefixes
const prefix = Config.prefixes;
// Console log when we connect
client.on('ready', () => {
Log.full('Connection', `Logged in as ${client.user.tag}!`);
// Guilds
guildsLogFileString = "";
client.guilds.forEach(guild => {
guildsLogFileString += `Name: ${guild.name}\n\tOwner: ${guild.owner.user.tag}\n`;
});
Log.console(`Connected to ${client.guilds.array().length} server/s`,'Servers');
Log.file(`\n${guildsLogFileString}`,'Servers');
});
client.on('message', msg => {
// Don't respond to it self.
if (msg.author == client.user)
return;
// Chat logging
if (Config.chatLogging)
{
var guild = msg.guild;
var guildName = "undefined";
if (guild.available)
guildName = guild.name;
Log.file(`#${msg.channel.name} | ${msg.content}`, `../guilds/${guildName}/logs/chat`);
}
// Are we ignoring this channel
if(Config.ignoredchannels.hasOwnProperty(msg.channel.id) && msg.content.split(' ')[0] !== '!ignorechannel' && prefix.indexOf(msg.content[0]) !== -1) {
Log.console(`Ignore command "${msg.content}" due to ignore channel ${msg.channel.id}`, 'Commands');
return false;
}
// Check if the message is a command
const guildname = msg.guild.name.toLowerCase();
if (prefix.indexOf(msg.content[0]) != -1 || (typeof Prefixes.prefixes[guildname] !== 'undefined' && Prefixes.prefixes[guildname].indexOf(msg.content[0]) != -1))
{
Log.full(`../guilds/${guildname}/logs/commands`, `@${guildname} | #${msg.channel.name} | ${msg.author.tag} | ${msg.content}`);
// If command exists
var cmdIndex = Cmds.commands.indexOf(msg.content.split(" ")[0].slice(1).toLowerCase());
if (cmdIndex != -1)
{
if((rate = Cmds.isRateLimited(cmdIndex, msg.author)) !== false) {
msg.reply(Cmds.getRateLimitResponse(rate));
return;
}
Cmds.addRateLimit(cmdIndex, msg.author);
Cmds.commandsModule[cmdIndex].run(msg, client);
return;
}
// Alias Code
// Guild does not have a alias object
if (!Aliases.aliases.hasOwnProperty(msg.guild.name.toLowerCase()))
return;
// Is it an Server alias
var cmd = msg.content.split(" ")[0].slice(1).toLowerCase();
if (Aliases.aliases[msg.guild.name.toLowerCase()].hasOwnProperty(cmd))
{
var actualCmd = Aliases.aliases[msg.guild.name.toLowerCase()][cmd];
cmdIndex = Cmds.commands.indexOf(actualCmd);
if (cmdIndex != -1)
{
if((rate = Cmds.isRateLimited(cmdIndex, msg.author)) !== false) {
msg.reply(Cmds.getRateLimitResponse(rate));
return;
}
Cmds.addRateLimit(cmdIndex, msg.author);
Cmds.commandsModule[cmdIndex].run(msg, client);
return;
}
}
// Is it a bot alises
if(Config.aliases.hasOwnProperty(cmd)){
var actualCmdd = Config.aliases[cmd];
cmdIndex = Cmds.commands.indexOf(actualCmdd);
if (cmdIndex != -1)
{
if((rate = Cmds.isRateLimited(cmdIndex, msg.author)) !== false) {
msg.reply(Cmds.getRateLimitResponse(rate));
return;
}
Cmds.addRateLimit(cmdIndex, msg.author);
Cmds.commandsModule[cmdIndex].run(msg, client);
return;
}
}
}
});
// Log in
client.login(Discord_Token);
process.on('unhandledRejection', (err) => console.log('Promise was rejected but there was no error handler: ' + err));