Skip to content

Commit

Permalink
discord fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jdutchak committed Aug 2, 2024
1 parent d093270 commit 89413f9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
26 changes: 13 additions & 13 deletions pkg/api/handlers_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,37 +373,37 @@ func (api *API) SearchDiscordProfile() gin.HandlerFunc {
// SearchChannelMessages returns a gin.HandlerFunc that processes a request to search for messages in a Discord channel.
func (api *API) SearchChannelMessages() gin.HandlerFunc {
return func(c *gin.Context) {
var reqBody struct {
var reqParams struct {
ChannelID string `json:"channelID"`
Limit int `json:"limit"`
Limit string `json:"limit"`
Before string `json:"before"`
}

// Extract the required parameter from the URL path
reqBody.ChannelID = c.Param("channelID")
if reqBody.ChannelID == "" {
reqParams.ChannelID = c.Param("channelID")
if reqParams.ChannelID == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "ChannelID must be provided and valid"})
return
}

// Extract optional query parameters
if limitStr := c.Query("limit"); limitStr != "" {
if limit, err := strconv.Atoi(limitStr); err == nil {
reqBody.Limit = limit
reqParams.Limit = c.Query("limit")
reqParams.Before = c.Query("before")

if reqParams.Limit != "" {
if _, err := strconv.Atoi(reqParams.Limit); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit parameter"})
return
}
}

reqBody.Before = c.Query("before")

// worker handler implementation
bodyBytes, err := json.Marshal(reqBody)
bodyBytes, err := json.Marshal(reqParams)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// Print the reqBody for debugging
logrus.Infof("Request Body: %+v", reqBody)
logrus.Infof("Request Body: %+v", reqParams)

requestID := uuid.New().String()
responseCh := pubsub2.GetResponseChannelMap().CreateChannel(requestID)
Expand Down
11 changes: 7 additions & 4 deletions pkg/scrapers/discord/getchannelmessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"os"
"strconv"

"github.com/masa-finance/masa-oracle/pkg/llmbridge"
)
Expand All @@ -26,9 +27,9 @@ type ChannelMessage struct {
}

// GetChannelMessages fetches messages for a specific channel from the Discord API
func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMessage, error) {
func GetChannelMessages(channelID string, limit string, before string) ([]ChannelMessage, error) {
// Print the parameters for debugging
fmt.Printf("Parameters - channelID: %s, limit: %d, before: %s\n", channelID, limit, before)
fmt.Printf("Parameters - channelID: %s, limit: %s, before: %s\n", channelID, limit, before)

botToken := os.Getenv("DISCORD_BOT_TOKEN") // Replace with your actual environment variable name
if botToken == "" {
Expand All @@ -41,9 +42,11 @@ func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMe
return nil, err
}

limitCheck, _ := strconv.Atoi(limit)

// Add query parameters if they are provided
q := req.URL.Query()
if limit > 0 && limit <= 100 {
if limitCheck > 0 && limitCheck <= 100 {
q.Add("limit", fmt.Sprintf("%d", limit))

Check failure on line 50 in pkg/scrapers/discord/getchannelmessages.go

View workflow job for this annotation

GitHub Actions / test

fmt.Sprintf format %d has arg limit of wrong type string
}
if before != "" {
Expand Down Expand Up @@ -83,7 +86,7 @@ func GetChannelMessages(channelID string, limit int, before string) ([]ChannelMe
// ScrapeDiscordMessagesForSentiment scrapes messages from a Discord channel and analyzes their sentiment.
func ScrapeDiscordMessagesForSentiment(channelID string, model string, prompt string) (string, string, error) {
// Fetch messages from the Discord channel
messages, err := GetChannelMessages(channelID, 100, "")
messages, err := GetChannelMessages(channelID, "100", "")
if err != nil {
return "", "", fmt.Errorf("error fetching messages from Discord channel: %v", err)
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/workers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ func (a *Worker) HandleWork(ctx actor.Context, m *messages.Work, node *masa.Orac
resp, err = discord.GetUserProfile(userID)
case string(WORKER.DiscordChannelMessages):
channelID := bodyData["channelID"].(string)
limit := bodyData["limit"].(int)
before := bodyData["before"].(string)

// Print the values for debugging
logrus.Infof("GetChannelMessages called with channelID: %s, limit: %d, before: %s", channelID, limit, before)

resp, err = discord.GetChannelMessages(channelID, limit, before)
resp, err = discord.GetChannelMessages(channelID, bodyData["limit"].(string), bodyData["before"].(string))
case string(WORKER.DiscordSentiment):
logrus.Infof("[+] Discord Channel Messages %s %s", m.Data, m.Sender)
channelID := bodyData["channelID"].(string)
Expand Down
2 changes: 1 addition & 1 deletion pkg/workers/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (a *Worker) Receive(ctx actor.Context) {
workerDoneCh <- msg
ctx.Poison(ctx.Self())
default:
logrus.Warningf("[+] Received unknown message: %T, message: %+v", m, m)
logrus.Warningf("[+] Received unknown message in workers: %T, message: %+v", m, m)
}
}

Expand Down

0 comments on commit 89413f9

Please sign in to comment.