16 Commits

Author SHA1 Message Date
738bf5caf5 added begin, end typing functions. 2022-01-28 22:06:20 +03:00
3b6caba1dd fixes #7? 2022-01-28 21:56:10 +03:00
a1ea1288bf Merge pull request #5 from ayntgl/embeds
feat: implement sendable embeds
2022-01-17 16:54:56 +03:00
c8ff7699c8 feat: implement sendable embeds 2022-01-17 14:37:37 +04:00
5e00110348 updated api reference link. 2021-09-13 17:51:16 +03:00
c5d9887a53 fixes #3 2021-09-13 17:49:16 +03:00
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
09b894aa98 fixed syntax in readme. 2021-09-01 10:27:31 +03:00
d3f89b37ee added block / unblock user. 2021-09-01 10:24:10 +03:00
330cdf6984 added add / remove friend functions. 2021-09-01 10:07:15 +03:00
eb561040dd added fetch relation ship functions. 2021-09-01 10:02:09 +03:00
3c50f7e6fa added new note to readme. 2021-08-30 13:15:31 +03:00
8 changed files with 284 additions and 18 deletions

View File

@ -4,9 +4,11 @@ Revoltgo is a go package for writing bots / self-bots in revolt easily.
**NOTE**: This package is still under development and not finished. Create an issue if you found a bug.
**NOTE 2**: This package requires go 1.17.
## Features
- Can listen an event multiple times
- Multiple event listen
- Easy to use
- Supports self-bots
- Simple cache system
@ -18,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.1.0) for api reference.
Click [here](https://pkg.go.dev/github.com/5elenay/revoltgo@v0.3.1) 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

@ -220,3 +220,13 @@ func (c Channel) DeleteGroupRecipient(user_id string) error {
_, err := c.Client.Request("DELETE", "/channels/"+c.Id+"/recipients/"+user_id, []byte{})
return err
}
// Send a typing start event to the channel.
func (c *Channel) BeginTyping() {
c.Client.Socket.SendText(fmt.Sprintf("{\"type\":\"BeginTyping\",\"channel\":\"%s\"}", c.Id))
}
// End the typing event in the channel.
func (c *Channel) EndTyping() {
c.Client.Socket.SendText(fmt.Sprintf("{\"type\":\"EndTyping\",\"channel\":\"%s\"}", c.Id))
}

111
client.go
View File

@ -189,7 +189,7 @@ func (c *Client) Auth() error {
return fmt.Errorf("can't auth user (not a self-bot.)")
}
resp, err := c.Request("POST", "/auth/login", []byte("{\"email\":\""+c.SelfBot.Email+"\",\"password\":\""+c.SelfBot.Password+"\",\"captcha\": \"\"}"))
resp, err := c.Request("POST", "/auth/session/login", []byte("{\"email\":\""+c.SelfBot.Email+"\",\"password\":\""+c.SelfBot.Password+"\",\"name\":\"Revoltgo\"}"))
if err != nil {
return err
@ -268,3 +268,112 @@ func (c *Client) CreateGroup(name, description string, users []string) (*Channel
err = json.Unmarshal(resp, groupChannel)
return groupChannel, err
}
// Fetch relationships.
func (c Client) FetchRelationships() ([]*UserRelations, error) {
relationshipDatas := []*UserRelations{}
resp, err := c.Request("GET", "/users/relationships", []byte{})
if err != nil {
return relationshipDatas, err
}
err = json.Unmarshal(resp, &relationshipDatas)
return relationshipDatas, err
}
// Send friend request. / Accept friend request.
// User relations struct only will have status. id is not defined for this function.
func (c Client) AddFriend(username string) (*UserRelations, error) {
relationshipData := &UserRelations{}
resp, err := c.Request("PUT", "/users/"+username+"/friend", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}
// Deny friend request. / Remove friend.
// User relations struct only will have status. id is not defined for this function.
func (c Client) RemoveFriend(username string) (*UserRelations, error) {
relationshipData := &UserRelations{}
resp, err := c.Request("DELETE", "/users/"+username+"/friend", []byte{})
if err != nil {
return relationshipData, err
}
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

@ -23,7 +23,6 @@ func (c Client) Request(method, path string, data []byte) ([]byte, error) {
if c.SelfBot == nil {
req.Header.Set("x-bot-token", c.Token)
} else if c.SelfBot.SessionToken != "" && c.SelfBot.UserId != "" {
req.Header.Set("x-user-id", c.SelfBot.UserId)
req.Header.Set("x-session-token", c.SelfBot.SessionToken)
}

View File

@ -7,14 +7,27 @@ import (
// Similar to message, but created for send message function.
type SendMessage struct {
Content string `json:"content,omitempty"`
Attachments []string `json:"attachments,omitempty"`
Nonce string `json:"nonce,omitempty"`
DeleteAfter uint `json:"-"`
Replies []struct {
Id string `json:"id,omitempty"`
Mention bool `json:"mention"`
} `json:"replies,omitempty"`
Content string `json:"content"`
Nonce string `json:"nonce,omitempty"`
Attachments []string `json:"attachments,omitempty"`
Replies []Replies `json:"replies,omitempty"`
Embeds []SendableEmbed `json:"embeds,omitempty"`
DeleteAfter uint `json:"-"`
}
type SendableEmbed struct {
Type string `json:"type"`
IconUrl string `json:"icon_url,omitempty"`
Url string `json:"url,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Media string `json:"media,omitempty"`
Colour string `json:"colour,omitempty"`
}
type Replies struct {
Id string `json:"id"`
Mention bool `json:"mention"`
}
// Set content.
@ -43,10 +56,7 @@ func (sms *SendMessage) AddAttachment(attachment string) *SendMessage {
// Add a new reply.
func (sms *SendMessage) AddReply(id string, mention bool) *SendMessage {
sms.Replies = append(sms.Replies, struct {
Id string "json:\"id,omitempty\""
Mention bool "json:\"mention\""
}{
sms.Replies = append(sms.Replies, Replies{
Id: id,
Mention: mention,
})
@ -279,3 +289,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
}

45
user.go
View File

@ -86,3 +86,48 @@ func (u User) FetchDefaultAvatar() (*Binary, error) {
avatarData.Data = resp
return avatarData, nil
}
// Fetch user relationship.
func (u User) FetchRelationship() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("GET", "/users/"+u.Id+"/relationship", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}
// Block user.
func (u User) Block() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("PUT", "/users/"+u.Id+"/block", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}
// Un-block user.
func (u User) Unblock() (*UserRelations, error) {
relationshipData := &UserRelations{}
relationshipData.Id = u.Id
resp, err := u.Client.Request("DELETE", "/users/"+u.Id+"/block", []byte{})
if err != nil {
return relationshipData, err
}
err = json.Unmarshal(resp, relationshipData)
return relationshipData, err
}

View File

@ -25,6 +25,8 @@ func (c *Client) Start() {
}
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
fmt.Println(message)
// Parse data
rawData := &struct {
Type string `json:"type"`
@ -54,7 +56,7 @@ func (c *Client) handleWebsocketAuth() {
if c.SelfBot == nil {
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Authenticate\",\"token\":\"%s\"}", c.Token))
} else {
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Authenticate\",\"id\":\"%s\",\"session_token\":\"%s\",\"user_id\":\"%s\"}", c.SelfBot.Id, c.SelfBot.SessionToken, c.SelfBot.UserId))
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Authenticate\",\"result\":\"Success\",\"_id\":\"%s\",\"token\":\"%s\",\"user_id\":\"%s\",\"name\":\"Revoltgo\"}", c.SelfBot.Id, c.SelfBot.SessionToken, c.SelfBot.UserId))
}
}
@ -67,7 +69,7 @@ func (c *Client) Destroy() {
func (c *Client) ping() {
for {
time.Sleep(30 * time.Second)
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Ping\",\"time\":%d}", time.Now().Unix()))
c.Socket.SendText("{\"type\":\"Ping\",\"data\":0}")
}
}