pgo

package module
v0.0.0-...-91be95b Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: AGPL-3.0 Imports: 9 Imported by: 0

README

PGo

About

A giant PnW discord bot that I hope to finish someday.

Trivia

Most of the base code was written with Vim/Neovim on Termux, on an Android Phone.

Documentation

Overview

Package pgo provides a set of commands that access the Politics And War API for information. All commands take a slice of strings as arguments and returns a string as an output

This package is not meant to be used directly, but is instead meant to be used as a command line interface, which is located in pgo/cmd/pgo. A version that can be run as a discord bot is also availible in pgo/cmd/pgo-discord.

Index

Constants

This section is empty.

Variables

View Source
var API = "https://api.politicsandwar.com/graphql"

API is the API URL

View Source
var ErrorHandler func(error) string = func(err error) string {
	return err.Error()
}

ErrorHandler is the function for handling errors. It should take an error and output a string in the desired format.

View Source
var HelpTemplate = template.Must(template.New("help").Funcs(template.FuncMap{"usages": func(fs *flag.FlagSet) string { return fs.FlagUsages() }}).Parse(`{{ .Name }} - {{ .Description }}
ALIASES{{ range .Aliases }}
{{ . }}{{end}}
ARGUMENTS
{{ .FlagSet.FlagUsages }}`))

HelpTemplate is the template for help messages.

Key is the API key

View Source
var NoCommandHandler func(string) string = func(command string) string {
	return fmt.Sprintf("Command %s does not exist.", command)
}

NoCommandHandler is the function for handling commands that were requested but do not exist. It should take the requested command name as a string and output a string.

View Source
var WhoAllianceTemplate = template.Must(template.New("whoAlliance").Parse(`# __{{.Data.Name}}__ ({{.Data.Acronym}})
*https://politicsandwar.com/alliance/id={{.Data.ID}}*
## General Information
- Total score: *{{.Data.Score}}*
- *{{.Cities}}* cities in total.
- *{{.Members}}* members and *{{.Applicants}}* applicants.
- Color: **{{.Data.Color}}**
## Wars
- Involved in a total of *{{.TotalWars}}* wars.
- *{{.OffensiveWars}}* are offensive and *{{.DefensiveWars}}* are defensive.
- Raiding in *{{.OffensiveRaids}}* wars and are being raided in *{{.DefensiveRaids}}*.
- Winning roughly {{.WinningWarPercent}}% of their wars.
## Treaties{{with .Data.Treaties}}{{range .}}
- **{{printf "%-12s" .TreatyType}}**: {{$oneIsAlliance := eq .Alliance1.Name $.Data.Name}}{{if $oneIsAlliance}}*{{end}}*{{printf "%24s" .Alliance1.Name}}*{{if $oneIsAlliance}}*{{end}} --=#=-- {{if not $oneIsAlliance}}*{{end}}*{{.Alliance2.Name}}*{{if not $oneIsAlliance}}*{{end}}
{{end}}{{else}}
**NONE**{{end}}`))

Functions

func AddCommand

func AddCommand(command *Command)

AddCommand registers the given command, along with its aliases.

func DeleteCommand

func DeleteCommand(name string)

DeleteCommand deletes a command by name, and removes all aliases. Aliases do not work for the name.

func HelpSubCommand

func HelpSubCommand(command *Command) string

HelpSubCommand provides help for the provided subcommand using HelpTemplate.

func Parse

func Parse(args []string) string

Parse is the entrypoint for running pgo. It requires arguments in an os.Args-like style and outputs a string.

Types

type Command

type Command struct {
	// Name is the name of the subcommand.
	Name string
	// Action is the body of the subcommand.
	// It takes the provided arguments (excluding the subcommand name) and outputs the output string,
	// along with any error that may happen.
	Action func([]string) (string, error)
	// The subcommand's description, for use in help messages.
	Description string
	// Aliases for the subcommand. These should also work when running the command.
	Aliases []string
	flags.Flags
}

Command is a type for pgo's subcommands.

func GetCommand

func GetCommand(name string) (cmd *Command, ok bool)

GetCommand gets a command by provided name. If the command does not exist, ok is false. Aliases are also searchable.

Directories

Path Synopsis
cmd
pgo
internal
api

Jump to

Keyboard shortcuts

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