-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
108 lines (77 loc) · 3.44 KB
/
functions.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
const editJsonFile = require("edit-json-file");
const config = require("./config");
let variablesfile = editJsonFile(`./storage/variables.json`);
module.exports = {
delay: function(time) {
/* function to add delay, works with a promise*/
return new Promise(function(resolve) {
setTimeout(resolve, time)
});
},
getRandomInt: function(max) {
/*
function getting a random number from 0 to arg if 1 arg and from arg1 to arg2 if 2 args
example :
=> getRandomInt(8) -> picks a random number from 0 to 8
=> getRandomInt(5, 65) -> picks a random number from 5 to 65
*/
return Math.floor(Math.random() * Math.floor(max)+1);
},
getTime: function(){
/* Creates date object */
date_ob = new Date();
date = date_ob.getDate().toString();
month = date_ob.getMonth().toString();
year = date_ob.getFullYear().toString();
if(date.length === 1){
date = "0" + date
}
if(month.length === 1){
month = "0" + month
}
dmy = date + "/" + month + "/" + year
/* Gets hours, minutes and seconds */
hms = date_ob.toLocaleTimeString();
return `[ ${dmy} | ${hms} ]`
},
loginToDiscord: async function(page){
/* Logs to Discord */
const { getTime } = require("./functions")
console.log(`${getTime()} > Loading discord login page...`)
await page.goto('https://discord.com/channels/@me');
console.log(`${getTime()} > Discord login page loaded !`)
console.log(`${getTime()} > Typing email & password...`)
await page.click('input[type="email"]');
await page.keyboard.type(config.email);
await page.click('input[type="password"]');
await page.keyboard.type(config.mdp);
console.log(`${getTime()} > Email & password typed ! `)
await page.click('button[type="submit"]');
console.log(`${getTime()} > Login request to discord...`);
await page.waitForSelector('div[class="childWrapper-anI2G9"]');
console.log(`${getTime()} > Logged in to discord !`);
},
postMessage: async function(page){
/* Gets the channels defined in storage/variables.json and paste message in all of them */
const { delay, getTime } = require("./functions");
channels = variablesfile.get("channels")
console.log(`${getTime()} > Started sending messages, processing...`)
for(i=0;i<channels.length;i++){
await page.goto(channels[i])
await page.waitForSelector('div[class="childWrapper-anI2G9"]');
await delay(500);
await page.keyboard.type(variablesfile.get("message"));
await delay(100);
await page.keyboard.press("Enter");
await delay(500);
};
console.log(`${getTime()} > Finnished sending messages in the ${channels.length} channels !`)
console.log(`${getTime()} > Waiting between 00:10:30 and 00:11:00 to relaunch`)
},
start: async function(page){
/* Calls the function loginToDiscord, copyText, postMessage one by one */
const { loginToDiscord, copyText, postMessage } = require("./functions")
await loginToDiscord(page);
await postMessage(page);
},
}