-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
129 lines (102 loc) · 3.17 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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
require('dotenv').config();
const {sosiskaCheck} = require('./lib/sosiska');
const PORT = process.env.PORT || 5000;
const isDevelopment = process.env.DEVELOPMENT;
const devRoleName = 'инженер ЭВМ';
const KARAS_ID = 356283233207320577;
// establish discordjs client
const Discord = require('discord.js');
const bot = new Discord.Client();
// establish database connection
const {establishDatabaseConnection} = require('./database');
establishDatabaseConnection();
// configure express app
app.use(bodyParser.json({limit: '70mb'}));
app.use(bodyParser.urlencoded({limit: '70mb', extended: true}));
// get api required libs
const messages = require('./lib/messages');
const {getPersonalFiles} = require('./lib/personalFile');
// declare expressjs routes
app.get('/', function (req, res) {
res.send('avsimach discord bot');
});
app.post('/miska', function (req, res) {
res.send(`POST request to the homepage\nBody: ${JSON.stringify(req.body)}`);
});
// listen to discordjs events
const HOME_ID = '444034088429551619';
const RETRY_ROLE_LIMIT = 5;
let retries = 0;
bot.login(process.env.BOT_TOKEN);
bot.on('error', () => {
console.log(`error`);
});
bot.on('ready', () => {
console.log(`ready`);
});
bot.on('debug', (info) => {
console.log(`debug, ${info}`);
});
bot.on('message', message => {
const author = message.author;
const {username} = author;
if (isDevelopment && !message.member.roles.find("name", devRoleName)) {
return;
}
Object.keys(messages).forEach(key => {
const word = messages[key];
if (word.pattern.test(message.content)) {
word.reply(message, Discord, bot);
}
});
});
bot.on('messageReactionAdd', (MessageReaction) => {
sosiskaCheck(MessageReaction, Discord, bot)
});
bot.on('guildMemberAdd', (member) => {
const memberId = member.id;
const {username} = member.user;
const guild = member.guild;
const channel = guild.channels.get(HOME_ID);
const suetaRole = guild.roles.find('name', 'суетливый').id;
if (KARAS_ID == memberId) {
channel.send('🐟 тревога');
return member.ban('🐟')
.then(() => console.log(`параноидальный стейт погубил ${member.displayName}`))
.catch(console.error);
}
getPersonalFiles()
.then((files = {}) => {
const existingUser = Object.values(JSON.parse(files))
.find(({id}) => id == memberId);
console.log('got personal files', {
memberId,
username,
channel,
suetaRole
});
if (existingUser) {
addRoles(member, [...existingUser.roles, suetaRole], username, channel);
return;
}
member.addRole(guild.roles.find('name', 'observer'));
})
.catch(error => console.log(error));
});
function addRoles(member, roles, username, channel) {
member.addRoles(roles)
.then(() => {
channel.send(`**${username}** получает назад все свои погоны плюс суетливого за лив`);
})
.catch((error) => {
console.log('addRoles error >', error);
if (retries < RETRY_ROLE_LIMIT) {
addRoles(...arguments);
retries++;
}
});
}
app.listen(PORT, () => console.log(`Listening on ${PORT}`));