howlongtobeat

package
v1.31.24 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &commands.YAGCommand{
	CmdCategory:         commands.CategoryFun,
	Name:                "HowLongToBeat",
	Aliases:             []string{"hltb"},
	RequiredArgs:        1,
	Description:         "Game information based on query from howlongtobeat.com.\nResults are sorted by popularity, it's their default. Without -p returns the first result.\nSwitch -p gives paginated output using Levenshtein distance sorting max 20 results.",
	DefaultEnabled:      true,
	SlashCommandEnabled: true,
	Arguments: []*dcmd.ArgDef{
		{Name: "Game-Title", Type: dcmd.String},
	},
	ArgSwitches: []*dcmd.ArgDef{
		{Name: "c", Help: "Compact output"},
		{Name: "p", Help: "Paginated output"},
	},
	RunFunc: func(data *dcmd.Data) (interface{}, error) {
		var compactView, paginatedView bool
		gameName := data.Args[0].Str()

		if data.Switches["c"].Value != nil && data.Switches["c"].Value.(bool) {
			compactView = true
		}

		if data.Switches["p"].Value != nil && data.Switches["p"].Value.(bool) {
			compactView = false
			paginatedView = true
		}

		getData, err := getGameData(gameName)
		if err != nil {
			return nil, err
		}
		toReader := strings.NewReader(getData)

		hltbQuery, err := parseGameData(gameName, toReader)
		if err != nil {
			return nil, err
		}

		if hltbQuery[0].GameTitle == "" {
			return "No results", nil
		}

		if compactView {
			compactData := fmt.Sprintf("%s: %s | %s | %s | <%s>",
				normaliseTitle(hltbQuery[0].GameTitle),
				strings.Trim(fmt.Sprint(hltbQuery[0].MainStory), "[]"),
				strings.Trim(fmt.Sprint(hltbQuery[0].MainExtra), "[]"),
				strings.Trim(fmt.Sprint(hltbQuery[0].Completionist), "[]"),
				hltbQuery[0].GameURL)
			return compactData, nil
		}

		hltbEmbed := embedCreator(hltbQuery, 0, paginatedView)

		if paginatedView {
			_, err := paginatedmessages.CreatePaginatedMessage(
				data.GuildData.GS.ID, data.ChannelID, 1, len(hltbQuery), func(p *paginatedmessages.PaginatedMessage, page int) (*discordgo.MessageEmbed, error) {
					i := page - 1
					sort.SliceStable(hltbQuery, func(i, j int) bool {
						return hltbQuery[i].LevDistance < hltbQuery[j].LevDistance
					})
					paginatedEmbed := embedCreator(hltbQuery, i, paginatedView)
					return paginatedEmbed, nil
				})
			if err != nil {
				return "Something went wrong", nil
			}
		} else {
			return hltbEmbed, nil
		}

		return nil, nil
	},
}

Command var needs a comment for lint :)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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