-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* start message impl * fix CreateMessage and add example
- Loading branch information
Showing
5 changed files
with
149 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
|
||
"github.com/Karitham/corde" | ||
) | ||
|
||
func main() { | ||
token := os.Getenv("DISCORD_BOT_TOKEN") | ||
if token == "" { | ||
log.Fatalln("DISCORD_BOT_TOKEN not set") | ||
} | ||
|
||
chID := corde.SnowflakeFromString(os.Getenv("DISCORD_CHANNEL_ID")) | ||
if chID == 0 { | ||
log.Fatalln("DISCORD_CHANNEL_ID not set") | ||
} | ||
|
||
m := corde.NewMux("", 0, token) | ||
|
||
message := corde.Message{ | ||
Embeds: []corde.Embed{ | ||
{ | ||
Title: "hello corde!", | ||
URL: "https://github.com/Karitham/corde", | ||
Description: "corde is awesome :knot:", | ||
}, | ||
}, | ||
} | ||
|
||
msg, err := m.CreateMessage(chID, message) | ||
if err != nil { | ||
log.Fatalln("error creating message: ", err) | ||
} | ||
|
||
log.Printf("message created: %s", msg.ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package corde | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"mime/multipart" | ||
|
||
"github.com/Karitham/corde/internal/rest" | ||
) | ||
|
||
// returns the content-type | ||
func toBodyMessage(w io.Writer, m Message) (string, error) { | ||
contentType := "application/json" | ||
|
||
payloadJSON := &bytes.Buffer{} | ||
err := json.NewEncoder(payloadJSON).Encode(m) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(m.Attachments) < 1 { | ||
payloadJSON.WriteTo(w) | ||
return contentType, nil | ||
} | ||
|
||
mw := multipart.NewWriter(w) | ||
defer mw.Close() | ||
|
||
contentType = mw.FormDataContentType() | ||
mw.WriteField("payload_json", payloadJSON.String()) | ||
|
||
if err := writeAttachments(mw, m.Attachments); err != nil { | ||
return contentType, err | ||
} | ||
|
||
return contentType, nil | ||
} | ||
|
||
// CreateMessage creates a new message in a channel | ||
// | ||
// https://discord.com/developers/docs/resources/channel#create-message | ||
func (m *Mux) CreateMessage(channelID Snowflake, data Message) (*Message, error) { | ||
body := &bytes.Buffer{} | ||
contentType, err := toBodyMessage(body, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := m.Client.Do( | ||
rest.Req("/channels", channelID, "messages"). | ||
AnyBody(body).Post(m.authorize, rest.ContentType(contentType)), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create message: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode > 299 { | ||
anyerr := map[string]any{} | ||
json.NewDecoder(resp.Body).Decode(&anyerr) | ||
return nil, fmt.Errorf("failed to create message: %+v", anyerr) | ||
} | ||
|
||
msg := &Message{} | ||
json.NewDecoder(resp.Body).Decode(msg) | ||
return msg, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters