-
Notifications
You must be signed in to change notification settings - Fork 0
/
RustDiscordBot-En.js
309 lines (278 loc) · 8.64 KB
/
RustDiscordBot-En.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const WebSocket = require('ws');
var Discord = require('discord.io');
const Sequelize = require('sequelize');
var bodyParser = require("body-parser");
var express = require('express');
var ExpressPort = 3000; //Web Server Port
var DiscordToken = ""; // Bot Discord Token
var serverID = ""; //ID Server discord
var AdminRole = ""; //ID Administrator role
var ModerRole = ""; //ID Moderator role
var VipRole = ""; //ID vip role
var ChatRole = ""; //ID chat role
var ChatChannel = ""; //ID chat channel
var RegisterTable = "ChatRegistred"; //registration tablename in Mysql
var BonusTable = "ChatBonus"; //bonus tablename in Mysql
var RconIP = ''; //rcon ip
var RconPort = ''; //rcon port
var RconPassword = ''; //rcon password
var RoleNotice1 = ""; //ID role administartor for mentions when anyone say admin
var RoleNotice2 = ""; //ID also second role
var ChatNoticeList = ['admin', 'adm', 'odmen',"odmin"];
const RCON_IDENTIFIER_CONSOLE_RANGE_MIN = 1337000000;
const RCON_IDENTIFIER_CONSOLE_RANGE_MAX = 1337999999;
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const sequelize = new Sequelize({
database: 'database',
username: 'root',
password: 'password',
dialect: 'mysql',
logging: false,
operatorsAliases: false
});
const ChatDB = sequelize.define(RegisterTable, {
steamid: { type: Sequelize.STRING, primaryKey: true },
discordid: Sequelize.STRING,
timecode: Sequelize.STRING,
confirmed: Sequelize.STRING
}, {
tableName: RegisterTable,
timestamps: false
});
const BonusDB = sequelize.define(BonusTable, {
steamid: { type: Sequelize.STRING, primaryKey: true },
discordid: Sequelize.STRING,
gived: Sequelize.STRING,
}, {
tableName: BonusTable,
timestamps: false
});
const prefix = "!";
var bot = new Discord.Client({
autorun: true,
token: DiscordToken
});
bot.on('ready', function() {
console.log('Logged in as %s - %s\n', bot.username, bot.id);
});
var RconConnect = function(){
socket = new WebSocket('ws://'+RconIP+':'+RconPort+'/'+RconPassword+'');
socket.on('error', (error) => {
console.log("connected");
});
socket.on('close', (e) => {
console.log('RCON: Disconnected');
setTimeout(reconnect,1000*60);
});
socket.on('open', (e) => {
console.log('RCON: Connected');
});
socket.on('message', (serializedData) => {
const data = JSON.parse(serializedData);
if (data.Type === 'Chat')
{
const message = JSON.parse(data.Message);
console.log('RCON: '+message.Message);
const regex = RegExp(ChatNoticeList.join('|'), 'gi');
if (regex.test(message.Message)) {
bot.sendMessage({
to: ChatChannel,
message: '<@&'+RoleNotice1+'> <@&'+RoleNotice2+'> '+message.Message+''
});
}else{
bot.sendMessage({
to: ChatChannel,
message: ''+message.Message+''
});
}
}
});
}
RconConnect();
function reconnect() {
Console.log(`RCON: Trying to reconnect`);
setTimeout(() => {
RconConnect();
}, 1000 * 60 * 5);
}
bot.on('message', function(user, userID, channelID, message, event) {
if (channelID in bot.directMessages) {
var discordid
var args = message.substring(prefix.length).split(" ");
cmd = args[0];
if(cmd === "confirm")
{
ChatDB.findAll({
where: {timecode: args[1]},
attributes: ['steamid', 'discordid', 'confirmed', 'timecode']
}).then(CDB => {
console.log(CDB.length);
if(CDB.length===0){
bot.sendMessage({
to: userID,
message: 'Sorry, but your application is not present, write in the game / disreg'
});
}else if(CDB[0].dataValues.confirmed=="true"){
bot.sendMessage({
to: userID,
message: 'You are already registered'
});
}else if(CDB[0].dataValues.confirmed==="false"){
var steamid = CDB[0].dataValues.steamid;
ChatDB.update(
{
discordid: userID,
confirmed: 'true'
},{
where: {timecode: args[1]}}
);
confirmregister(steamid+" "+userID+" true");
bot.addToRole({"serverID":serverID,"userID":userID,"roleID":chatrole},function(err,response) {
if (err) console.error(err); /* Failed to apply role */
});
bot.sendMessage({
to: userID,
message: 'You have successfully confirmed the registration, you have been granted a role with access to the chat channel'
});
}
});
}
if(cmd === "GetBonus"){
ChatDB.findAll({
where: {discordid: userID},
attributes: ['steamid', 'discordid', 'confirmed', 'timecode']
}).then(CDB => {
if(CDB.length===0){
bot.sendMessage({
to: userID,
message: 'Sorry, but your application is not present, write in the game / disreg'
});
}else if(CDB[0].dataValues.confirmed=="false"){
bot.sendMessage({
to: userID,
message: 'You have not confirmed your registration, write! !confirm and the number that was issued to you was in the game'
});
}else if(CDB[0].dataValues.confirmed=="true"){
var steamid = CDB[0].steamid;
var discordid = CDB[0].discordid;
BonusDB.findAll({
where: {discordid: userID},
attributes: ['steamid', 'discordid', 'gived']
}).then(BDB => {
if(BDB.length===0){
BonusDB.bulkCreate([{
steamid: CDB[0].steamid,
discordid: userID,
gived: 'false'
}]);
SendBonus("|"+CDB[0].steamid+"|"+CDB[0].discordid+"|");
}else if(BDB[0].dataValues.gived==="true"){
bot.sendMessage({
to: userID,
message: 'You have already received a bonus'
});
}
});
}
});
}
}else{
var AdminRol = bot.servers[serverID].members[userID].roles.includes(AdminRole);
var ModerRol = bot.servers[serverID].members[userID].roles.includes(ModerRole)
var VipRol = bot.servers[serverID].members[userID].roles.includes(VipRole);
var ChatRol = bot.servers[serverID].members[userID].roles.includes(ChatRole);
var Args = message.substring(prefix.length).split(" ");
var ChatArgs = message.substring(0,1);
var MuteArgs = message.substring(5,64567);
var name = bot.users[userID].username;
cmd = Args[0];
if(ChatArgs!=="!" && channelID===ChatChannel){
if(AdminRol===true){
ChatsendMessage("|" + userID + "|Admin|" + message + "|");
}
else if(ChatRol===true && ModerRol===true){
ChatsendMessage("|" + userID + "|Moder|" + message + "|");
}
else if(ChatRol===true && VipRol===true){
ChatsendMessage("|" + userID + "|Vip|" + message + "|");
}
else if(ChatRol===true){
ChatsendMessage("|" + userID + "|User|" + message + "|");
}
}
if(AdminRol===true || ModerRol===true)
{
if(cmd === "mute" && channelID===ChatChannel){
mute(MuteArgs);
}
}
}
});
function ChatsendMessage(message) {
const packet = JSON.stringify({
Identifier: -1,
Message: `dissay ${message}`,
Name: 'WebRcon',
});
socket.send(packet);
}
function confirmregister(message) {
const packet = JSON.stringify({
Identifier: -1,
Message: `disconfirm ${message}`,
Name: 'WebRcon',
});
socket.send(packet);
}
function SendBonus(message) {
const packet = JSON.stringify({
Identifier: -1,
Message: `disbonus ${message}`,
Name: 'WebRcon',
});
socket.send(packet);
}
function mute(message) {
const packet = JSON.stringify({
Identifier: -1,
Message: `mute ${message}`,
Name: 'WebRcon',
});
socket.send(packet);
}
app.post('/rustbonus', function (req, res) {
var post = req.body;
console.log(post);
if(post.Check == "NotFind"){
bot.sendMessage({
to: post.Discordid,
message: 'I did not find you in the game, you need to go to our server'
});
}
if(post.Check == "Gived"){
BonusDB.update(
{gived: 'true'},
{where: {steamid: post.Steamid}}
);
bot.sendMessage({
to: post.Discordid,
message: 'You have been given a bonus for registration in a discord'
});
}
});
app.post('/rustrequest', function (req, res) {
var post = req.body;
if(post.Check == "DisReg")
{
ChatDB.bulkCreate([{
steamid: post.Steamid,
discordid: 0,
timecode: post.TimeCode,
confirmed: 'false'
}]);
res.send("Writed");
}
});
app.listen(ExpressPort);