starting on guild and member fetching
This commit is contained in:
parent
9755318400
commit
aafe3e2d21
12 changed files with 149 additions and 20 deletions
|
@ -1,8 +1,57 @@
|
|||
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)
|
||||
}
|
||||
|
||||
func Get(url string) {
|
||||
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
|
||||
}
|
||||
|
|
30
discord/api_mock.go
Normal file
30
discord/api_mock.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type DiscordClientMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (c *DiscordClientMock) Do(req *http.Request) (*http.Response, error) {
|
||||
args := c.Called(req)
|
||||
|
||||
return args.Get(0).(*http.Response), args.Error(1)
|
||||
}
|
||||
|
||||
func (c *DiscordClientMock) MockResponse(statusCode int, data any) *http.Response {
|
||||
body := bytes.Buffer{}
|
||||
json.NewEncoder(&body).Encode(data)
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: statusCode,
|
||||
Body: io.NopCloser(&body),
|
||||
}
|
||||
}
|
|
@ -1,11 +1,12 @@
|
|||
package discord
|
||||
|
||||
import "context"
|
||||
import "git.sapphic.engineer/roleypoly/v4/types"
|
||||
|
||||
type IGuild interface {
|
||||
GetMember(ctx context.Context, memberID string) (IMember, error)
|
||||
GetMember(memberID string) (IMember, error)
|
||||
}
|
||||
|
||||
type Guild struct {
|
||||
ID string
|
||||
Client IDiscordClient
|
||||
types.DiscordGuild
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
|
@ -10,8 +8,8 @@ type GuildMock struct {
|
|||
mock.Mock
|
||||
}
|
||||
|
||||
func (g *GuildMock) GetMember(ctx context.Context, memberID string) (IMember, error) {
|
||||
args := g.Called(ctx, memberID)
|
||||
func (g *GuildMock) GetMember(memberID string) (IMember, error) {
|
||||
args := g.Called(memberID)
|
||||
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package discord
|
||||
|
||||
import "context"
|
||||
|
||||
type IGuildService interface {
|
||||
GetGuild(ctx context.Context, guildID string) (IGuild, error)
|
||||
Client() IDiscordClient
|
||||
GetGuild(guildID string) (IGuild, error)
|
||||
}
|
||||
|
||||
type GuildService struct {
|
||||
client IDiscordClient
|
||||
}
|
||||
|
||||
func (gs *GuildService) GetGuild(ctx context.Context, guildID string) (IGuild, error) {
|
||||
return nil, nil
|
||||
func (gs *GuildService) Client() IDiscordClient {
|
||||
return gs.client
|
||||
}
|
||||
|
||||
func (gs *GuildService) GetGuild(guildID string) (IGuild, error) {
|
||||
// gs.client
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
|
@ -10,8 +8,8 @@ type GuildServiceMock struct {
|
|||
mock.Mock
|
||||
}
|
||||
|
||||
func (gs *GuildServiceMock) GetGuild(ctx context.Context, guildID string) (IGuild, error) {
|
||||
args := gs.Called(ctx, guildID)
|
||||
func (gs *GuildServiceMock) GetGuild(guildID string) (IGuild, error) {
|
||||
args := gs.Called(guildID)
|
||||
|
||||
if args.Get(0) == nil {
|
||||
return nil, args.Error(1)
|
||||
|
|
|
@ -72,3 +72,8 @@ func (i *Interactions) PostHandler(c fiber.Ctx) error {
|
|||
|
||||
return types.NewAPIError(http.StatusNotImplemented, "not implemented").Send(c)
|
||||
}
|
||||
|
||||
func (i *Interactions) Respond(ix Interaction, ir InteractionResponse) error {
|
||||
// TODO: make request to /webhooks/appid/{ix.Token}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ type Interaction struct {
|
|||
GuildID string `json:"guild_id"`
|
||||
AppPermissions uint64 `json:"app_permissions"`
|
||||
Type uint64 `json:"type"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
@ -1,14 +1,43 @@
|
|||
package testing
|
||||
|
||||
import "github.com/gofiber/fiber/v3"
|
||||
import (
|
||||
"log"
|
||||
|
||||
type TestingController struct{}
|
||||
"github.com/gofiber/fiber/v3"
|
||||
|
||||
"git.sapphic.engineer/roleypoly/v4/discord"
|
||||
"git.sapphic.engineer/roleypoly/v4/types"
|
||||
)
|
||||
|
||||
type TestingController struct {
|
||||
Guilds discord.IGuildService
|
||||
}
|
||||
|
||||
func (t *TestingController) Routes(r fiber.Router) {
|
||||
r.Get("/picker/:version?", t.Picker)
|
||||
r.Get("/m/:server/:user", t.GetMember)
|
||||
}
|
||||
|
||||
func (t *TestingController) Picker(c fiber.Ctx) error {
|
||||
version := c.Params("version", "main")
|
||||
return c.Render("picker/"+version, fiber.Map{})
|
||||
}
|
||||
|
||||
func (t *TestingController) GetMember(c fiber.Ctx) error {
|
||||
serverID := c.Params("server")
|
||||
userID := c.Params("user")
|
||||
|
||||
g, err := t.Guilds.GetGuild(serverID)
|
||||
if err != nil {
|
||||
log.Println("testing/get guild: ", err)
|
||||
types.NewAPIError(500, err.Error()).Send(c)
|
||||
}
|
||||
|
||||
m, err := g.GetMember(userID)
|
||||
if err != nil {
|
||||
log.Println("testing/get member: ", err)
|
||||
types.NewAPIError(500, err.Error()).Send(c)
|
||||
}
|
||||
|
||||
return c.JSON(m)
|
||||
}
|
||||
|
|
3
types/guild.go
Normal file
3
types/guild.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
package types
|
||||
|
||||
type DiscordGuild struct{}
|
|
@ -1,7 +1,14 @@
|
|||
package utils
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func HeadTitle(text string) string {
|
||||
return fmt.Sprintf("%s | Roleypoly", text)
|
||||
}
|
||||
|
||||
func J(parts ...string) string {
|
||||
return "/" + strings.Join(parts, "/")
|
||||
}
|
||||
|
|
|
@ -11,3 +11,7 @@ import (
|
|||
func TestHeadTitle(t *testing.T) {
|
||||
assert.Equal(t, utils.HeadTitle("Hello World"), "Hello World | Roleypoly")
|
||||
}
|
||||
|
||||
func TestJ(t *testing.T) {
|
||||
assert.Equal(t, "a/b/c", utils.J("a", "b", "c"))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue