Skip to content

Commit 28039b8

Browse files
committed
Update cmds/wiki.js
1 parent 6c6b5a7 commit 28039b8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

cmds/wiki.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const axios = require('axios');
2+
3+
async function wiki(event, api) {
4+
if (event.body.includes('-help')) {
5+
const usage = "Usage: wiki <word>\n\n" +
6+
"Description: Get a summary and information about a topic from Wikipedia.\n\n" +
7+
"Example: wiki OpenAI";
8+
api.sendMessage(usage, event.threadID);
9+
return Promise.resolve();
10+
}
11+
12+
const data = event.body.split(' ');
13+
if (data.length < 2) {
14+
api.sendMessage('Invalid Use Of Command!\nUsage: Wiki <word>', event.threadID);
15+
} else {
16+
try {
17+
data.shift();
18+
let txtWiki = '';
19+
const res = await getWiki(data.join(' '));
20+
if (res === undefined || res.title === undefined) {
21+
throw new Error(`API RETURNED THIS: ${res}`);
22+
}
23+
txtWiki += `🔎 You searched for the word '${res.title}' \n\n TimeStamp: ${res.timestamp}\n\n Description: ${res.description}\n\n Info: ${res.extract}\n\nSource: https://en.wikipedia.org`;
24+
api.sendMessage(txtWiki, event.threadID, event.messageID);
25+
} catch (err) {
26+
api.sendMessage(err.message, event.threadID, event.messageID);
27+
}
28+
}
29+
}
30+
31+
async function getWiki(q) {
32+
try {
33+
const response = await axios.get(`https://en.wikipedia.org/api/rest_v1/page/summary/${q}`);
34+
return response.data;
35+
} catch (error) {
36+
return error;
37+
}
38+
}
39+
40+
module.exports = wiki;

0 commit comments

Comments
 (0)