-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
345 lines (321 loc) · 9.96 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
const { Client, MessageEmbed } = require('discord.js')
const fs = require('fs');
const { prefix, token, perrol } = require('./config.json');
const client = new Client({
disableEveryone: true
});
const { google } = require('googleapis');
const { firebaserules } = require('googleapis/build/src/apis/firebaserules');
const { Console } = require('console');
const auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
const clients = auth.getClient();
const googleSheets = google.sheets({ version: "v4", auth: clients });
const spreadsheetId = "1JruH9prKK6uD0YLMilNUWqmuKlUf8Zr39IUtBRKZNBg";
const arraysort =require('array-sort')
//main globals
let post = {};
let points = [];
let sht
function fread(worksheet, cell) {
try {
post = {
rd : worksheet[0],
kls : worksheet[1],
win : worksheet[2],
pts : worksheet[3],
team : worksheet[4],
did :worksheet[5]
}
points.push(post);
post = {};
}catch(er) {
console.log('');
}
}
function selsheet(grp){
if(grp == "groupa"){
sht = "sheet1"
}
else if(grp == "groupb"){
sht = "sheet2"
}
else if(grp == "groupc"){
sht = "sheet3"
}
else if(grp == "groupd"){
sht = "sheet4"
}
else if(grp == "groupe"){
sht = "sheet5"
}
else if(grp == "groupf"){
sht = "sheet6"
}
else if(grp == "groupg"){
sht = "sheet7"
}
else if(grp == "grouph"){
sht = "sheet8"
}
return sht
}
function updateit(ar , kl , wn , pts , ran){
console.log("come check")
googleSheets.spreadsheets.values.update({
auth,
spreadsheetId,
range : ran,
valueInputOption: 'USER_ENTERED',
resource: {
range : ran,
majorDimension: 'ROWS',
values: [[ar , kl , wn , pts]]}
})
}
async function readit(gsht) {
try{
console.log("the val of ghst inside is : " + gsht)
let shet = gsht + "!A:F"
console.log("the val of shet is : " + shet)
const getRows = await googleSheets.spreadsheets.values.get({
auth,
spreadsheetId,
range: shet,
});
points = [];
for (let i = 0; i <= 10; i++)
fread(getRows.data.values[i], i);
return points;
}catch(er){
console.log(er)
}
}
async function writeit(nvals,gsht){
let shet = gsht + "!A:B"
await googleSheets.spreadsheets.values.append({
auth,
spreadsheetId,
range: shet,
valueInputOption: "USER_ENTERED",
resource: {
values: [nvals],
},
});
console.log("write successfull")
return points;
}
//it is used for embeding the messages
function embeds(title, des, color) {
const exampleEmbed = new MessageEmbed()
.setColor(`${color}`)
.setTitle(`${title}`)
.setDescription(`${des}`)
.setTimestamp()
return exampleEmbed;
}
//bot on here
client.on('ready', () => {
console.log("bot online!!")
client.user.setPresence({
status: 'online',
activity: {
type: 'PLAYING',
name: 'Created By Monk and Team Contact: Monk#4158',
},
});
});
//used for variables
let c = 0
//readit()
let tgroups = ['groupa','groupb','groupc','groupd','groupe','groupf','groupg','grouph']
//commands for the bot starts here
client.on('message', async (msg) => {
if (msg.content.startsWith("*seeme")) {
const args = msg.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (!args.length) {
msg.channel.send(embeds("Argument Error", `You didn't provide any arguments : ${msg.author.username}!`, "#d40407"));
return
}
if(tgroups.indexOf(args[0]) == -1){
console.log("argument error ")
msg.channel.send(embeds("Argument Error", `You didn't provide crt group : ${msg.author.username}!`, "#d40407"));
return
}
console.log("the val of grop is : " + args[0])
let gsht = selsheet(args[0])
readit(gsht).then(data => {
let val = 0;
let n = data.length
console.log("the length of points is :" + n)
for (c = 0; c <= n; c++) {
console.log(`the val is ${data[c].did} , ${msg.author.id}`)
if (data[c].did == msg.author.id) {
console.log(`the pts is ${data[c].pts}`)
val = c
break;
}
else {
val = 0
}
}
console.log(`the val is ${val}`)
if (val != 0) {
msg.author.send(embeds("View points", ` ${msg.author.username} has ${data[val].pts} points `, "#09ff00"))
msg.reply(embeds("View points", `${msg.author.username}plz check ur DM for points :`, "#09ff00"))
console.log(` =====> ${msg.author.username} requested for points`);
}
else {
msg.reply(embeds("View points", "user has no Account", "#e67000"));
console.log(` =====> ${msg.author.username} requested for points`);
}
}).catch(err => {
// console.log(err);
msg.reply(embeds("View points", "user has no Account", "#e67000"));
console.log(` =====> ${msg.author.username} requested for points`);
});
}
else if (msg.content.startsWith('*open')) {
const args = msg.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (!msg.member.roles.cache.has(`${perrol}`)) {
msg.reply(embeds("account open", `don't have the permession to do that `, "#d40407"))
console.log(`${msg.author.id} have tried to acess the higher commands ${msg.author.username.tag}`)
return;
}
if (!args.length) {
return msg.channel.send(embeds("Argument Error", `You didn't provide any arguments : ${msg.author.user}!`, "#d40407"));
}
if(tgroups.indexOf(args[3]) == -1){
console.log("argument error ")
msg.channel.send(embeds("Argument Error", `You didn't provide crt group : ${msg.author.username}!`, "#d40407"));
return
}
let gsht = selsheet(args[3])
console.log("the val of sheet is : " + gsht)
readit(gsht);
const taggedUser = msg.mentions.users.first();
let id = taggedUser.id;
let team = args[2]
if(args[2] == '' || args[2] == undefined){
console.log("Argument Error")
msg.channel.send(embeds("Argument Error", `You didn't provide any arguments : ${msg.author.username}!`, "#d40407"));
return
}
const valch = points.find(vala => vala.did == id)
if (!valch) {
console.log("the id is : " + id)
rval = [0,0,0,0,team,id]
writeit(rval,gsht)
msg.reply(embeds("account open", `Account opened successfully for ${taggedUser.username}`, "#09ff00"))
console.log(` ======> Account opend for ${msg.author.username} and has `);
}
else {
console.log("already reg user ")
msg.reply(embeds("account open", `User already exists : ${taggedUser.username}`, "#d40407"))
}
}
else if (msg.content.startsWith(`${prefix}stands`)) {
try{
const args = msg.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if(tgroups.indexOf(args[0]) == -1){
console.log("argument error ")
msg.channel.send(embeds("Argument Error", `You didn't provide crt group : ${msg.author.username}!`, "#d40407"));
return
}
console.log("the val of agrs is : " + args[0])
let gsht = selsheet(args[0])
console.log("the val of sheet is : " + gsht)
let tpts = await readit(gsht);
console.log("the val of : " + tpts)
console.log("the len of pts " + tpts.length)
for(let k = 1 ; k < tpts.length ; k ++){
tpts[k].pts = tpts[k].pts * 1
}
let spts = arraysort(tpts , 'pts' , {reverse : true})
console.log("the sorted array is :" + spts)
let n = spts.length
msg.reply(" here is the standings ")
console.log("=================================")
let printStr = '===================\n';
for (c = 0; c < n; c++) {
printStr += `| \t ${spts[c].rd} \t | \t ${spts[c].kls} \t | \t ${spts[c].win} \t | \t ${spts[c].pts}\t | \t ${spts[c].team} \t|\n================ \n`;
}
console.log(printStr);
msg.channel.send(embeds("standings", printStr, "#A19882"))
// msg.channel.send(printStr)
}catch(er){
console.log(er);
}
}
else if (msg.content.startsWith('*addto')) {
const args = msg.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
if (!msg.member.roles.cache.has(`${perrol}`)) {
msg.reply(embeds("account open", `don't have the permession to do that `, "#d40407"))
console.log(`${msg.author.id} have tried to acess the higher commands ${msg.author.username.tag}`)
return;
}
if (!args.length) {
return msg.channel.send(embeds("Argument Error", `You didn't provide any arguments : ${msg.author}!`, "#d40407"));
}
if (!Number(args[2]) || !Number(args[3]) || !Number(args[4]) || !Number(args[5])) {
msg.reply(embeds("command Error", `plz use the command correctly : ${msg.author.username}!`, "#d40407"));
return;
}
if(tgroups.indexOf(args[6]) == -1){
console.log("argument error ")
msg.channel.send(embeds("Argument Error", `You didn't provide crt group : ${msg.author}!`, "#d40407"));
return
}
let gsht = selsheet(args[6])
console.log("the val of sheet is : " + gsht)
readit(gsht).then( data=> {
const taggedUser = msg.mentions.users.first();
id = taggedUser.id;
console.log("the value of of arg is : " + args[2]);
let ap = args[5];
let ar = args[2]
let kl = args[3]
let wn = args[4]
let alen = data.length
let ne = 0;
for (let c = 1; c < alen; c++) {
console.log("the val is : " + data[c].did)
if (data[c].did === id) {
let tot = (data[c].pts * 1) + (ap * 1)
console.log(` tag user : ${data[c].did} and ${id} , val is ${ap} and tot is ${tot}`)
data[c].pts = (tot * 1)
data[c].rd = ar
data[c].kls = kl
data[c].win = wn
console.log("the val : " + c)
console.log("the val of pts is : " + data[c].pts)
let k = c+1
let ran = gsht + "!A" + k +":D" + k
updateit(ar , kl , wn , tot ,ran)
ne = ne + 1;
}
}
if(ne == 0 ){
console.log("account error ")
msg.channel.send(embeds("points", `No user found : ${taggedUser.username}!`, "#d40407"));
return
}
else {
console.log(` =====> the user ${taggedUser.username} gets ` + ap + ' goes to ' + data.id);
msg.reply(embeds("Points", `gave ${ap} to ${taggedUser.username}`, "#f47fff"));
}
})
}
else if( msg.content.startsWith('*')){
msg.reply(embeds("command Error", `plz use the command correctly : ${msg.author.username}!`, "#d40407"));
return
}
});
let itoken = token;
client.login(itoken);