57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package discord
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
const DiscordBaseUrl = "https://discord.com/api/v10"
|
|
|
|
type IDiscordClient interface {
|
|
Do(req *http.Request) (*http.Response, error)
|
|
}
|
|
|
|
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 NewRequest(method string, path string, botToken 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,
|
|
}
|
|
|
|
req.Header.Set("User-Agent", "Roleypoly (https://roleypoly.com, v4)")
|
|
|
|
if botToken != "" {
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", botToken))
|
|
}
|
|
|
|
return req
|
|
}
|