-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.js
99 lines (84 loc) · 2.82 KB
/
discord.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
/* eslint-disable no-console */
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const { handleCommands, handleSimpleMessage } = require('./functions');
const jahirUserId = (process.env.JAHIR_USER_ID || '').toString();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.MessageContent,
],
});
const deletedMessages = new WeakSet();
const isMessageDeleted = (message) => {
return deletedMessages.has(message);
};
const markMessageAsDeleted = (message) => {
deletedMessages.add(message);
};
// Quick validation of whether a message was sent by Jahir or not
const jahirSentMessage = (actualAuthor, author) => {
return (
actualAuthor.toString() === jahirUserId ||
(author ? author.username || '' : '') === 'jahirfiquitiva'
);
};
client.once('ready', () => {
console.log('Discord bot is ready!!');
});
client.on('messageCreate', async (message) => {
const { cleanContent: text = '', author = {}, authorId, type, reference } = message;
let referenceToJahir = false;
if (reference != null) {
const { messageId } = reference;
const referencedMessage = await message.channel.messages.fetch(messageId).catch(console.error);
if (referencedMessage) {
const { authorId } = referencedMessage;
referenceToJahir = authorId === jahirUserId;
}
}
if (type === 'PINS_ADD') {
message.delete().catch(console.error);
return;
}
const actualAuthor = author.id || authorId || '';
// Do not reply to other bots, if the message was deleted
if (
isMessageDeleted(message) ||
author.bot ||
actualAuthor.toString() === (process.env.BOT_USER_ID || '').toString()
) {
return;
}
// Quick check if Jahir is mentioned, and if so, tell them to not do it
const mentionedJahir = message.mentions.users.some((user) => {
return user.username === 'jahirfiquitiva';
});
if (mentionedJahir) {
if (!jahirSentMessage(actualAuthor, author) && !referenceToJahir) {
message.reply(
`Please don't mention Jahir; he checks this server regularly and will get back to you as soon as he can.`
);
}
}
// If message is a command handle it, otherwise check if message includes some keywords
// and reply accordingly
if (text.startsWith(process.env.BOT_COMMAND_KEY)) {
await handleCommands(message).catch(console.error);
} else {
if (jahirSentMessage(actualAuthor, authorId)) return;
await handleSimpleMessage(message).catch(console.error);
}
});
client.on('messageDelete', async (message) => {
markMessageAsDeleted(message);
});
// Initialize discord client
try {
client.login(process.env.BOT_TOKEN || '').catch(() => {
console.error('Error connecting to Discord');
});
} catch (e) {}
module.exports = client;