about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--cmd/bot/main.go6
-rw-r--r--go.mod2
-rw-r--r--go.sum2
-rw-r--r--pkg/bot/bot.go25
-rw-r--r--pkg/discord/discord.go94
5 files changed, 124 insertions, 5 deletions
diff --git a/cmd/bot/main.go b/cmd/bot/main.go
index 2813504..b709ffc 100644
--- a/cmd/bot/main.go
+++ b/cmd/bot/main.go
@@ -3,5 +3,9 @@ package main
 import "jinx/pkg/bot"
 
 func main() {
-	bot.Greet()
+	err := bot.Start()
+
+	if err != nil {
+		panic(err)
+	}
 }
diff --git a/go.mod b/go.mod
index d59af7c..3e15bef 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
 module jinx
 
 go 1.18
+
+require github.com/gorilla/websocket v1.5.0
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..e5a03d4
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
+github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go
index 28c506c..264e328 100644
--- a/pkg/bot/bot.go
+++ b/pkg/bot/bot.go
@@ -1,7 +1,24 @@
 package bot
 
-import "fmt"
+import (
+	"fmt"
+	"jinx/pkg/discord"
+)
 
-func Greet() {
-	fmt.Println("hello, world!")
-}
+const TOKEN = "123567890123456789012345678901234567890"
+
+func Start() error {
+	fmt.Println("hi..!")
+
+	client := discord.NewClient(TOKEN)
+
+	fmt.Println("connecting..")
+
+	err := client.Connect()
+
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
\ No newline at end of file
diff --git a/pkg/discord/discord.go b/pkg/discord/discord.go
new file mode 100644
index 0000000..13de020
--- /dev/null
+++ b/pkg/discord/discord.go
@@ -0,0 +1,94 @@
+package discord
+
+import (
+	"bytes"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"net/http"
+
+	"github.com/gorilla/websocket"
+)
+
+const DISCORD_URl = "https://discord.com/api/v9/"
+const USER_AGENT = "DiscordBot (https://jinx.rnrd.eu/, v0.0.0) Jinx"
+
+type Discord struct {
+	Token string
+}
+
+func NewClient(token string) *Discord {
+	return &Discord{
+		Token: token,
+	}
+}
+
+func (d *Discord) Connect() error {
+	gatewayURL, err := d.gateway()
+	if err != nil {
+		return err
+	}
+
+	fmt.Printf("Gateway: %s\n", gatewayURL)
+
+	connectHeader := http.Header{}
+	conn, _, err := websocket.DefaultDialer.Dial(gatewayURL, connectHeader)
+	if err != nil {
+		return err
+	}
+
+	defer conn.Close()
+
+	messageType, message, err := conn.ReadMessage()
+	if err != nil {
+		return err
+	}
+
+	fmt.Printf("Type: %d, Message: %s\n", messageType, message)
+
+	return nil
+}
+
+func (d *Discord) gateway() (string, error) {
+	url := DISCORD_URl + "gateway"
+
+	req, err := http.NewRequest("GET", url, nil)
+	if err != nil {
+		return "", err
+	}
+
+	req.Header.Set("Authorization", d.Token)
+	req.Header.Set("Content-Type", "application/json")
+	req.Header.Set("User-Agent", USER_AGENT)
+
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		return "", err
+	}
+
+	defer resp.Body.Close()
+
+	var buf bytes.Buffer
+	_, err = buf.ReadFrom(resp.Body)
+	if err != nil {
+		return "", err
+	}
+
+	switch resp.StatusCode {
+	case 200:
+	default:
+		return "", errors.New("Gateway response status code: " + resp.Status)
+	}
+
+	body := struct {
+		URL string `json:"url"`
+	}{}
+
+	err = json.Unmarshal(buf.Bytes(), &body)
+	if err != nil {
+		return "", err
+	}
+
+	url = body.URL + "?v=9&encoding=json"
+	return url, nil
+}