From c21bd368a7067b6bef6ba6fc6b9fcbd3032f8295 Mon Sep 17 00:00:00 2001 From: pikachu0310 Date: Wed, 7 Feb 2024 13:02:42 +0900 Subject: [PATCH] =?UTF-8?q?:sparcles:=20=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=AB=E5=85=A5=E3=81=A3=E3=81=A6=E3=81=84=E3=81=AA?= =?UTF-8?q?=E3=81=8B=E3=81=A3=E3=81=9F=E3=82=89=E3=82=B5=E3=83=BC=E3=83=90?= =?UTF-8?q?=E3=83=BC=E3=81=AB=E8=BF=BD=E5=8A=A0=E3=81=95=E3=81=9B=E3=82=8B?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yml | 2 ++ internal/api/discord.go | 13 ++++++++++++- internal/apperrors/app_errors.go | 12 ++++++++++++ internal/handler/auth.go | 6 +++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/compose.yml b/compose.yml index 7871fb3..19d33e1 100644 --- a/compose.yml +++ b/compose.yml @@ -17,6 +17,8 @@ services: DISCORD_CLIENT_ID: ${DISCORD_CLIENT_ID} DISCORD_CLIENT_SECRET: ${DISCORD_CLIENT_SECRET} DISCORD_CLIENT_REDIRECT_URI: ${DISCORD_CLIENT_REDIRECT_URI} + DISCORD_SERVER_ID: ${DISCORD_SERVER_ID} + DISCORD_BOT_TOKEN: ${DISCORD_BOT_TOKEN} depends_on: db: condition: service_healthy diff --git a/internal/api/discord.go b/internal/api/discord.go index dfed108..63bd7ce 100644 --- a/internal/api/discord.go +++ b/internal/api/discord.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "github.com/traPtitech/game3-back/internal/apperrors" "github.com/traPtitech/game3-back/internal/enum" "github.com/traPtitech/game3-back/internal/pkg/util" "github.com/traPtitech/game3-back/openapi/models" @@ -118,7 +119,9 @@ func AddUserToGuild(accessToken *string, guildID string, userID string) error { botToken, err := util.GetEnvOrErr("DISCORD_BOT_TOKEN") if err != nil { return err + } + req.Header.Set("Authorization", "Bot "+botToken) req.Header.Set("Content-Type", "application/json") @@ -129,8 +132,16 @@ func AddUserToGuild(accessToken *string, guildID string, userID string) error { } defer resp.Body.Close() + if resp.StatusCode == http.StatusNoContent { + return apperrors.NewAlreadyInGuildError() + } if resp.StatusCode != http.StatusCreated { - return errors.New("Failed to add user to guild: status " + resp.Status) + body, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + + return errors.New("Failed to add user to guild: status " + resp.Status + " body: " + string(body)) } return nil diff --git a/internal/apperrors/app_errors.go b/internal/apperrors/app_errors.go index 2a0c7c6..b960dc3 100644 --- a/internal/apperrors/app_errors.go +++ b/internal/apperrors/app_errors.go @@ -71,3 +71,15 @@ func (e *SessionTokenNotFoundError) Error() string { func NewSessionTokenNotFoundError() error { return &SessionTokenNotFoundError{} } + +// AlreadyInGuildError 既にギルドに所属している +type AlreadyInGuildError struct{} + +func (e *AlreadyInGuildError) Error() string { + return "Already in the guild" +} + +// NewAlreadyInGuildError 既にギルドに所属しているエラーを返す +func NewAlreadyInGuildError() error { + return &AlreadyInGuildError{} +} diff --git a/internal/handler/auth.go b/internal/handler/auth.go index 6ea660c..5191706 100644 --- a/internal/handler/auth.go +++ b/internal/handler/auth.go @@ -1,6 +1,7 @@ package handler import ( + "errors" "github.com/google/uuid" "github.com/labstack/echo/v4" "github.com/traPtitech/game3-back/internal/api" @@ -67,8 +68,11 @@ func (h *Handler) OauthCallback(c echo.Context, params models.OauthCallbackParam return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - if isInGame3Server == false { + if !isInGame3Server { err = addUserToGame3Guild(tokenResponse.AccessToken) + if errors.Is(err, apperrors.NewAlreadyInGuildError()) { + // 起きないはずだけど pass + } if err != nil { return err }