Skip to content

Commit 0d14137

Browse files
authored
Merge pull request #16 from ap4gh/master
fixed a bug where bot responds with empty definition from wikipedia.
2 parents 1fbb9ce + a653d5d commit 0d14137

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

commands/define.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const maintainerID = '274434863711518722';
1212

1313
const request = require('request-promise-native');
1414

15-
const maxRelatedTopics = 3;
15+
const maxRelatedTopics = 4;
1616

1717
/*
1818
------------ HELPER FUNCTIONS ------------
@@ -175,9 +175,11 @@ const wikipediaOpenSearch = async (message, args) => {
175175
// remove useless definition at index 0:
176176
definitions.shift();
177177
// collect related definition:
178-
for (let d of definitions) {
179-
result += `${definitions.indexOf(d) + 1}. ${d}\n\n`;
180-
if (definitions.indexOf(d) === maxRelatedTopics - 1) break;
178+
let nonEmptyDefinitions = [];
179+
for (let d of definitions) if (d.length > 0) nonEmptyDefinitions.push(d);
180+
for (let i = 0; i < maxRelatedTopics; ++i) {
181+
if (nonEmptyDefinitions[i] == undefined) break;
182+
result += `${i + 1}. ${nonEmptyDefinitions[i]}\n\n`;
181183
}
182184
result += '```';
183185
} // exact meaning is obtained:
@@ -192,7 +194,9 @@ const wikipediaOpenSearch = async (message, args) => {
192194
* •• HELP COMMAND ••
193195
*/
194196
const showHelp = (message, args) => {
195-
sendMessage(message, `
197+
sendMessage(
198+
message,
199+
`
196200
\`\`\`
197201
NAME
198202
define -- provide definition for words from web.
@@ -226,7 +230,8 @@ GUIDE
226230
events, news etc. and does not provide meaning of the words from
227231
english dictionary. DDG bang redirects will also not work here.
228232
\`\`\`
229-
`);
233+
`
234+
);
230235
};
231236

232237
// run function for !define command:

0 commit comments

Comments
 (0)