added edit channel function.

This commit is contained in:
5elenay 2021-08-22 06:45:52 +03:00
parent cadd1c6413
commit f80532ef53
3 changed files with 115 additions and 21 deletions

View File

@ -22,21 +22,10 @@ type Channel struct {
Description string `json:"description"`
Icon *Attachment `json:"icon"`
DefaultPermissions int `json:"default_permissions"`
RolePermissions int `json:"role_permissions"`
RolePermissions interface{} `json:"role_permissions"`
Permissions int `json:"permissions"`
}
// Similar to message, but created for sendmessage function.
type SendMessageStruct struct {
Content string `json:"content"`
Attachments []string `json:"attachments"`
Nonce string `json:"nonce"`
Replies []struct {
Id string `json:"id"`
Mention bool `json:"mention"`
}
}
// Fetched messages struct.
type FetchedMessages struct {
Messages []*Message `json:"messages"`
@ -44,12 +33,13 @@ type FetchedMessages struct {
}
// Send a message to the channel.
func (c Channel) SendMessage(message *SendMessageStruct) (*Message, error) {
func (c Channel) SendMessage(message *SendMessage) (*Message, error) {
if message.Nonce == "" {
message.Nonce = genULID()
message.CreateNonce()
}
respMessage := &Message{}
respMessage.Client = c.Client
msgData, err := json.Marshal(message)
if err != nil {
@ -68,7 +58,6 @@ func (c Channel) SendMessage(message *SendMessageStruct) (*Message, error) {
return respMessage, err
}
respMessage.Client = c.Client
return respMessage, nil
}
@ -122,6 +111,7 @@ func (c Channel) FetchMessages(options map[string]interface{}) (*FetchedMessages
// Fetch a message from channel by Id.
func (c Channel) FetchMessage(id string) (*Message, error) {
msg := &Message{}
msg.Client = c.Client
resp, err := c.Client.Request("GET", "/channels/"+c.Id+"/messages/"+id, []byte{})
@ -135,6 +125,31 @@ func (c Channel) FetchMessage(id string) (*Message, error) {
return msg, err
}
msg.Client = c.Client
return msg, nil
}
// Edit channel.
func (c *Channel) Edit(ec *EditChannel) error {
data, err := json.Marshal(ec)
if err != nil {
return err
}
_, err = c.Client.Request("PATCH", "/channels/"+c.Id, data)
if err != nil {
return err
}
// Change channel struct
if ec.Name != "" {
c.Name = ec.Name
}
if ec.Description != "" {
c.Description = ec.Description
}
return nil
}

View File

@ -46,8 +46,9 @@ func (c *Client) OnMessageDelete(fn func(channel_id, message_id string)) {
}
// Fetch a channel by Id.
func (c Client) FetchChannel(id string) (*Channel, error) {
func (c *Client) FetchChannel(id string) (*Channel, error) {
channel := &Channel{}
channel.Client = c
data, err := c.Request("GET", "/channels/"+id, []byte{})
@ -60,14 +61,13 @@ func (c Client) FetchChannel(id string) (*Channel, error) {
if err != nil {
return channel, err
}
channel.Client = &c
return channel, nil
}
// Fetch an user by Id.
func (c Client) FetchUser(id string) (*User, error) {
func (c *Client) FetchUser(id string) (*User, error) {
user := &User{}
user.Client = c
data, err := c.Request("GET", "/users/"+id, []byte{})
@ -81,6 +81,5 @@ func (c Client) FetchUser(id string) (*User, error) {
return user, err
}
user.Client = &c
return user, nil
}

80
other.go Normal file
View File

@ -0,0 +1,80 @@
package revoltgo
// Similar to message, but created for send message function.
type SendMessage struct {
Content string `json:"content"`
Attachments []string `json:"attachments"`
Nonce string `json:"nonce"`
Replies []struct {
Id string `json:"id"`
Mention bool `json:"mention"`
}
}
// Set content.
func (sms *SendMessage) SetContent(content string) *SendMessage {
sms.Content = content
return sms
}
// Add a new attachment.
func (sms *SendMessage) AddAttachment(attachment string) *SendMessage {
sms.Attachments = append(sms.Attachments, attachment)
return sms
}
// Add a new reply.
func (sms *SendMessage) AddReply(id string, mention bool) *SendMessage {
sms.Replies = append(sms.Replies, struct {
Id string "json:\"id\""
Mention bool "json:\"mention\""
}{
Id: id,
Mention: mention,
})
return sms
}
// Create a unique nonce.
func (sms *SendMessage) CreateNonce() *SendMessage {
sms.Nonce = genULID()
return sms
}
// Edit channel struct.
// Please see: https://developers.revolt.chat/api/#tag/Channel-Information/paths/~1channels~1:channel/patch for more information.
type EditChannel struct {
Name string `json:"name"`
Description string `json:"description"`
Icon string `json:"icon"`
Remove string `json:"remove"`
}
// Set name for struct.
func (ec *EditChannel) SetName(name string) *EditChannel {
ec.Name = name
return ec
}
// Set description for struct.
func (ec *EditChannel) SetDescription(desc string) *EditChannel {
ec.Description = desc
return ec
}
// Set icon for struct.
func (ec *EditChannel) SetIcon(autumn_id string) *EditChannel {
ec.Icon = autumn_id
return ec
}
// Set remove item.
func (ec *EditChannel) RemoveItem(item string) *EditChannel {
if item != "Description" && item != "Icon" {
return ec
}
ec.Remove = item
return ec
}