wolframalpha

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.CategoryFun,
	Name:        "WolframAlpha",
	Aliases:     []string{"wolfram", "wa"},
	Description: `Queries the API of WolframAlpha for results on ...anything!
					Results are given in metric system, link below would use user's local unit-system.

					Needs user created AppID for WolframAlpha.
					To setup a WolframAlpha appID, you must register a Wolfram ID and sign in to the Wolfram|Alpha Developer Portal > https://developer.wolframalpha.com/portal/
					Upon logging in, go to the *My Apps* tab to start creating your first app. 
					
					This free access gives for up to **2 000** non-commercial API calls per month.`,
	RequiredArgs: 1,
	Arguments: []*dcmd.ArgDef{
		{Name: "Expression", Type: dcmd.String},
	},
	ArgSwitches: []*dcmd.ArgDef{
		{Name: "appid", Help: "Add your Wolfram|Alpha appID case sensitive"},
	},

	RunFunc: func(data *dcmd.Data) (interface{}, error) {
		var directURL = "https://www.wolframalpha.com/input/?i="

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

			targetID := data.Author.ID
			target, _ := bot.GetMember(data.GuildData.GS.ID, targetID)

			if isAdmin, _ := data.GuildData.GS.GetMemberPermissions(data.GuildData.CS.ID, data.Author.ID, target.Member.Roles); isAdmin&discordgo.PermissionAdministrator != 0 {
				appID := data.Args[0].Str()
				if len(appID) < 8 || len(appID) > 25 {
					return "appID is too short or too long", nil
				}
				err := common.RedisPool.Do(radix.Cmd(nil, "SET", "wolfram_appID:"+strconv.FormatInt(data.GuildData.GS.ID, 10), appID))
				if err != nil {
					return "", err
				}
				return fmt.Sprintln("Wolfram|Alpha appID added"), nil
			} else {
				return "Only a Guild Admin can add appID", nil
			}
		}

		var appID string
		err := common.RedisPool.Do(radix.Cmd(&appID, "GET", "wolfram_appID:"+strconv.FormatInt(data.GuildData.GS.ID, 10)))
		if err != nil {
			return "No Wolfram|Alpha appID", nil
		}

		input := url.QueryEscape(data.Args[0].Str())
		response := "```\n"
		responseTooLong := "\n\n(response too long)"
		responseEnd := "\n```<" + directURL + input + ">"

		query, err := requestWolframAPI(input, appID)
		if err != nil {
			return "", err
		}

		if len(query) > 2000 {
			query = common.CutStringShort(query, 1980-len(responseTooLong+responseEnd)) + responseTooLong
		}

		response += query + responseEnd
		return response, nil
	},
}

Functions

This section is empty.

Types

type WolframAlpha

type WolframAlpha struct {
	Queryresult struct {
		XMLName       xml.Name `xml:"queryresult"`
		Text          string   `xml:",chardata"`
		Success       string   `xml:"success,attr"`
		AttrError     string   `xml:"error,attr"`
		Numpods       string   `xml:"numpods,attr"`
		Datatypes     string   `xml:"datatypes,attr"`
		Timedout      string   `xml:"timedout,attr"`
		Timedoutpods  string   `xml:"timedoutpods,attr"`
		Timing        string   `xml:"timing,attr"`
		Parsetiming   string   `xml:"parsetiming,attr"`
		Parsetimedout string   `xml:"parsetimedout,attr"`
		Recalculate   string   `xml:"recalculate,attr"`
		ID            string   `xml:"id,attr"`
		Host          string   `xml:"host,attr"`
		Server        string   `xml:"server,attr"`
		Related       string   `xml:"related,attr"`
		Version       string   `xml:"version,attr"`
		Pod           []struct {
			Text       string `xml:",chardata"`
			Title      string `xml:"title,attr"`
			Scanner    string `xml:"scanner,attr"`
			ID         string `xml:"id,attr"`
			Position   string `xml:"position,attr"`
			Error      string `xml:"error,attr"`
			Numsubpods string `xml:"numsubpods,attr"`
			Primary    string `xml:"primary,attr"`
			Subpod     []struct {
				Text         string `xml:",chardata"`
				Title        string `xml:"title,attr"`
				Plaintext    string `xml:"plaintext"`
				Microsources struct {
					Text        string `xml:",chardata"`
					Microsource string `xml:"microsource"`
				} `xml:"microsources"`
			} `xml:"subpod"`
			Expressiontypes struct {
				Text           string `xml:",chardata"`
				Count          string `xml:"count,attr"`
				Expressiontype struct {
					Text string `xml:",chardata"`
					Name string `xml:"name,attr"`
				} `xml:"expressiontype"`
			} `xml:"expressiontypes"`
			States struct {
				Text  string `xml:",chardata"`
				Count string `xml:"count,attr"`
				State []struct {
					Text  string `xml:",chardata"`
					Name  string `xml:"name,attr"`
					Input string `xml:"input,attr"`
				} `xml:"state"`
			} `xml:"states"`
			Infos struct {
				Text  string `xml:",chardata"`
				Count string `xml:"count,attr"`
				Info  []struct {
					Text  string `xml:",chardata"`
					Units struct {
						Text  string `xml:",chardata"`
						Count string `xml:"count,attr"`
						Unit  []struct {
							Text  string `xml:",chardata"`
							Short string `xml:"short,attr"`
							Long  string `xml:"long,attr"`
						} `xml:"unit"`
					} `xml:"units"`
					Link struct {
						Text     string `xml:",chardata"`
						URL      string `xml:"url,attr"`
						AttrText string `xml:"text,attr"`
					} `xml:"link"`
				} `xml:"info"`
			} `xml:"infos"`
		} `xml:"pod"`
		Assumptions struct {
			Text       string `xml:",chardata"`
			Count      string `xml:"count,attr"`
			Assumption struct {
				Text     string `xml:",chardata"`
				Type     string `xml:"type,attr"`
				Template string `xml:"template,attr"`
				Count    string `xml:"count,attr"`
				Value    []struct {
					Text  string `xml:",chardata"`
					Name  string `xml:"name,attr"`
					Desc  string `xml:"desc,attr"`
					Input string `xml:"input,attr"`
				} `xml:"value"`
			} `xml:"assumption"`
		} `xml:"assumptions"`
		Userinfoused struct {
			Text     string `xml:",chardata"`
			Count    string `xml:"count,attr"`
			Userinfo struct {
				Text string `xml:",chardata"`
				Name string `xml:"name,attr"`
			} `xml:"userinfo"`
		} `xml:"userinfoused"`
		Sources struct {
			Text   string `xml:",chardata"`
			Count  string `xml:"count,attr"`
			Source []struct {
				Text     string `xml:",chardata"`
				URL      string `xml:"url,attr"`
				AttrText string `xml:"text,attr"`
			} `xml:"source"`
		} `xml:"sources"`
		Error struct {
			Text string `xml:",chardata"`
			Code string `xml:"code"`
			Msg  string `xml:"msg"`
		} `xml:"error"`
	}
}

Jump to

Keyboard shortcuts

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