-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
executable file
·189 lines (167 loc) · 4.8 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
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
const config = require("./config.json");
const filters = require("./filters.json");
const package = require("./package.json");
const request = require("request");
const client = new Client({ intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]});
let date = getTime();
let selector = 0;
// Do every hour
setInterval(function GameDealsNew() {
date = getTime();
freeGames();
}, 3600000);
function getTime() {
let dateWhole = new Date();
let date = dateWhole.getDate() + "." + (dateWhole.getMonth() + 1) + " " + dateWhole.getHours() + ":" + dateWhole.getMinutes();
return date;
}
function freeGames() {
const fetch = require("node-fetch");
fetch("https://api.reddit.com/r/gamedeals/new.json?sort=new&t=day")
.then(response => response.json())
.then(response => {
chooseGame(response)
});
/* You can just stack sources like this
fetch("https://api.reddit.com/r/someothersubreddit/new.json?sort=new&t=day")
.then(response => response.json())
.then(response => {
chooseGame(response)
});
*/
}
function chooseGame(list) {
if (list.data) {
game = list.data.children[selector].data
console.log("\n" + date + " Current game: " + game.title);
}
else {
console.log("couldn't get data");
return;
}
//check if the post is older than an hour (And 10sec because otherwise it still can get the same one in some cases)
let unixTime = (new Date).getTime() / 1000;
if (game.created_utc > (unixTime - 3610)) {
filterGame(game);
selector++;
chooseGame(list);
}
else {
console.log("Posted over an hour ago. Ignoring");
selector = 0;
}
return;
}
function filterGame(game) {
let valid = 0;
let i = 0;
console.log("Checking for the whitelisted words...");
filters.whitelist.forEach(function() {
if (game.title.toLowerCase().includes(filters.whitelist[i].toLowerCase())) {
console.log("Matched whitelisted word: " + filters.whitelist[i]);
valid = 1;
}
i++;
});
if(valid) {
i = 0;
console.log("Checking for the blacklisted words...");
filters.blacklist.forEach(function() {
if (game.title.toLowerCase().includes(filters.blacklist[i].toLowerCase())) {
console.log("Matched blacklisted word: " + filters.blacklist[i]);
valid = 0;
}
i++;
});
}
if(valid) {
// Check the other precentages for 'free' in wrong context
console.log("Checking for the precentages...");
let wrongPercentOff = game.title.match(/\d{2,3}%/ig);
if (wrongPercentOff != null) {
let wrongPercentNumber;
i = 0;
wrongPercentOff.forEach(function() {
wrongPercentNumber = wrongPercentOff[i].match(/\d{2,3}/ig);
if(wrongPercentNumber < 100) {
valid = 0;
console.log("Wrong precentage: " + wrongPercentNumber);
}
i++;
});
}
}
if(valid) {
// Check if there's any other values of money than 0
console.log("Checking for the money signs...");
let wrongMoneyOff = game.title.match(/^[0-9]+(\.[0-9]{1,})|[0-9]+(\.[0-9]{1,})€|€[0-9]+(\.[0-9]{1,})|[0-9]+(\.[0-9]{1,})\$|\$[0-9]+(\.[0-9]{1,})|£[0-9]+(\.[0-9]{1,})|[0-9]+(\.[0-9]{1,})£?$/ig);
if (wrongMoneyOff != null) {
let wrongMoneyNumber;
i = 0;
wrongMoneyOff.forEach(function() {
wrongMoneyNumber = wrongMoneyOff[i].match( /\d+/ig ).join([]);
if(wrongMoneyNumber > 0) {
valid = 0;
console.log("Wrong money value: " + wrongMoneyNumber);
}
i++;
});
}
}
if(valid) {
sendGame(game.title, game.url, game.thumbnail);
console.log("free");
return;
}
console.log("not free");
return;
}
function sendGame(gameTitle, gameUrl, gameThumb) {
if (gameThumb == "default" || gameThumb == "self") {
gameThumb = "https://puu.sh/B8rUY.jpg";
}
console.log("Free game info:\nName: ", gameTitle, "\nURL: ", gameUrl, "\nThumb URL: ", gameThumb);
// build an embed message for prettines points
const embed = {
color: config.embedColor,
title: 'Free Game!',
description: gameTitle + ": " + gameUrl,
thumbnail: {
url: 'https://puu.sh/AZxe5.png'
},
image: {
url: gameThumb,
},
};
// Send embed message to all servers
try {
client.guilds.cache.map((guild) => {
guild.channels.cache.map((channel) => {
// find the proper channel
if (channel.name === config.channel_name) {
channel.send({embeds: [embed]});
}
});
});
}
catch (err) {
console.log("Could not send message to all channels!\n" + err);
}
}
// Discord.js events
client.on("ready", function() {
client.user.setActivity("for free games!", { type: 3 });
console.log(date + ": Connected!");
console.log("Version: " + package.version);
//freeGames();
});
client.on('uncaughtException', (e) => console.error(date + ": " + e));
client.on('error', (e) => console.error(date + ": " + e));
client.on('warn', (e) => console.error(date + ": " + e));
// The bot logins here and goes to ready state
client.login(config.token);