forked from Tywele/TheCraft-Discord-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
351 lines (305 loc) · 14.9 KB
/
app.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
346
347
348
349
350
351
const Discord = require('discord.js');
const client = new Discord.Client();
const settings = require('./settings.json');
const snekfetch = require('snekfetch');
const api = 'https://api.xivdb.com/search?string='; // https://github.com/xivdb/api
const sql = require('sqlite');
// open sqlite file
sql.open('./log.sqlite');
// react to starting the bot
client.on('ready', () => {
console.log('I\'m online\nI\'m online');
client.user.setActivity('FFXIV Market');
sql.run('CREATE TABLE IF NOT EXISTS users (userId TEXT, name TEXT)');
sql.run('CREATE TABLE IF NOT EXISTS requests (item TEXT, amount INTEGER, id TEXT, accepted BOOLEAN)');
sql.run('CREATE TABLE IF NOT EXISTS offers (item TEXT, price INTEGER, quality TEXT, id TEXT, sold BOOLEAN)');
sql.run('CREATE TABLE IF NOT EXISTS userHasRequests(userId TEXT, requestId TEXT)');
sql.run('CREATE TABLE IF NOT EXISTS userHasOffers(userId TEXT, offerId TEXT)');
});
// react to messages
client.on('message', message => {
// stop when message has no prefix
if (!message.content.startsWith(settings.prefix)) return;
// stop bot from responding to itself
if (message.author.bot) return;
// create user entry in DB
sql.get('SELECT * FROM users WHERE userId = ?', [message.author.id]).then(row => {
if (!row) {
sql.run('INSERT INTO users (userId, name) VALUES (?, ?)', [message.author.id, message.author.username]);
}
});
// get cmd args
let args = message.content.slice(settings.prefix.length).trim().split(settings.delimiter);
let command = args.shift().toLowerCase();
for (var i = 0; i < args.length; i++) {
args[i] = args[i].trim();
}
console.log(command);
console.log(args);
// request gathering
if (message.content.startsWith(settings.prefix + 'request')) {
let item;
let amount;
let name_de;
let name_en;
let name_ja;
let name_fr;
let embed;
let id;
// stop processing command if not enough arguments
if (args.length != 2) {
message.channel.send('Info! Requesting an item to be gathered works as follows: \`+request;[item];[amount]\`\nThe name must be provided in English.');
return;
}
// check if first argument is a string
if (typeof (args[0]) === 'string') {
item = args[0];
} else {
message.channel.send(`Error! Please provide an item name`);
return;
}
// check if second argument is a number
if (!isNaN(args[1])) {
amount = parseInt(args[1]);
} else {
message.channel.send(`Error! Please provide a number`);
return;
}
// call api.xivdb.com
snekfetch.get(api + item + '&one=items').then(r => {
let url;
let db_url;
if (r.body.items.results.length <= 0) url = "";
else {
url = r.body.items.results[0].url_api;
db_url = r.body.items.results[0].url_xivdb;
}
if (url !== "") {
snekfetch.get(url).then(t => {
name_de = t.body.name_de;
name_en = t.body.name_en;
name_ja = t.body.name_ja;
name_fr = t.body.name_fr;
id = parseInt(Date.now()).toString();
embed = new Discord.RichEmbed()
.setDescription(`${name_en}\n${name_de}\n${name_fr}\n${name_ja}\n\nID: \`${id}\`\n\nType \`+accept;[id]\` to accept the request.`)
.setTitle(db_url)
.setURL(db_url)
.setAuthor(`${message.author.username} requests x${amount}`);
message.channel.send({
embed: embed
});
// insert to DB
sql.run('INSERT INTO requests (item, amount, id, accepted) VALUES (?, ?, ?, ?)', [item, amount, id, 0]);
sql.run('INSERT INTO userHasRequests (userId, requestID) VALUES (?, ?)', [message.author.id, id]);
});
} else {
id = parseInt(Date.now()).toString();
embed = new Discord.RichEmbed()
.setDescription(`No item with the provided name found\n\nID: \`${id}\`\n\nType \`+accept;[id]\` to accept the request.`)
.setTitle(db_url)
.setURL(db_url)
.setAuthor(`${message.author.username} requests x${amount} of ${item}`);
message.channel.send({
embed: embed
});
// insert to DB
sql.run('INSERT INTO requests (item, amount, id, accepted) VALUES (?, ?, ?, ?)', [item, amount, id, 0]);
sql.run('INSERT INTO userHasRequests (userId, requestID) VALUES (?, ?)', [message.author.id, id]);
}
});
} else
// accept request
if (message.content.startsWith(settings.prefix + 'accept')) {
let id;
// stop processing command if not enough arguments
if (args.length != 1) {
message.channel.send('Info! Accepting a request works as follows: \`+accept;[id]\`');
return;
}
// check if first argument is a number
if (!isNaN(args[0])) {
id = parseInt(args[0]);
} else {
message.channel.send(`Error! Please provide a number`);
return;
}
sql.get(`SELECT * FROM requests WHERE id = ${id}`).then(row => {
if (row.accepted == 1) {
message.channel.send('This request has already been accepted.');
return;
}
let embed = new Discord.RichEmbed().setDescription(`${message.author.username} accepted the request to gather ${row.item} x${row.amount}`);
message.channel.send({
embed: embed
});
});
sql.run(`UPDATE requests SET accepted = 1 WHERE id = ${id}`);
} else
// post an item for sale
if (message.content.startsWith(settings.prefix + 'offer')) {
let item;
let quality;
let name_de;
let name_en;
let name_fr;
let name_ja;
let price;
let id;
let embed;
// stop processing command if not enough arguments
if (args.length != 3) {
message.channel.send('Info! Posting an item for sale works as follows: \`+offer;[item];[price];[quality]\`\nThe must be provided in English.');
return;
}
if (typeof (args[0]) === 'string') {
item = args[0];
} else {
message.channel.send(`Error! Please provide an item name`);
return;
}
if (!isNaN(args[1])) {
price = parseInt(args[1]);
} else {
message.channel.send('Error! Please provide a number');
return;
}
if (args[2].toLowerCase() == 'nq' || args[2].toLowerCase() == 'hq') {
quality = args[2];
quality = quality.toUpperCase();
} else {
message.channel.send('Error! Quality must be either \`NQ\` or \`HQ\`');
return;
}
// call api.xivdb.com
snekfetch.get(api + item + '&one=items').then(r => {
let url;
let db_url;
if (r.body.items.results.length <= 0) url = "";
else {
url = r.body.items.results[0].url_api;
db_url = r.body.items.results[0].url_xivdb;
}
if (url !== "") {
snekfetch.get(url).then(t => {
name_de = t.body.name_de;
name_en = t.body.name_en;
name_ja = t.body.name_ja;
name_fr = t.body.name_fr;
id = parseInt(Date.now()).toString();
embed = new Discord.RichEmbed()
.setDescription(`${name_en}\n${name_de}\n${name_fr}\n${name_ja}\n\nID: \`${id}\`\n\nType \`+buy;[id]\` to buy the item.`)
.setTitle(db_url)
.setURL(db_url)
.setAuthor(`${message.author.username} sells below item for ${price} Gil`);
message.channel.send({
embed: embed
});
// insert to DB
sql.run('INSERT INTO offers (item, price, quality, id, sold) VALUES (?, ?, ?, ?, ?)', [item, price, quality, id, 0]);
sql.run('INSERT INTO userHasOffers (userId, offerID) VALUES (?, ?)', [message.author.id, id]);
});
} else {
id = parseInt(Date.now()).toString();
embed = new Discord.RichEmbed()
.setDescription(`No item with the provided name found\n\nID: \`${id}\`\n\nType \`+buy;[id]\` to buy the item.`)
.setTitle(db_url)
.setURL(db_url)
.setAuthor(`${message.author.username} sells ${item} for ${price} Gil`);
message.channel.send({
embed: embed
});
// insert to DB
sql.run('INSERT INTO offers (item, price, quality, id, sold) VALUES (?, ?, ?, ?, ?)', [item, price, quality, id, 0]);
sql.run('INSERT INTO userHasOffers (userId, offerID) VALUES (?, ?)', [message.author.id, id]);
}
});
} else
// accept offer
if (message.content.startsWith(settings.prefix + 'buy')) {
let id;
// stop processing command if not enough arguments
if (args.length != 1) {
message.channel.send('Info! Accepting an offer works as follows: \`+buy;[id]\`');
return;
}
// check if first argument is a number
if (!isNaN(args[0])) {
id = parseInt(args[0]);
} else {
message.channel.send(`Error! Please provide a number`);
return;
}
sql.get(`SELECT * FROM offers WHERE id = ${id}`).then(row => {
if (row.sold == 1) {
message.channel.send('This offer has already been bought.');
return;
}
let embed = new Discord.RichEmbed().setDescription(`${message.author.username} wants to buy ${row.item} in ${row.quality} for ${row.price} Gil`);
message.channel.send({
embed: embed
});
});
sql.run(`UPDATE offers SET sold = 1 WHERE id = ${id}`);
} else
// list all requests or sale offers
if (message.content.startsWith(settings.prefix + 'list')) {
let list = ``;
// stop processing command if not enough arguments
if (args.length != 1) {
message.channel.send('Info! Getting a list of requests or sale offers works as follows: \`+list;requests\` or \`+list;offers\`');
return;
}
if (args[0] === 'requests') {
sql.each('SELECT * FROM requests, users, userHasRequests WHERE accepted = 0 AND users.userId = userHasRequests.userId AND userHasRequests.requestId = requests.id', (err, row) => {
if (!err) list += `\`${row.id}\` \`x${row.amount}\` \`${row.item}\` \`by ${row.name}\`\n`;
}).then(t => {
if (list !== "") message.channel.send(list);
else message.channel.send('No requests are currently listed');
});
}
if (args[0] === 'offers') {
sql.each('SELECT * FROM offers, users, userHasOffers WHERE sold = 0 AND users.userId = userHasOffers.userId AND userHasOffers.offerId = offers.id', (err, row) => {
if (!err) list += `\`${row.id}\` \`${row.quality}\` \`${row.price} Gil\` \`${row.item}\` \`by ${row.name}\`\n`;
}).then(t => {
if (list !== "") message.channel.send(list);
else message.channel.send('No offers are currently listed');
});
}
}
// help command
if (message.content.startsWith(settings.prefix + 'help')) {
message.channel.send({
embed: {
"title": "Available Commands",
"fields": [{
"name": "+request;[item];[count]",
"value": "Use this command if you need items gathered from someone.\nThe item name has to be provided in English so that the automatic search will work."
},
{
"name": "+accept;[id]",
"value": "Use this command to accept a posted request."
},
{
"name": "+offer;[item];[price];[quality]",
"value": "Use this command to offer an item for sale.\nThe item name has to be provided in English so that the automatic search will work.\nQuality has to be provided as either NQ or HQ."
},
{
"name": "+buy;[id]",
"value": "Use this command if you want to buy an item from a posted offer."
},
{
"name": "+list;[requests|offers]",
"value": "Use this command to either list all available requests or offers."
},
{
"name": "Github",
"value": "https://github.com/Tywele/TheCraft-Discord-Bot"
}
]
}
});
}
});
// login the bot
client.login(settings.token);