v4/discord/discord_client.go
2025-03-26 13:35:47 -07:00

66 lines
1.3 KiB
Go

package discord
import (
"fmt"
"net/http"
"net/url"
"time"
"github.com/goccy/go-json"
)
const DiscordBaseUrl = "https://discord.com/api/v10"
type IDiscordClient interface {
Do(req *http.Request) (*http.Response, error)
BotAuth(req *http.Request)
}
type DiscordClient struct {
Client *http.Client
BotToken string
ClientID string
ClientSecret string
}
func NewDiscordClient(clientID, clientSecret, botToken string) *DiscordClient {
return &DiscordClient{
ClientID: clientID,
ClientSecret: clientSecret,
BotToken: botToken,
Client: &http.Client{
Timeout: time.Second * 10,
},
}
}
func (d *DiscordClient) Do(req *http.Request) (*http.Response, error) {
return d.Client.Do(req)
}
func (d *DiscordClient) BotAuth(req *http.Request) {
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", d.BotToken))
}
func NewRequest(method string, path string) *http.Request {
url, err := url.Parse(fmt.Sprintf("%s%s", DiscordBaseUrl, path))
if err != nil {
panic("discord/api: wtf couldnt join a string??")
}
req := &http.Request{
Method: method,
URL: url,
Header: http.Header{},
}
req.Header.Set("User-Agent", "Roleypoly (https://roleypoly.com, v4)")
return req
}
func OutputResponse(resp *http.Response, dst any) error {
// TODO: more checks?
return json.NewDecoder(resp.Body).Decode(dst)
}