covidstats

package
v1.31.14 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &commands.YAGCommand{
	CmdCategory:         commands.CategoryTool,
	Name:                "CoronaStatistics",
	Aliases:             []string{"coronastats", "cstats", "cst"},
	Description:         "Shows COVID-19 statistics sourcing Worldometer statistics. Location is country name or their ISO2/3 shorthand.\nIf nothing is added, shows World's total.\nListings are sorted by count of total cases not deaths.",
	RunInDM:             true,
	DefaultEnabled:      true,
	SlashCommandEnabled: true,
	Arguments: []*dcmd.ArgDef{
		{Name: "Location", Type: dcmd.String},
	},
	ArgSwitches: []*dcmd.ArgDef{
		{Name: "countries", Help: "Countries of the World"},
		{Name: "continents", Help: "The Continents of the World"},
		{Name: "states", Help: "A State name in USA"},
		{Name: "page", Help: "Go to page number, works for paginated output", Type: dcmd.Int},
		{Name: "1d", Help: "Stats from yesterday"},
		{Name: "2d", Help: "Stats from the day before yesterday (does not apply to states)"},
	},
	RunFunc: func(data *dcmd.Data) (interface{}, error) {

		var cStats coronaWorldWideStruct
		var cConts []coronaWorldWideStruct
		var queryType = typeWorld
		var whatDay = "current day"
		var yesterday = "false"
		var twoDaysAgo = "false"
		var where, queryURL string
		var pagination = true
		var pageInit = 1

		if data.Switches["countries"].Value != nil && data.Switches["countries"].Value.(bool) {
			queryType = typeCountries

		} else if data.Switches["continents"].Value != nil && data.Switches["continents"].Value.(bool) {
			queryType = typeContinents

		} else if data.Switches["states"].Value != nil && data.Switches["states"].Value.(bool) {
			queryType = typeStates

		}

		if data.Switches["1d"].Value != nil && data.Switches["1d"].Value.(bool) {
			whatDay = "yesterday"
			yesterday = "true"
		} else if data.Switches["2d"].Value != nil && data.Switches["2d"].Value.(bool) {
			whatDay = "day before yesterday"
			twoDaysAgo = "true"
			if queryType == typeStates {
				yesterday = "true"
				twoDaysAgo = "false"
			}
		}

		queryURL = fmt.Sprintf("%s%s/%s", diseaseAPIHost, queryType, "?yesterday="+yesterday+"&twoDaysAgo="+twoDaysAgo+"&strict=true")
		if data.Args[0].Str() != "" {
			if queryType == typeWorld {
				queryType = typeCountries
			}
			where = data.Args[0].Str()

			queryURL = fmt.Sprintf("%s%s/%s", diseaseAPIHost, queryType, where+"?yesterday="+yesterday+"&twoDaysAgo="+twoDaysAgo+"&strict=true")
		}

		body, err := getData(queryURL, where, queryType)
		if err != nil {
			return nil, err
		}

		jsonDetector := bytes.TrimLeft(body, " \t\r\n")
		mapYes := len(jsonDetector) > 0 && jsonDetector[0] == '['
		mapNo := len(jsonDetector) > 0 && jsonDetector[0] == '{'
		if mapYes {
			queryErr := json.Unmarshal([]byte(body), &cConts)
			if queryErr != nil {
				return nil, queryErr
			}
		} else if mapNo {
			queryErr := json.Unmarshal([]byte(body), &cStats)
			if queryErr != nil {
				return nil, queryErr
			}
		}

		cConts = append(cConts, cStats)

		p := data.Switch("page")
		if p.Value != nil {
			pageInit = int(p.Value.(int64))
		}

		if pageInit > len(cConts)-1 {
			pageInit = 1
		}

		sort.SliceStable(cConts, func(i, j int) bool {
			return cConts[i].Cases > cConts[j].Cases
		})

		var embed = &discordgo.MessageEmbed{}
		embed = embedCreator(cConts, queryType, whatDay, 0)

		if queryType == typeWorld || data.Args[0].Str() != "" {
			pagination = false
		}

		if pagination {
			_, err := paginatedmessages.CreatePaginatedMessage(
				data.GuildData.GS.ID, data.ChannelID, pageInit, len(cConts)-1, func(p *paginatedmessages.PaginatedMessage, page int) (*discordgo.MessageEmbed, error) {
					embed = embedCreator(cConts, queryType, whatDay, page-1)
					return embed, nil
				})
			if err != nil {
				return "Something went wrong", nil
			}
		} else {
			return embed, nil
		}

		return nil, nil
	},
}

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