diff --git a/bot.go b/bot.go index 1a665da..fb6b1f2 100644 --- a/bot.go +++ b/bot.go @@ -1,10 +1,39 @@ package revoltgo +import ( + "time" + + "github.com/oklog/ulid/v2" +) + // Bot struct. type Bot struct { + Client *Client + CreatedAt time.Time + Id string `json:"_id"` OwnerId string `json:"owner"` Token string `json:"token"` IsPublic bool `json:"public"` InteractionsUrl string `json:"interactionsURL"` } + +// Fetched bots struct. +type FetchedBots struct { + Bots []*Bot `json:"bots"` + Users []*User `json:"users"` +} + +// Calculate creation date and edit the struct. +func (b *Bot) CalculateCreationDate() error { + ulid, err := ulid.Parse(b.Id) + + if err != nil { + return err + } + + b.CreatedAt = time.UnixMilli(int64(ulid.Time())) + return nil +} + +// Edit the bot. diff --git a/client.go b/client.go index 13ef068..f0b5ade 100644 --- a/client.go +++ b/client.go @@ -312,3 +312,68 @@ func (c Client) RemoveFriend(username string) (*UserRelations, error) { err = json.Unmarshal(resp, relationshipData) return relationshipData, err } + +// Create a new bot. +func (c *Client) CreateBot(name string) (*Bot, error) { + botData := &Bot{} + botData.Client = c + + resp, err := c.Request("POST", "/bots/create", []byte("{\"name\":\""+name+"\"}")) + + if err != nil { + return botData, err + } + + err = json.Unmarshal(resp, botData) + return botData, err + +} + +// Fetch client bots. +func (c *Client) FetchBots() (*FetchedBots, error) { + bots := &FetchedBots{} + + resp, err := c.Request("GET", "/bots/@me", []byte{}) + + if err != nil { + return bots, err + } + + err = json.Unmarshal(resp, bots) + + if err != nil { + return bots, err + } + + // Add client for bots. + for _, i := range bots.Bots { + i.Client = c + } + + // Add client for users. + for _, i := range bots.Users { + i.Client = c + } + + return bots, nil +} + +// Fetch a bot. +func (c *Client) FetchBot(id string) (*Bot, error) { + bot := &struct { + Bot *Bot `json:"bot"` + }{ + Bot: &Bot{ + Client: c, + }, + } + + resp, err := c.Request("GET", "/bots/"+id, []byte{}) + + if err != nil { + return bot.Bot, err + } + + err = json.Unmarshal(resp, bot) + return bot.Bot, err +}