-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·225 lines (185 loc) · 7.51 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const fs = require('fs');
const Discord = require('discord.js');
const config = require('config');
const client = new Discord.Client();
const renderHelpText = require('./help');
// NOTE: When adding to a server, bot needs permission code 268512256, which is:
// - Manage roles
// - View channels
// - Send messages
// - Manage messages
// - Read message history
// https://discordapp.com/oauth2/authorize?client_id=<YOUR_ID_HERE>&scope=bot&permissions=268512256
const roleDefinitions = JSON.parse(fs.readFileSync('config/roles.json'));
const roleNames = Object.keys(roleDefinitions);
const discordRoleNames = Object.values(roleDefinitions).map(role => role.name);
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
if (client.guilds.size === 1) {
const server = client.guilds.first();
if (server.id === config.get('homeServer')) {
client.homeServer = server;
console.log(`Home server set as ${server.name}`);
const nickname = client.homeServer.me.nickname || client.user.username;
client.user.setPresence({game: {name: `@${nickname} help`}});
return;
}
}
throw 'Should only be connected to the home server!';
});
client.on('message', async message => {
// Delete messages in welcome channel after a given time (if by non-mod/admin or bot)
if (message.channel.id === config.get('welcomeChannel')) {
if (config.get('deleteMessages') && !(message.member.hasPermission(Discord.Permissions.FLAGS.ADMINISTRATOR) || message.member.roles.find(role => role.name === 'Mods')) || message.author.bot) {
message.delete(config.get('messageDeleteDelay'));
}
}
if (message.author === message.client.user || message.author.bot) {
return;
}
// If bot is mentioned in message, log it
if (message.channel.type === 'dm' || message.mentions.users.get(message.client.user.id) || message.channel.id === config.get('welcomeChannel')) {
logIncomingMessage(message);
}
if (message.channel.type === 'dm' || message.content.startsWith(`<@${message.client.user.id}> `) || message.channel.id === config.get('welcomeChannel')) {
const args = message.content.toLowerCase().slice().split(/\s+/);
const foundRoles = findRoles(args);
if (args.includes('help')) {
sendHelpMessage(message);
} else if (foundRoles.length == 1) {
const result = await setRole(message, foundRoles[0]);
if (result) {
sendReply(message, roleDefinitions[foundRoles[0]].successText);
} else {
sendReply(message, config.roleFailText);
}
} else if (message.channel.type === 'dm' || message.content.startsWith(`<@${message.client.user.id}> `)) {
sendReply(message, config['dontUnderstandText']);
} else if (message.channel.id === config.get('welcomeChannel')) {
if (!(message.member.hasPermission(Discord.Permissions.FLAGS.ADMINISTRATOR) || message.member.roles.find(role => role.name === config.get('modsRoleName')))) {
sendReply(message, config['dontUnderstandText']);
}
}
}
});
function findRoles(args) {
return roleNames.filter(role => args.includes(role));
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
client.on('guildMemberAdd', async member => {
if (!member.user.bot) {
// When new user joins, wait a few minutes, and if they haven't managed to get a role yet, DM them
console.log(`New member joined: ${member.user.tag}`);
await delay(config.get('welcomeDmDelay'));
if (!member.roles.find(role => discordRoleNames.includes(role.name))) {
sendHelpMessage(member);
}
}
});
async function setRole(message, roleName) {
let member, roleToAdd, rolesToDelete;
roleToAdd = roleDefinitions[roleName].name;
rolesToDelete = discordRoleNames.filter((role) => role !== roleToAdd);
if (message.member) { // Non-DM channel
member = message.member;
roleToAdd = message.member.guild.roles.find((role) => role.name === roleToAdd);
rolesToDelete = message.member.guild.roles.filter((role) => rolesToDelete.includes(role.name));
} else { // DM
// See if we already have a reference to the member
const user = message.author;
member = message.client.homeServer.member(user);
// If not, fetch from server
if (!member) {
console.warn(`Unable to get member for user ${user} (${user.tag}). Fetching from API...`);
try {
member = await message.client.homeServer.fetchMember(user);
} catch (error) {
console.warn(`Unable to fetch member for user ${user} (${user.tag}) from API`);
member = null;
// Send message to bot owner to notify of failure
if (config.get('notifyFailures')) {
const owner = await message.client.fetchUser(config.get('owner'));
owner.send(`Error: Unable to fetch member for user ${user} from API`);
}
}
}
// Get roles
roleToAdd = message.client.homeServer.roles.find((role) => role.name === roleToAdd);
rolesToDelete = message.client.homeServer.roles.filter((role) => rolesToDelete.includes(role.name));
}
if (member) {
await member.addRole(roleToAdd);
rolesToDelete.forEach(async role => await member.removeRole(role));
return true;
}
}
function logIncomingMessage(message) {
if (config.get('debug')) {
const destination = message.channel.type === 'dm' ? '' : `#${message.channel.name} `;
console.log(`${destination}<- ${message.author.tag}: ${message.content}`);
}
}
async function sendChannelMessage(channel, user, content) {
const sentMessage = await channel.send(content);
if (config.get('debug')) {
const destination = user ? `${user.tag} (#${channel.name})` : `(#${channel.name})`;
const contentToLog = truncateMessage(sentMessage.content);
console.log(`-> ${destination}: ${contentToLog}`);
}
return sentMessage;
}
async function sendDm(user, content) {
try {
const sentMessage = await user.send(content);
if (config.get('debug')) {
const contentToLog = truncateMessage(sentMessage.content);
console.log(`-> ${user.tag}: ${contentToLog}`);
}
return sentMessage;
} catch (error) {
console.log(`Failed to DM ${user.tag}: ${error}`);
}
}
async function sendReply(message, content) {
const sentMessage = await message.reply(content);
if (config.get('debug')) {
const destination = message.channel.type === 'dm' ? message.channel.recipient.tag : `${message.author.tag} (#${message.channel.name})`;
const contentToLog = truncateMessage(sentMessage.content);
console.log(`-> ${destination}: ${contentToLog}`);
}
return sentMessage;
}
function truncateMessage(content) {
if (content.length > 90) {
return content.substring(0, 90) + '...';
} else {
return content;
}
}
async function sendHelpMessage(messageOrMember) {
const welcomeChannel = client.channels.get(config.get('welcomeChannel'));
const mainChannel = client.channels.get(config.get('mainChannel'));
let message, user;
if (messageOrMember.channel && messageOrMember.channel.type === 'dm') {
user = messageOrMember.author;
} else if (messageOrMember.author) {
message = messageOrMember;
user = messageOrMember.author;
} else {
user = messageOrMember.user;
}
const helpText = renderHelpText(user, welcomeChannel, mainChannel);
if (message) {
sendChannelMessage(message.channel, user, helpText);
} else {
sendDm(user, helpText);
}
}
client.login(config.get('discordToken'));
// For heroku compatibility
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('O hai');
}).listen(process.env.PORT);