Skip to content

Commit

Permalink
fix: stayActiveMessageTask
Browse files Browse the repository at this point in the history
  • Loading branch information
deanxv committed Mar 7, 2024
1 parent e3726f7 commit fbfc18c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var OnlyOpenaiApi = os.Getenv("ONLY_OPENAI_API")

var DebugEnabled = os.Getenv("DEBUG") == "true"

var Version = "v4.3.7" // this hard coding will be replaced automatically when building, no need to manually change
var Version = "v4.3.8" // this hard coding will be replaced automatically when building, no need to manually change

const (
RequestIdKey = "X-Request-Id"
Expand Down
2 changes: 1 addition & 1 deletion discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ func SendMessage(c *gin.Context, channelID, cozeBotId, message string) (*discord

if len(UserAuthorizations) == 0 {
//SetChannelDeleteTimer(channelID, 5*time.Second)
common.LogError(c.Request.Context(), fmt.Sprintf("无可用的 user_auth"))
common.LogError(ctx, fmt.Sprintf("无可用的 user_auth"))

// tg发送通知
if telegram.NotifyTelegramBotToken != "" && telegram.TgBot != nil {
Expand Down
18 changes: 13 additions & 5 deletions discord/sendmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package discord

import (
"bytes"
"context"
"coze-discord-proxy/common"
"encoding/json"
"fmt"
Expand All @@ -14,20 +15,27 @@ import (

// 用户端发送消息 注意 此为临时解决方案 后续会优化代码
func SendMsgByAuthorization(c *gin.Context, userAuth, content, channelId string) (string, error) {
var ctx context.Context
if c == nil {
ctx = context.Background()
} else {
ctx = c.Request.Context()
}

postUrl := "https://discord.com/api/v9/channels/%s/messages"

// 构造请求体
requestBody, err := json.Marshal(map[string]interface{}{
"content": content,
})
if err != nil {
common.LogError(c.Request.Context(), fmt.Sprintf("Error encoding request body:%s", err))
common.LogError(ctx, fmt.Sprintf("Error encoding request body:%s", err))
return "", err
}

req, err := http.NewRequest("POST", fmt.Sprintf(postUrl, channelId), bytes.NewBuffer(requestBody))
if err != nil {
common.LogError(c.Request.Context(), fmt.Sprintf("Error creating request:%s", err))
common.LogError(ctx, fmt.Sprintf("Error creating request:%s", err))
return "", err
}

Expand Down Expand Up @@ -56,7 +64,7 @@ func SendMsgByAuthorization(c *gin.Context, userAuth, content, channelId string)

resp, err := client.Do(req)
if err != nil {
common.LogError(c.Request.Context(), fmt.Sprintf("Error sending request:%s", err))
common.LogError(ctx, fmt.Sprintf("Error sending request:%s", err))
return "", err
}
defer resp.Body.Close()
Expand Down Expand Up @@ -87,14 +95,14 @@ func SendMsgByAuthorization(c *gin.Context, userAuth, content, channelId string)
if errMessage, ok := result["message"].(string); ok {
if strings.Contains(errMessage, "401: Unauthorized") ||
strings.Contains(errMessage, "You need to verify your account in order to perform this action.") {
common.LogWarn(c.Request.Context(), fmt.Sprintf("USER_AUTHORIZATION:%s EXPIRED", userAuth))
common.LogWarn(ctx, fmt.Sprintf("USER_AUTHORIZATION:%s EXPIRED", userAuth))
return "", &common.DiscordUnauthorizedError{
ErrCode: 401,
Message: "discord 鉴权未通过",
}
}
}
common.LogError(c.Request.Context(), fmt.Sprintf("user_auth:%s result:%s", userAuth, bodyString))
common.LogError(ctx, fmt.Sprintf("user_auth:%s result:%s", userAuth, bodyString))
return "", fmt.Errorf("/api/v9/channels/%s/messages response err", channelId)
} else {
return id, nil
Expand Down

0 comments on commit fbfc18c

Please sign in to comment.