Skip to content

Commit

Permalink
api: Mutate validated embeds from discord.Embed.Validate (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
mavolin authored Aug 24, 2021
1 parent 1e9eb4a commit f334491
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
12 changes: 10 additions & 2 deletions api/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (c *Client) RespondInteraction(
if sum > 6000 {
return &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
}

(*resp.Data.Embeds)[i] = embed // embed.Validate changes fields
}
}
}
Expand Down Expand Up @@ -180,14 +182,16 @@ func (c *Client) EditInteractionResponse(

if data.Embeds != nil {
sum := 0
for _, e := range *data.Embeds {
for i, e := range *data.Embeds {
if err := e.Validate(); err != nil {
return nil, errors.Wrap(err, "embed error")
}
sum += e.Length()
if sum > 6000 {
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of text in embeds"}
}

(*data.Embeds)[i] = e // e.Validate changes fields
}
}

Expand Down Expand Up @@ -227,6 +231,8 @@ func (c *Client) CreateInteractionFollowup(
if sum > 6000 {
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
}

(*data.Embeds)[i] = embed // embed.Validate changes fields
}
}

Expand All @@ -247,14 +253,16 @@ func (c *Client) EditInteractionFollowup(

if data.Embeds != nil {
sum := 0
for _, e := range *data.Embeds {
for i, e := range *data.Embeds {
if err := e.Validate(); err != nil {
return nil, errors.Wrap(err, "embed error")
}
sum += e.Length()
if sum > 6000 {
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of text in embeds"}
}

(*data.Embeds)[i] = e // e.Validate changes fields
}
}

Expand Down
5 changes: 4 additions & 1 deletion api/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,13 @@ func (c *Client) EditMessageComplex(
}
sum += embed.Length()
if sum > 6000 {
return nil, &discord.OverboundError{sum, 6000, "sum of all text in embeds"}
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
}

(*data.Embeds)[i] = embed // embed.Validate changes fields
}
}

var msg *discord.Message
return msg, sendpart.PATCH(c.Client, data, &msg,
EndpointChannels+channelID.String()+"/messages/"+messageID.String())
Expand Down
4 changes: 3 additions & 1 deletion api/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ func (c *Client) SendMessageComplex(
}
sum += embed.Length()
if sum > 6000 {
return nil, &discord.OverboundError{sum, 6000, "sum of all text in embeds"}
return nil, &discord.OverboundError{Count: sum, Max: 6000, Thing: "sum of all text in embeds"}
}

data.Embeds[i] = embed // embed.Validate changes fields
}

var URL = EndpointChannels + channelID.String() + "/messages"
Expand Down

0 comments on commit f334491

Please sign in to comment.