-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (118 loc) · 4.23 KB
/
index.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
/*Luca Comba*/
// configuration
//const { prefix, token, news_api_key } = require('./config.json');
const prefix = "?";
// require the discord.js module
const Discord = require('discord.js');
// news api
//const NewsAPI = require('newsapi');
//const newsapi = new NewsAPI(news_api_key);
// axios
const axios = require('axios')
// create a new Discord client
const client = new Discord.Client();
// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
console.log('Ready!');
});
/*HELP EMBEEMD MESSAGE*/
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#168032')
.setTitle('THE CODER DOCS !')
.setAuthor('Lukfd', 'https://image.flaticon.com/icons/svg/1660/1660165.svg', 'https://github.com/lukfd')
.setDescription('Here are the commands available')
.setThumbnail('https://image.flaticon.com/icons/svg/1660/1660165.svg')
.addFields(
{ name: '?help', value: 'return the docs', inline: false },
{ name: '? <YOUR CODING QUESTION>', value: 'return the first answer for your question from STACKOVERFLOW.com', inline: false },
{ name: '?NEWS', value: "return today's news", inline: false },
)
.setImage('https://image.flaticon.com/icons/svg/1660/1660165.svg')
.setTimestamp()
.setFooter('For issues get my GitHub');
/*HANDELING MESSAGES*/
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(' ');
const command = args.shift().toLowerCase();
// loading
if (command === 'help') {
message.channel.send(exampleEmbed);
} else if (command.length == 0) {
if (!args.length) {
return message.channel.send(`You didn't provide any arguments, ${message.author}!`);
} else {
// DO THE SEARCH
var question = message.content.slice(prefix.length);
// using stockexchange api
const url = 'https://api.stackexchange.com/2.2/search?order=desc&sort=relevance&intitle='+question+'&site=stackoverflow'
// calling api
axios.get(url).then(resp => {
// get question id
var questionId = resp.data.items[0].question_id;
const ansUrl = 'https://api.stackexchange.com/2.2/questions/'+questionId+'/answers?order=asc&sort=activity&site=stackoverflow'
axios.get(ansUrl).then(resp => {
const link = 'https://stackoverflow.com/a/'+resp.data.items[0].answer_id;
axios.get(link).then(resp => {
const cheerio = require('cheerio');
var $ = cheerio.load(resp.data);
var firstDiv = $('.answercell').html();
// update $
$ = cheerio.load(firstDiv)
var secondDiv = $('.s-prose').html();
// getting rid of paragraph tags
var p = /(<p>|<\/p>)/g;
// code to ```
var code = /(<code>|<\/code>)/g;
// other
var other = /(<em>|<\/em>|<b>|<\/b>|<i>|<\/i>)/g;
var result = secondDiv.replace(p,'').replace(code,"`").replace(other,'**');
// more polishing
var extraHtml = /(<([^>]+)>)/ig;
result = result.replace(extraHtml, '');
result = result + '\n\n\n' + link;
result = result.trim();
// check for code
if (result.length > 2000) {
var int = result.length/1993;
for (var i = 0; i < int; i++) {
var lessresult = result.substring(0,1993);
result = result.substring(1993);
message.channel.send(lessresult);
}
} else {
message.channel.send(result);
}
});
});
});
}
} /* else if (command === 'news') {
// RETURN TODAYS NEWS
newsapi.v2.topHeadlines({
language: 'en',
country: 'us',
category: 'general'
}).then(response => {
var result = response.articles
var toRet = '';
console.log(response.articles[0]);
for (var i = 0; i < result.length; i++) {
// should do this better!
// if (toRet.length > 1500) {
// message.channel.send(toRet);
// toRet = '';
// } else {
// // toRet = toRet + result[i].title + '\n'
// // + result[i].description + '\n'
// // + 'READ IT AT:' + result[i].url + '\n'
// toRet = toRet + result[i].url + '\n';
// }
message.channel.send(result[i].url);
}
});
} */
});
// login to Discord with your app's token
client.login(process.env.DISCORD_TOKEN);