-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
251 lines (225 loc) Β· 9.28 KB
/
bot.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
console.log('Bot is starting!');
const Twit = require('twit');
const Config = require('./config');
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;
const $ = jQuery = require('jquery')(window);
const T = new Twit(Config);
const KEY = process.env.YT_KEY;
const dangerZone = 10000;
const maxTweetsInHour = 10;
const messages = [
'We will not stop fighting!',
'This is not the end!',
'Everyone subscribe!',
'This cannot be the end!',
'We need to do something!',
'A sub audit could screw us over!'
];
var tweetsInHour = 0;
console.log(`Bot has started!`)
tweetIt(`The subgap is currently ${subGap().toLocaleString('en')}! \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
//All async functions called
alertGap();
negativeAlert();
tweetSubCount();
reply('pewdiepie');
replyToSummons();
resetConstraints();
function subGap()
{
let pewdsSubCount = getChannelData('pewdiepie');
let tseriesSubCount = getChannelData('tseries');
return (pewdsSubCount - tseriesSubCount);
}
async function alertGap()
{
let previous = subGap();
setInterval(function(){
let subgap = subGap();
if(subgap < 500 && subgap > 0)
{
tweetIt(`π¨π¨ALERT! SUBGAP < 500 ALERT!π¨π¨ \n\nTHE SUBGAP IS NOW ${subgap.toLocaleString('en')}! \n${selectMessage().toUpperCase()} \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
}
else if(subgap < 1000 && subgap > 0)
{
tweetIt(`π¨π¨ALERT! SUBGAP < 1,000 ALERT!π¨π¨ \n\nThe subgap is now ${subgap.toLocaleString('en')}! \n${selectMessage()} \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
}
else if(subgap < 5000 && subgap > 0)
{
tweetIt(`π¨π¨ALERT! SUBGAP < 5,000 ALERT!π¨π¨ \n\nThe subgap is now ${subgap.toLocaleString('en')}! \n${selectMessage()} \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
}
else if(subgap < dangerZone && subgap > 0)
{
tweetIt(`π¨π¨ALERT! ALERT!π¨π¨ \n\nThe subgap is now ${subgap.toLocaleString('en')}! \n${selectMessage()} \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
}
else if(subgap > dangerZone && previous < dangerZone)
{
tweetIt(`No alert right now. The subgap is now back above ${dangerZone.toLocaleString()} thanks to all you nine year olds! ${subgap.toLocaleString()}! \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`)
}
previous = subgap;
}, 1000*60*60);
}
async function negativeAlert()
{
let previousGap = 1;
setInterval(function(){
let subgap = subGap();
if(subgap < 0 && previousGap > 0)
{
tweetIt(`π¨π¨ALERT! ALERT!π¨π¨ THE SUBGAP IS NOW NEGATIVE! ${subgap.toLocaleString('en')}! \n\nT-SERIES HAS PASSED PEWDS! THIS IS NOT THE END! \nEVERYONE SUBSCRIBE TO GET PEWDIEPIE BACK! ${selectMessage().toUpperCase()} LETS GET #Sub2Pewds TRENDING! \n\n#PewDiePie #TSeries #MemeReview #Subgap`);
}
else if(subgap < -15000 && previousGap > -15000)
{
tweetIt(`ππππππππSUBGAP < -15,000!ππππππππ\n\n${subgap.toLocaleString('en')}! SOMEONE DO SOMETHING! SAVE US!\n\n#PewDiePie #TSeries #MemeReview #Subgap`);
}
else if(subgap > 0 && previousGap < 0)
{
tweetIt(`π¨π¨ALERT! ALERT!π¨π¨ THE GAP IS POSITIVE AGAIN! \n\n${subgap.toLocaleString('en')}! \nWE WILL KEEP FIGHTING! \n\n#PewDiePie #TSeries #MemeReview #Subgap`);
}
previousGap = subgap;
}, 1000*60*5)
}
async function tweetSubCount()
{
setInterval(function(){
let pewdsCount = getChannelData('pewdiepie');
let tCount = getChannelData('tseries')
let subgap = pewdsCount - tCount;
tweetIt(`PewDiePie currently has ${pewdsCount.toLocaleString('en')}. \n\nT-Series currently has ${tCount.toLocaleString('en')}. \n\nThe subgap is currenly at ${subgap.toLocaleString('en')}. \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`);
}, 1000*60*60*6)
}
async function resetConstraints()
{
setInterval(function(){
tweetsInHour = 0;
}, 1000*60*60)
}
async function replyToSummons()
{
let replyToId;
let previousReplyId = [];
setInterval(function(){
T.get('search/tweets', { q: '@PewDieB0t the #SubGap', count: 1 }, function(err, data, response) {
replyToId = data.statuses[0].id_str;
let user = data.statuses[0].user
if(replyToId !== null && !previousReplyId.includes(replyToId))
{
let subgap = subGap();
if(!isNaN(subgap))
{
var opts = {
in_reply_to_status_id: replyToId,
status: `@${user.screen_name} The subgap is currenly at ${subgap.toLocaleString('en')}! #PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`
}
T.post('statuses/update', opts, function(err, data, response){
if(err) console.log(`Something went horribly wrong!: ${err}`)
else { console.log(`Tweeted: 'The subgap is currenly at ${subgap.toLocaleString('en')}! \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds' in response to: ${user.screen_name}.`); previousReplyId.push(replyToId); replyToId = null; likeTweet(replyToId); likeTweet(data.id_str); };
});
}
else console.log(`Sorry, we have sent too many requests to the youtube API. Please wait until 2 am CST for the quota to reset.`);
}
});
}, 1000*15)
}
function selectMessage()
{
let r = Math.floor(Math.random()*messages.length);
return messages[r];
}
/**
* Replies to the latest tweet from a person.
* @param {string} screenName - Who you're replying to.
* @param {string} tweet - What you're tweeting.
*/
async function reply(screenName)
{
let replyToId;
let previousReplyId;
setInterval(function(){
T.get('statuses/user_timeline', { screen_name: screenName, count: 1 }, function(err, data, response) {
replyToId = data[0].id_str;
if(replyToId !== null && replyToId !== previousReplyId)
{
let subgap = subGap();
if(!isNaN(subgap))
{
var opts = {
in_reply_to_status_id: replyToId,
status: `@${screenName} The subgap is currenly at ${subgap.toLocaleString('en')}! #PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds`
}
T.post('statuses/update', opts, function(err, data, response){
if(err) console.log(`Something went horribly wrong!: ${err}`)
else { console.log(`Tweeted: 'The subgap is currenly at ${subgap.toLocaleString('en')}! \n\n#PewDiePie #TSeries #MemeReview #Subgap #Sub2Pewds' in response to: ${screenName}`); previousReplyId = replyToId; replyToId = null; likeTweet(replyToId); likeTweet(data.id_str); };
});
}
else console.log(`Sorry, we have sent too many requests to the youtube API. Please wait until 2 am CST for the quota to reset.`);
}
});
}, 1000*15)
}
/**
* Deletes a tweet.
* @param {string} id_str - The tweet you're deleting.
*/
function deleteTweet(id_str)
{
T.post('statuses/destroy/:id', { id: id_str }, function(err, data, response){
if(err) console.log(`Something went horribly wrong!: ${err}`)
else { console.log(`Deleted tweet with id: ${id_str}`) }
});
}
/**
* Tweets something out.
* @param {string} statusText - What you're tweeting.
*/
function tweetIt(statusText)
{
if(tweetsInHour == maxTweetsInHour)
{
console.log(`Sorry, we have already reached our limit of tweets in an hour: ${maxTweetsInHour}`);
return;
}
else if(isNaN(subGap()))
{
console.log(`Sorry, we have sent too many requests to the youtube API. Please wait until 2 am CST for the quota to reset.`);
return;
}
else
{
T.post('statuses/update', { status: statusText }, function(err, data, response) {
if(err) console.log(`Something went horribly wrong!: ${err}`)
else { console.log(`Tweeted: '${statusText}'`); tweetsInHour++; likeTweet(data.id_str); }
});
}
}
/**
* Likes a tweet.
* @param {string} id_str - The string id of the tweet you are liking.
*/
function likeTweet(id_str)
{
T.post('favorites/create', { id: id_str }, function(err, data, response){
if(err) console.log(`Something went horribly wrong!: ${err}`)
else { console.log(`Liked a tweet with id: ${id_str}`); };
});
}
/**
* Gets the sub count of a channel.
* @param {string} channel - The channel you are trying to get the sub count of.
*/
function getChannelData(channel)
{
var ytApiUrl = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=' + channel + '&key=' + KEY;
var subsVar;
$.ajaxSetup({
async: false
});
$.getJSON(ytApiUrl, function(result){
subsVar = +result['items'][0]['statistics'].subscriberCount;
});
return subsVar;
}