-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
316 lines (284 loc) · 11.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
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
require('dotenv').config();
const Discord = require('discord.js');
const QuickChart = require('quickchart-js');
const axios = require("axios");
const chartconf = require('./modulesconf');
const schedule = require('node-schedule');
var mysql = require('mysql');
const client = new Discord.Client(
{intents: ["GUILDS", "GUILD_MESSAGES" , "GUILD_WEBHOOKS" ,"GUILD_SCHEDULED_EVENTS"]}
); //create new client intent
const PREFIX = "%";
//Initilize BOT
client.on('ready', () => {
console.log("Youre now Online");
console.log("Servers List:");
client.guilds.cache.forEach((guild) => {
console.log(" - " + guild.name);
});
});
//CMDs Here
client.on('messageCreate', (message) => {
if (message.author.bot) return;
if (message.content.startsWith(PREFIX)) {
const [CMD_NAME, ...args] = message.content
.trim()
.substring(PREFIX.length)
.split(/\s+/);
//6 MONTHS
if (CMD_NAME === "6m"){
var itemname = args.join(" ");
let itemname2 = itemname.toString();
console.log(itemname2);
///DB CNNCTION OPEN
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT *, DATE_FORMAT(datetimestamp,'%m-%d-%Y') AS newDate FROM pricehistory3 WHERE itemname= '"+itemname2+"' AND datetimestamp >= (NOW() - INTERVAL 6 MONTH) ORDER BY newDate ASC;", function (err, result, fields) {
if (err) throw err;
if(result.length > 0){
var newdata = [];
result.forEach(row => {
newdata.push([row.newDate,row.itemprice]);
});
//START CHART
const myChart = new QuickChart();
myChart.setConfig(chartconf.months6(newdata,itemname2));
///SEND CHART
getUrl(myChart).then(data =>{
console.log(data)
message.channel.send(`Here's what we got ${data}`);
}).catch(function (error) {
console.log('Error')
console.log(error)
});
}else{
message.channel.send(`The item that your trying to search is not on my Database try adding it to the watchlist`);
}
});
});
//1 DAY RANGE
}else if(CMD_NAME === "1d"){
var itemname = args.join(" ");
let itemname2 = itemname.toString();
console.log(itemname2);
///DB CNNCTION OPEN
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT *, DATE_FORMAT(datetimestamp,'%m-%d-%Y %H:%i') AS newDate FROM pricehistory3 WHERE itemname= '"+itemname2+"' AND datetimestamp >= (NOW() - INTERVAL 1 DAY) ORDER BY newDate ASC;", function (err, result, fields) {
if (err) throw err;
if(result.length > 0){
var newdata = [];
result.forEach(row => {
newdata.push([row.newDate,row.itemprice]);
});
//START CHART
const myChart = new QuickChart();
myChart.setConfig(chartconf.day1(newdata,itemname2));
///SEND CHART
getUrl(myChart).then(data =>{
console.log(data)
message.channel.send(`Here's what we got ${data}`);
}).catch(function (error) {
console.log('Error')
console.log(error)
});
}else{
message.channel.send(`The item that your trying to search is not on my Database try adding it to the watchlist`);
}
});
});
}else if(CMD_NAME === "7d"){
var itemname = args.join(" ");
let itemname2 = itemname.toString();
console.log(itemname2);
///DB CNNCTION OPEN
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT *, DATE_FORMAT(datetimestamp,'%m-%d-%Y') AS newDate FROM pricehistory3 WHERE itemname= '"+itemname2+"' AND datetimestamp >= (NOW() - INTERVAL 7 DAY) ORDER BY newDate ASC;", function (err, result, fields) {
if (err) throw err;
if(result.length > 0){
var newdata = [];
result.forEach(row => {
newdata.push([row.newDate,row.itemprice]);
});
//START CHARTnbnbn
const myChart = new QuickChart();
myChart.setConfig(chartconf.day7(newdata,itemname2));
///SEND CHART
getUrl(myChart).then(data =>{
message.channel.send(`Here's what we got ${data}`);
}).catch(function (error) {
console.log('Error')
});
}else{
message.channel.send(`The item that your trying to search is not on my Database try adding it to the watchlist`);
}
console.log(result);
});
});
}else if(CMD_NAME === "1m"){
var itemname = args.join(" ");
let itemname2 = itemname.toString();
console.log(itemname2);
///DB CNNCTION OPEN
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT *, DATE_FORMAT(datetimestamp,'%m-%d-%Y') AS newDate FROM pricehistory3 WHERE itemname= '"+itemname2+"' AND datetimestamp >= (NOW() - INTERVAL 1 MONTH) ORDER BY newDate ASC;", function (err, result, fields) {
if (err) throw err;
if(result.length > 0){
var newdata = [];
result.forEach(row => {
newdata.push([row.newDate,row.itemprice]);
});
//START CHART
const myChart = new QuickChart();
myChart.setConfig(chartconf.month1(newdata,itemname2));
///SEND CHART
getUrl(myChart).then(data =>{
console.log(data)
message.channel.send(`Here's what we got ${data}`);
}).catch(function (error) {
console.log('Error')
console.log(error)
});
console.log(newdata);
}else{
message.channel.send(`The item that your trying to search is not on my Database try adding it to the watchlist`);
}
console.log(result);
});
});
}else if(CMD_NAME === "add-watchlist"){
var itemname = args.join(" ");
let itemstring = itemname.toString();
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("INSERT INTO watchlist (items) VALUES ('"+itemstring+"');", function (err, result, fields) {
if (err) throw err;
console.log(result.affectedRows);
console.log(itemstring+' has been added to my watchlist');
message.channel.send(`${itemstring} has been added to my watchlist`);
});
});
}else if(CMD_NAME === "show-watchlist"){
var itemname = args.join(" ");
let itemstring = itemname.toString();
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT * FROM watchlist;", function (err, result, fields) {
if (err) throw err;
if(result.length > 0){
var watchlist = [];
// result.forEach(row => {
// watchlist.push([row.items]);
// });
const exampleEmbed = new Discord.MessageEmbed();
exampleEmbed.setColor("#3c00ff");
exampleEmbed.setTitle("🔥On Kana's Watchlist🔥");
for (var i = 0; i < result.length; i++) {
exampleEmbed.addFields({
name: "-------------",
value: result[i]["items"],
});
}
message.channel.send({ embeds: [exampleEmbed] });
}
});
});
}
}
});
async function getUrl(chart)
{
const url = await chart.getShortUrl();
return url;
}
//DISCORD TOKEN
client.login(process.env.CLIENT_TOKEN);
const watchjob = schedule.scheduleJob('*/59 * * * *', function(){
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("SELECT items FROM watchlist;", function (err, result, fields) {
if(result.length > 0){
result.forEach(row => {
const items = row.items;
var data1 = {
tradezone: "13",
category: "0",
search: items,
};
var sentdata = Object.keys(data1).map((key) => key + "=" + data1[key]).join("&");
var url = "http://meaty.dfprofiler.com/browsemarketplace.php?function=browseMarketWithCredits";
axios({
method: "post",
url: url,
data: sentdata,
})
.then(function (response) {
var result = response.data;
var resname1 = result[1].name;
var resprice1 = result[1].price;
const consql = mysql.createConnection({
host: process.env.HOST_DB,
user: process.env.USER_DB,
password: process.env.PASS_DB,
database: process.env.DB_DB
});
consql.connect(function(err) {
if (err) throw err;
consql.query("INSERT INTO pricehistory3( itemname, itemprice) VALUES ( '"+resname1+"' , "+resprice1+");", function (err, result, fields) {
if (err) throw err;
console.log(result.affectedRows);
console.log(resname1+' Insert Succesfully');
});
});
}).catch(function (error) {
console.log(resname1+' Insert Not Succesfull');
});
});
}
});
});
});