Start with discord integration

This commit is contained in:
2023-07-02 19:10:14 -04:00
commit 31b17be2ca
10 changed files with 370 additions and 0 deletions

74
cmd/hub/main.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"golang.org/x/oauth2"
"go.fixergrid.net/servicedemon/pkg/discord"
)
func main() {
fmt.Println("Starting .... the >HUB<")
token, isSet := os.LookupEnv("DISCORD_BOT_TOKEN")
if !isSet {
fmt.Println("please set the environment variable 'DISCORD_BOT_TOKEN'")
return
}
tokensrc := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: token,
TokenType: "Bot",
})
ctx := context.Background()
client := discord.
NewClient(ctx, tokensrc).
WithDefaultChannel("1125162127133523978")
resp, err := client.Get("https://discord.com/api/v10/channels/1125162127133523978")
if err != nil {
fmt.Printf("there was an error making the request: %v\n", err)
return
}
out, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("failed to read response body: %v\n", err)
return
}
fmt.Printf("Got response:\n%s\n", out)
messageContent := map[string]interface{}{
"content": "Hello, world!",
}
b, err := json.Marshal(messageContent)
if err != nil {
fmt.Printf("failed to marshal message: %v\n", err)
return
}
r2, err := client.Post("https://discord.com/api/v10/channels/1125162127133523978/messages", "application/json", bytes.NewReader(b))
if err != nil {
fmt.Printf("failed tdo send message to server: %v\n", err)
return
}
out, err = ioutil.ReadAll(r2.Body)
if err != nil {
fmt.Printf("failed to read response body on send message: %v\n", err)
return
}
fmt.Printf("status: %v - res: %v\n", r2.Status, out)
}