5 Commits

Author SHA1 Message Date
8c858464ec updated api reference url. 2021-09-08 17:29:34 +03:00
4a88e59aba added edit, delete bots. 2021-09-08 17:23:22 +03:00
cd73fbb079 added edit bot struct. 2021-09-08 17:00:13 +03:00
6d10fabd42 added fetch bot(s) functions. 2021-09-08 16:55:28 +03:00
24b09a68ba added bot struct. 2021-09-08 16:34:08 +03:00
4 changed files with 155 additions and 1 deletions

View File

@ -20,7 +20,7 @@ Revoltgo is a go package for writing bots / self-bots in revolt easily.
## API Reference
Click [here](https://pkg.go.dev/github.com/5elenay/revoltgo@v0.2.0) for api reference.
Click [here](https://pkg.go.dev/github.com/5elenay/revoltgo@v0.3.0) for api reference.
## Ping Pong Example (Bot)

56
bot.go Normal file
View File

@ -0,0 +1,56 @@
package revoltgo
import (
"encoding/json"
"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.
func (b *Bot) Edit(eb *EditBot) error {
data, err := json.Marshal(eb)
if err != nil {
return err
}
_, err = b.Client.Request("PATCH", "/bots/"+b.Id, data)
return err
}
// Delete the bot.
func (b *Bot) Delete() error {
_, err := b.Client.Request("DELETE", "/bots/"+b.Id, []byte{})
return err
}

View File

@ -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
}

View File

@ -279,3 +279,36 @@ type Binary struct {
func (b Binary) Save(path string) error {
return os.WriteFile(path, b.Data, 0666)
}
// Edit bot struct
// Please see https://developers.revolt.chat/api/#tag/Bots/paths/~1bots~1:bot/patch for more information.
type EditBot struct {
Name string `json:"name,omitempty"`
Public bool `json:"public,omitempty"`
InteractionsUrl string `json:"interactionsURL,omitempty"`
Remove string `json:"remove,omitempty"`
}
// Set name for struct.
func (eb *EditBot) SetName(name string) *EditBot {
eb.Name = name
return eb
}
// Set public value for struct.
func (eb *EditBot) SetPublicValue(is_public bool) *EditBot {
eb.Public = is_public
return eb
}
// Set interaction url for struct.
func (eb *EditBot) SetInteractionsUrl(url string) *EditBot {
eb.InteractionsUrl = url
return eb
}
// Remove interaction url for struct.
func (eb *EditBot) RemoveInteractionsUrl() *EditBot {
eb.Remove = "InteractionsURL"
return eb
}