forked from PolyphasicDevTeam/NapGod.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (122 loc) · 4.08 KB
/
app.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
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
var mongoose = require('mongoose');
// mongoose.connect('mongodb://database:27017/napgod');
mongoose.connect(config.mongo);
const { processCommands } = require('./server/command.ctrl');
const { processHelpCommands } = require('./server/help_command.ctrl');
const { processDMCommands } = require('./server/dm_command.ctrl');
const { processDevCommands } = require('./server/devCommand.ctrl')(client);
const { fixHistLogs } = require('./server/fixHistScheds.js');
console.log('NapGod.js is starting...');
const logsChannelName = 'adaptation_logs';
client.on('message', (message) => {
//Ignore other bots and messages that do not start with prefix
if (message.author.bot) return;
if (message.channel.name === logsChannelName) {
message.author
.send(message.content)
.then(
message.author
.send(
'You need to write the logs through me. Write `+log` in this direct chat to proceed or `+loghelp` for help'
)
.catch(console.warn)
)
.catch((err) =>
console.warn(
`WARN\t: Sending message to ${message.author.username}: ${err}`
)
);
message.delete({
reason:
'All adaptation logs must be made through DMing NapGod. See +loghelp for details',
});
return;
}
const isDirectMessage = message.channel instanceof Discord.DMChannel;
if (
config.modonly != null &&
config.modonly != undefined &&
config.modonly == true &&
!isDirectMessage
) {
//Reject commands from non-mods because we are in Mod-only mode
let roles = message.member.roles;
roles = new Set(roles.keys());
let mods = message.guild.roles.find('name', 'Admins').id;
let admins = message.guild.roles.find('name', 'Admins').id;
permissions = false;
if (roles.has(mods) || roles.has(admins)) {
permissions = true;
}
if (!permissions) {
console.log(
'INFO : ',
'Command was rejected because author was not a mod and we are in mod-only mode.'
);
return;
}
}
const args = getArgs(message);
const command = args.shift().toLowerCase();
if (command == '') {
return;
} //There is probably space after prefix, reject
//if (isDevPrefix(message)) {
//processDevCommands(command, message, args);
//} else
if (isValidPrefix(message)) {
// For now, these are separated b/c a lot of existing commands break in DMs
let handled = false;
if (!isDirectMessage) {
handled = processCommands(command, message, args);
}
if (!handled) {
if (!processDMCommands(command, message, args)) {
console.error('WARN>>: ', 'Command was not handled:', command, args);
}
}
}
if (isValidHelpPrefix(message)) {
processHelpCommands(command, message, args);
}
});
function isValidPrefix(message) {
return message.content.indexOf(config.prefix) === 0;
}
function isValidHelpPrefix(message) {
return message.content.indexOf(config.help_prefix) === 0;
}
function isDevPrefix(message) {
return message.content.indexOf(config.devPrefix) === 0;
}
function getArgs(message) {
return message.content
.slice(config.prefix.length)
.trimRight()
.replace(/\n/g, ' ')
.split(/ +/g);
}
client.on('ready', () => {
// This event will run if the bot starts, and logs in, successfully.
console.log(
`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`
);
client.user.setActivity(config.prefix + 'help');
});
client.on('guildCreate', (guild) => {
console.log(
`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`
);
client.user.setGame(`on ${client.guilds.size} servers`);
});
client.on('guildDelete', (guild) => {
console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
client.user.setGame(`on ${client.guilds.size} servers`);
});
// TODO: remove fixHistLogs (not client.login) after executed once
fixHistLogs().then(
client.login(config.token)
);