Skip to content

Commit

Permalink
:sparcles: サーバーに入っていなかったらサーバーに追加させる処理の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
pikachu0310 committed Feb 7, 2024
1 parent d05baa5 commit c21bd36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion internal/api/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/apperrors/app_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
6 changes: 5 additions & 1 deletion internal/handler/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/game3-back/internal/api"
Expand Down Expand Up @@ -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()) {

Check warning on line 73 in internal/handler/auth.go

View workflow job for this annotation

GitHub Actions / Lint

empty-block: this block is empty, you can remove it (revive)
// 起きないはずだけど pass
}
if err != nil {
return err
}
Expand Down

0 comments on commit c21bd36

Please sign in to comment.