-
Notifications
You must be signed in to change notification settings - Fork 902
/
dictionary.go
235 lines (202 loc) · 6.5 KB
/
dictionary.go
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
package dictionary
import (
"encoding/json"
"fmt"
"html"
"io"
"net/http"
"net/url"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/botlabs-gg/yagpdb/v2/bot/paginatedmessages"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/lib/dcmd"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/microcosm-cc/bluemonday"
"github.com/sirupsen/logrus"
)
var Command = &commands.YAGCommand{
CmdCategory: commands.CategoryFun,
Name: "dictionary",
Aliases: []string{"owldict", "owl", "dict"},
Description: "Get the definition of an English word using dictionaryapi.dev",
RequiredArgs: 1,
Cooldown: 5,
Arguments: []*dcmd.ArgDef{
{Name: "Query", Help: "Word to search for", Type: dcmd.String},
},
DefaultEnabled: true,
SlashCommandEnabled: true,
RunFunc: func(data *dcmd.Data) (interface{}, error) {
query := strings.ToLower(data.Args[0].Str())
url := "https://api.dictionaryapi.dev/api/v2/entries/en/"+url.QueryEscape(query)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode == 404 {
return "Could not find a definition for that word.", nil
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var res []DictionaryResponse
err = json.Unmarshal(body, &res)
if err != nil || len(res[0].Meanings) == 0 {
logrus.WithError(err).Error("Failed getting response from dictionarydev")
return "Could not find a definition for that word.", err
}
var dictionary = &res[0]
if len(dictionary.Meanings) == 1 || data.Context().Value(paginatedmessages.CtxKeyNoPagination) != nil {
return createDictionaryDefinitionEmbed(dictionary, &dictionary.Meanings[0]), nil
}
_, err = paginatedmessages.CreatePaginatedMessage(data.GuildData.GS.ID, data.ChannelID, 1, len(dictionary.Meanings), func(p *paginatedmessages.PaginatedMessage, page int) (*discordgo.MessageEmbed, error) {
if page > len(dictionary.Meanings) {
return nil, paginatedmessages.ErrNoResults
}
return createDictionaryDefinitionEmbed(dictionary, &dictionary.Meanings[page-1]), nil
})
return nil, err
},
}
func createDictionaryDefinitionEmbed(res *DictionaryResponse, def *Meaning) *discordgo.MessageEmbed {
title := strings.Title(normalizeOutput(res.Word))
embed := &discordgo.MessageEmbed{
Title: title,
Description: common.CutStringShort(capitalizeSentences(normalizeOutput(def.Definitions[0].Definition)), 2048),
Color: 0x07AB99,
Timestamp: time.Now().Format(time.RFC3339),
}
if(len(res.SourceUrls) > 0) {
embed.URL = res.SourceUrls[0];
}
var description = "";
for _, d := range def.Definitions {
if(len(description) + len(d.Definition) + len(d.Example) > 2000) {
// if all definitions along with examples cannot be fit into the description, skip remaining definitions.
break;
}
description = fmt.Sprintf("%s\n- %s", description, capitalizeSentences(normalizeOutput(d.Definition)));
if d.Example != ""{
var example = capitalizeSentences(normalizeOutput(d.Example))
if !hasEndOfSentenceSymbol(example) {
example = example + "." // add period if no other symbol that ends the sentence is present
}
description = fmt.Sprintf("%s\n**Example:** *%s*", description, example)
}
}
embed.Description = common.CutStringShort(description, 2048);
if res.Origin != "" {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "Origin",
Value: normalizeOutput(res.Origin),
Inline: true,
})
}
if len(res.Phonetics) != 0 {
var pronunciation = &discordgo.MessageEmbedField{
Name: "Pronunciation",
Value: "",
Inline: true,
}
for _, v := range res.Phonetics {
if(v.Audio != ""){
if(v.Text == ""){
v.Text = res.Word;
}
pronunciation.Value = fmt.Sprintf("%s\n🔊[%s](%s)", pronunciation.Value, normalizeOutput(v.Text), v.Audio)
}else {
pronunciation.Value = fmt.Sprintf("%s\n%s", pronunciation.Value, normalizeOutput(v.Text))
}
}
embed.Fields = append(embed.Fields, pronunciation )
}
if def.PartOfSpeech != "" {
embed.Fields = append(embed.Fields, &discordgo.MessageEmbedField{
Name: "Type",
Value: strings.Title(normalizeOutput(def.PartOfSpeech)),
Inline: true,
})
}
return embed
}
var policy = bluemonday.StrictPolicy()
func normalizeOutput(s string) string {
// The API occasionally returns HTML tags and escapes as part of output, remove them.
decoded := html.UnescapeString(policy.Sanitize(s))
// It also sometimes returns non-printable characters, strip them out too.
return strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) {
return r
}
return -1
}, decoded)
}
func capitalizeSentences(s string) string {
var builder strings.Builder
capitalizeCur := true // whether the current phrase should be capitalized.
for i, word := range strings.Fields(s) {
if i > 0 {
builder.WriteByte(' ')
}
if capitalizeCur {
// strings.Title() does not work properly with punctuation: for example, "what's" becomes 'What'S" when passed to it, which is undesirable.
// Instead, title-case the first rune manually and write the rest as is, as we know `word` represents a single word.
r, size := utf8.DecodeRuneInString(word)
if r == utf8.RuneError {
// fall back to original text
builder.WriteString(word)
} else {
builder.WriteRune(unicode.ToTitle(r))
builder.WriteString(word[size:])
}
} else {
builder.WriteString(word)
}
capitalizeCur = hasEndOfSentenceSymbol(word)
}
return builder.String()
}
func hasEndOfSentenceSymbol(s string) bool {
if len(s) == 0 {
return false
}
switch s[len(s)-1] {
case '.', '?', '!':
return true
default:
return false
}
}
type Phonetic struct {
Text string `json:"text"`
Audio string `json:"audio"`
}
type Definition struct {
Definition string `json:"definition"`
Synonyms []string `json:"synonyms"`
Antonyms []string `json:"antonyms"`
Example string `json:"example,omitempty"`
}
type Meaning struct {
PartOfSpeech string `json:"partOfSpeech"`
Definitions []Definition `json:"definitions"`
Synonyms []string `json:"synonyms"`
Antonyms []string `json:"antonyms"`
}
type DictionaryResponse struct {
Origin string `json:"origin,omitempty"`
Word string `json:"word"`
Phonetics []Phonetic `json:"phonetics"`
Meanings []Meaning `json:"meanings"`
SourceUrls []string `json:"sourceUrls"`
}