· 5 years ago · Feb 06, 2021, 03:20 PM
1 // If the command that was sent matches any of the commands that we have configured...
2 if (command === "define") {
3 if (!args.length) {
4 return channel.send(
5 `Uh oh, you didn't provide a word to define, <@${author.id}>!`
6 );
7 }
8//go here and ge the api https://www.dictionaryapi.com/products/api-collegiate-dictionary
9 axios
10 .get(
11 `https://dictionaryapi.com/api/v3/references/collegiate/json/${args[0]}`,
12 {
13 params: {
14 key: "MERRIAM-WEBSTER-KEY",
15 },
16 }
17 )
18 .then(function (response) {
19 let data = response.data[0];
20
21 // If the result is a list of strings, it's not a valid word.
22 if (typeof data !== "object") {
23 const result = new Discord.MessageEmbed()
24 .setTitle(
25 `Invalid word: The word you have entered isnt in the dictionary.`
26 )
27 .setColor(0xff0000);
28 if (!response.data || response.data.length === 0)
29 result.setDescription("");
30 else
31 result.setDescription(
32 "Maybe try one of these suggestions: \n" +
33 arrayToSentence(response.data)
34 );
35 return channel.send(result);
36 }
37
38 let definitions = data.shortdef,
39 type = data.fl,
40 description = "";
41 for (let i = 0; i < definitions.length; i++) {
42 description += `[${i + 1}] ${definitions[i]}`;
43 description += i == definitions.length - 1 ? "" : "\n";
44 }
45
46 let audio = data.hwi.prs[0].sound.audio;
47 let subdirectory = "";
48
49 // @see https://dictionaryapi.com/products/json#sec-2.prs for documenation regarding audio URL's
50 if (audio.startsWith("bix")) subdirectory = "bix";
51 else if (audio.startsWith("gg")) subdirectory = "gg";
52 else if (!!audio.match(/^[.,:!?]/) || !!audio.match(/^[0-9]/))
53 subdirectory = "number";
54 else subdirectory = audio[0];
55
56 const attachment = new Discord.MessageAttachment(
57 `https://media.merriam-webster.com/audio/prons/en/us/mp3/${subdirectory}/${audio}.mp3`
58 );
59
60 channel.send(
61 new Discord.MessageEmbed()
62 .setTitle(`Definition: ${args[0]} (${type})`)
63 .setDescription(`**Pronunciation:** [${data.hwi.prs[0].mw}]:`)
64 .setColor(0x1af200)
65 .setDescription(description)
66 .setFooter(`Requested by ${message.author.username}`)
67 );
68
69 channel.send(
70 attachment
71 );
72 })
73 .catch(function (error) {
74 console.log(error);
75 return channel.send(
76 `Sorry, I can't define that at the moment <@${author.id}>!`
77 );
78 });