dictionary

package
v2.33.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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
	},
}

Functions

This section is empty.

Types

type Definition

type Definition struct {
	Definition string   `json:"definition"`
	Synonyms   []string `json:"synonyms"`
	Antonyms   []string `json:"antonyms"`
	Example    string   `json:"example,omitempty"`
}

type DictionaryResponse

type DictionaryResponse struct {
	Origin     string     `json:"origin,omitempty"`
	Word       string     `json:"word"`
	Phonetics  []Phonetic `json:"phonetics"`
	Meanings   []Meaning  `json:"meanings"`
	SourceUrls []string   `json:"sourceUrls"`
}

type Meaning

type Meaning struct {
	PartOfSpeech string       `json:"partOfSpeech"`
	Definitions  []Definition `json:"definitions"`
	Synonyms     []string     `json:"synonyms"`
	Antonyms     []string     `json:"antonyms"`
}

type Phonetic

type Phonetic struct {
	Text  string `json:"text"`
	Audio string `json:"audio"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL