-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
262 lines (232 loc) · 8.2 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
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
// set up dependencies
const Discord = require("discord.js");
const fetch = require("node-fetch");
// set up discord client
const { token } = require("./config.json");
const client = new Discord.Client();
const prefix = "~~";
// ready prompt
client.once("ready", () => {
console.log("Ready.. with prefix " + prefix);
});
// login using bot token
// client.login(process.env.BOT_TOKEN);
client.login(token);
// CONSTANTS
const WIKI_SEARCH = `${prefix}wikisearch`;
const WIKI_RANDOM = `${prefix}wikirandom`;
const WIKI_OTD = `${prefix}wikiotd`;
const WIKI_HELP = `${prefix}help`;
const MAX_CHARS = 500;
const WIKI_CLIENT_DOMAIN = "https://en.wikipedia.org/wiki/";
const WIKI_CLIENT_OTD = "https://en.wikipedia.org/wiki/Wikipedia:On_this_day/Today";
// client messages
client.on("message", async (message) => {
if (!message.author.bot) {
// avoid schizoid bots
// select action according to user input
if (message.content.startsWith(WIKI_SEARCH)) {
await wikiSearch(message);
} else if (message.content.startsWith(WIKI_RANDOM)) {
await wikiRandom(message);
} else if (message.content.startsWith(WIKI_OTD)) {
await wikiOTD(message);
} else if (message.content.startsWith(WIKI_HELP)) {
wikiHelp(message);
} else if (message.content.startsWith(prefix)) {
message.channel.send(`Command not found please use ${WIKI_HELP}`);
}
}
});
function wikiHelp(message) {
// TODO maybe later retrieve help info from another website?
createHelpEmbed(message.channel);
// create and send a help message to the origin channel
function createHelpEmbed(channel) {
const helpEmbed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle("WikiBOT")
.setURL("https://github.com/oguztuzgen/wikibot")
.setDescription(
"WikiBOT is an under-development Wikipedia query tool for Discord servers."
)
.addFields(
{
name: "Searching Wikipedia",
value: `\`\`\`${WIKI_SEARCH} [item]\`\`\``,
},
{
name: "Get a random article from Wikipedia",
value: `\`\`\`${WIKI_RANDOM}\`\`\``,
},
{
name: "Get today's events, births, deaths and holdiays",
value: `\`\`\`${WIKI_OTD}\`\`\``, // TODO make events births etc options
}
)
.setTimestamp();
channel.send(helpEmbed);
}
}
// functions
async function wikiSearch(message) {
// parse message
var toSearch = message.content.substr(WIKI_SEARCH.length + 1);
if (toSearch.length > 0) {
// output prompt
message.channel.send(
`\`\`\`Searching \"${toSearch}\" on Wikipedia...\`\`\``
);
var wikiSearchResults = await requestData(getWikiSearchString(toSearch));
if (wikiSearchResults.hasOwnProperty("query")) {
let pages = wikiSearchResults.query.pages;
// create and send embed message
createSearchEmbed(
pages[Object.keys(pages)[0]].title,
pages,
encodeURI(WIKI_CLIENT_DOMAIN + toSearch),
message.channel
);
} else {
message.channel.send("Could not found that...");
}
} else {
message.channel.send(`I can't search for nothing you dumbfuck`);
}
// create a search embed including the title and the link of the searched article
// also send two of the closest matches
function createSearchEmbed(title, pages, link, channel) {
const searchEmbed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle(title)
.setURL(link)
.setDescription(pages[Object.keys(pages)[0]].extract)
.addFields(
{
name: `I found ${pages[Object.keys(pages)[0]].title}`,
value: "But did you mean:",
inline: false,
},
{
name: pages[Object.keys(pages)[1]].title,
value: // embedded link
`[${pages[Object.keys(pages)[1]].title}](${encodeURI(
WIKI_CLIENT_DOMAIN + pages[Object.keys(pages)[1]].title
)})\n` + pages[Object.keys(pages)[1]].extract,
inline: true,
},
{
name: pages[Object.keys(pages)[2]].title,
value:
`[${pages[Object.keys(pages)[2]].title}](${encodeURI(
WIKI_CLIENT_DOMAIN + pages[Object.keys(pages)[2]].title
)})\n` + pages[Object.keys(pages)[2]].extract,
inline: true,
}
)
.setImage(
pages[Object.keys(pages)[0]].hasOwnProperty("thumbnail")
? pages[Object.keys(pages)[0]].thumbnail.source
: "https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png"
)
.setTimestamp();
channel.send(searchEmbed);
}
}
async function wikiRandom(message) {
const randomURL =
"https://en.wikipedia.org/w/api.php?format=json&action=query&generator=random&grnnamespace=0&prop=pageimages|extracts&exchars=500&exintro&explaintext&exlimit=max&format=json&origin=*";
var result = await requestData(encodeURI(randomURL));
if (result.hasOwnProperty("query")) {
let pages = result.query.pages;
let firstPage = pages[Object.keys(pages)[0]];
createRandomEmbed(
firstPage.title,
pages,
encodeURI(WIKI_CLIENT_DOMAIN + firstPage.title),
message.channel
);
} else {
console.log("ERROR: result has no property 'query'");
}
function createRandomEmbed(title, pages, link, channel) {
const randomEmbed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle(title)
.setURL(link)
.setDescription(pages[Object.keys(pages)[0]].extract)
.setImage(
pages[Object.keys(pages)[0]].hasOwnProperty("thumbnail")
? pages[Object.keys(pages)[0]].thumbnail.source
: "https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png"
)
.setTimestamp();
channel.send(randomEmbed);
}
}
async function wikiOTD(message) {
var today = new Date();
const otdURL = `https://en.wikipedia.org/api/rest_v1/feed/onthisday/all/${('0' + (today.getMonth() + 1)).slice(-2)}/${('0' + today.getDate()).slice(-2)}`;
var result = await requestData(encodeURI(otdURL));
// this API does not have a 'query' field so the check is done via selected
if (result.hasOwnProperty("selected")) {
var pagesList = [
result.selected[Object.keys(result.selected)[0]].pages,
result.births[Object.keys(result.births)[0]].pages,
result.deaths[Object.keys(result.deaths)[0]].pages,
result.events[Object.keys(result.events)[0]].pages,
result.holidays[Object.keys(result.holidays)[0]].pages];
var items = [];
var index = 0;
pagesList.forEach(pages => {
items.push({
name: Object.keys(result)[index][0].toUpperCase()+Object.keys(result)[index].substr(1),
value: `[${getFirstPageTitle(pages)}](${getFirstPageWebsiteURL(pages)})\n` + getFirstPageExtract(pages),
inline: false
});
index++;
});
function getFirstPageWebsiteURL(pages) {
return pages[Object.keys(pages)[0]].content_urls.desktop.page;
}
function getFirstPageTitle(pages) {
return pages[Object.keys(pages)[0]].displaytitle;
}
function getFirstPageExtract(pages) {
return pages[Object.keys(pages)[0]].extract;
}
createOTDEmbed(
"On This Day In Wikipedia",
message.channel
);
} else {
console.log("ERROR: result has no property 'query'");
}
function createOTDEmbed(title , channel) {
const OTDEmbed = new Discord.MessageEmbed()
.setColor("#0099ff")
.setTitle(title)
.setURL(WIKI_CLIENT_OTD)
.setDescription(new Date().toDateString())
.addFields(items)
.setTimestamp();
channel.send(OTDEmbed);
}
}
function getWikiSearchString(searchTerm) {
// append the search term and maximum char lenght to the api url
var rawSearchString = `https://en.wikipedia.org/w/api.php?action=query&generator=search&gsrsearch=${searchTerm}&gsrlimit=20&prop=pageimages|extracts&exchars=${MAX_CHARS}&exintro&explaintext&exlimit=max&format=json&origin=*`;
const searchString = encodeURI(rawSearchString);
return searchString;
}
async function requestData(searchString) {
try {
// get the json from the api
const response = await fetch(searchString);
// convert to json
const data = await response.json();
return data;
} catch (err) {
console.error(err);
}
}