go-serverapi/ban.go

66 lines
1.4 KiB
Go

package serverapi
import (
"fmt"
"time"
)
// Unban is a Minecraft pardon
type Unban struct {
Target string `json:"target"`
}
// Kick is a Minecraft kick
type Kick struct {
Unban
Reason string `json:"reason"`
}
// Ban is a Minecraft ban
type Ban struct {
Kick
Source string `json:"source"`
Created int64 `json:"created"`
}
// Ban is a Minecraft ban with an expiration
type TempBan struct {
Ban
Expiration int64 `json:"expiration"`
}
// CreatedTime is Created converted to a time.Time
func (b *Ban) CreatedTime() time.Time {
return time.Unix(b.Created/1000, 0)
}
// ExpirationTime is Expiration converted to a time.Time
func (t *TempBan) ExpirationTime() time.Time {
return time.Unix(t.Expiration/1000, 0)
}
// Bans gets a list of Ban from a ServerAPI instance
func (c *Client) Bans() (bans Ban, err error) {
return bans, c.jsonGET(fmt.Sprintf("%s/bans", c.endpoint), &bans)
}
// Kick kicks a player
func (c *Client) Kick(k Kick) (int, error) {
return c.jsonPOST(fmt.Sprintf("%s/kick", c.endpoint), k)
}
// Ban bans a player
func (c *Client) Ban(b Ban) (int, error) {
return c.jsonPOST(fmt.Sprintf("%s/ban", c.endpoint), b)
}
// TempBan temporarily bans a player
func (c *Client) TempBan(t TempBan) (int, error) {
return c.jsonPOST(fmt.Sprintf("%s/ban", c.endpoint), t)
}
// Unban pardons a player
func (c *Client) Unban(u Unban) (int, error) {
return c.jsonPOST(fmt.Sprintf("%s/unban", c.endpoint), u)
}