-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
178 lines (154 loc) · 8.57 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
const Discord = require('discord.js');
const client = new Discord.Client();
const https = require('https');
const util = require('util');
var Fuse = require('fuse.js');
const talkedRecently = new Set();
var parseString = require('xml2js').parseString;
var prefix = '!gr ';
client.on('ready', () => {
//console.log(`Logged in as ${client.user.tag} with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
client.user.setPresence({
game: {
name: 'with books | !gr help',
},
status: 'online'
})
});
client.on('message', async msg => {
if (msg.author.bot) return;
if (msg.content === 'ping') {
msg.channel.send('kong!');
}
if (!msg.content.startsWith(prefix) || (msg.author.bot && (msg.author.id !== 521799384644714496))) return;
var grquery = msg.content.slice(prefix.length).trim();
if (grquery != "help" && grquery != "example" && grquery != "Aevan" && grquery != "ping") {
var uri = 'https://www.goodreads.com/search/index.xml?key=' + process.env.GRTOKEN + '&q=' + grquery;
https.get(uri, (res) => {
//Sorry Boson The invite Kick System Pls DM ME
//whats your ID , Mineknight#9386
//You Can Also Join The Server Back
const statusCode = res.statusCode;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
}
if (error) {
console.log(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => rawData += chunk);
res.on('end', () => {
try {
parseString(rawData, function(err, result) {
console.log(result)
var resultJSON = JSON.stringify(result);
var options = {
shouldSort: true,
tokenize: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 42,
minMatchCharLength: 2,
keys: [
"GoodreadsResponse.search.results.work.best_book.title",
"GoodreadsResponse.search.results.work.best_book.author.name"
// these are keys for fuzzy matching with fusejs.io
]
};
// the goodreads response is xml, so I am converting it to json from xml2js module
// and then I am using fuzzy search (fusejs.io) to match title and author names from JSON
// anyone active to help????
//use chat.txt file
console.log(resultJSON);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function monthNumToName(monthnum) {
return months[monthnum - 1] || '';
}
var firstTitle = result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].title[0];
var firstAuthor = result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].author[0].name[0];
var firstTitleLink = "https://goodreads.com/book/show/" + result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].id[0];
var firstAuthorLink = "https://goodreads.com/author/show/" + result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].author[0].id[0];
var month = result.GoodreadsResponse.search[0].results[0].work[0].original_publication_month[0]._
var month = monthNumToName(month);
var firstDate = month + " " + result.GoodreadsResponse.search[0].results[0].work[0].original_publication_year[0]._;
var firstRatings = result.GoodreadsResponse.search[0].results[0].work[0].ratings_count[0]._;
var firstReviews = result.GoodreadsResponse.search[0].results[0].work[0].text_reviews_count[0]._;
var firstRating = result.GoodreadsResponse.search[0].results[0].work[0].average_rating[0];
var firstCover = result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].image_url[0];
const replyEmbed = new Discord.RichEmbed()
.setColor(0x7DD121)
.setDescription("**[firstTitle](firstTitleLink)** by [firstAuthor](firstAuthorLink)", firstTitle, firstTitleLink, firstAuthor, firstAuthorLink)
.setImage(firstCover)
.addField("Published:", firstDate, true)
.addBlankField(true)
.addField("Rating:", firstRating, true)
.addField("No. of Reviews:", firstReviews, true)
.addBlankField(true)
.addField("No. of Ratings:", firstRatings, true);
msg.channel.send(replyEmbed);
var fuse = new Fuse(resultJSON, options);
var resultSort = fuse.search(grquery);
console.log(resultSort); //this is not showing any results
console.log(grquery);
console.log(result.GoodreadsResponse.search[0].results[0].work[0].best_book[0].title[0]);
});
} catch (e) {
console.log(e.message);
}
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
}
if (msg.content.startsWith(prefix + "ping")) {
msg.channel.send(`One-way Latency is ${Math.round(client.ping)}ms`);
}
if (msg.content.startsWith(prefix + "help")) {
const embed = new Discord.RichEmbed()
.setColor(0x7DD121)
.setDescription("**Search examples:**```\n!gr the fellowship of the ring``````\n!gr fellowship ring``````\n!gr fellowship ring tolkien``````\n!gr 9780618346257``````\n!gr example```\n\n")
.addField("Cannot search your book?", "\'Check for typos\' or \'refine your search\'")
.addField("Found a BUGGG!!! or missing an important feature?: ", "Mention or DM @Aevan")
msg.channel.send(embed);
};
if (talkedRecently.has(msg.author.id))
msg.reply('Speed Searcher');
talkedRecently.add(msg.author.id);
setTimeout(() => {
// Removes the user from the set after 2.0 seconds
talkedRecently.delete(msg.author.id);
}, 2000);
if (msg.content.startsWith(prefix + "eval")) {
if (msg.author.id !== process.env.ownerID)
return;
}
if (msg.content.startsWith(prefix + "example")) {
const embed = new Discord.RichEmbed()
.setColor(0x7DD121)
.setDescription("**[The Fellowship of the Ring (The Lord of the Rings, #1)](https://www.goodreads.com/book/show/34)** by [J.R.R. Tolkien](https://www.goodreads.com/author/show/656983)")
.setImage("https://images.gr-assets.com/books/1298411339m/34.jpg")
.addField("Published:", "9 Jul 1954", true)
.addBlankField(true)
.addField("Rating:", "4.35", true)
.addField("No. of Reviews:", "17637", true)
.addBlankField(true)
.addField("No. of Ratings:", "2028090", true);
msg.channel.send(embed);
}
if (msg.content == "!gr Aevan") { msg.channel.send('Master! We got a Follower. @Aevan'); }
if (msg.content == "!gr bug") {
msg.author.send("DM or mention @Aevan")
};
});
client.login(process.env.DISCORDTOKEN);
//client.on("error", (e) => console.error(e));
//client.on("warn", (e) => console.warn(e));
// client.on("debug", (e) => console.info(e)););