aboutsummaryrefslogtreecommitdiff
path: root/web/revolt/bot.go
blob: 147dfedb304b013e0acd0f18a76658d5ddafbf5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package revolt

import (
	"context"
	"encoding/json"
	"time"

	"github.com/oklog/ulid/v2"
)

// Bot struct.
type Bot struct {
	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
}

func (c *Client) BotEdit(ctx context.Context, id string, eb *EditBot) error {
	data, err := json.Marshal(eb)
	if err != nil {
		return err
	}

	if _, err := c.Request(ctx, "PATCH", "/bots/"+id, data); err != nil {
		return err
	}

	return nil
}

func (c *Client) BotDelete(ctx context.Context, id string) error {
	if _, err := c.Request(ctx, "DELETE", "/bots/"+id, []byte{}); err != nil {
		return err
	}

	return nil
}