Skip to content

Commit

Permalink
Merge pull request #32 from ritsec/30-message-modification-edge-case
Browse files Browse the repository at this point in the history
Edge case in Message Modifications
  • Loading branch information
1nv8rzim authored Sep 8, 2023
2 parents 12a3ed0 + da59ca9 commit 26e9647
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions commands/handlers/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,14 @@ func MessageDelete(s *discordgo.Session, m *discordgo.MessageDelete) {

// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits
// 1024 characters is the max length of a field value
if len(message) > 1013 {

// Math:
// "```\n" + "\n```" = 8 characters => 1024 - 8 = 1016 characters or less does not need to be truncated
// "```\n" + "...\n```" = 11 characters => 1024 - 8 = 1013 max characters if needs truncated
if len(message) > 1016 {
message = "```\n" + message[:1013] + "\n...```"
} else {
message = "```\n" + message + "\n...```"
message = "```\n" + message + "\n```"
}

_, err := s.ChannelMessageSendComplex(
Expand Down Expand Up @@ -157,22 +161,26 @@ func MessageEdit(s *discordgo.Session, m *discordgo.MessageUpdate) {

// https://discord.com/developers/docs/resources/channel#embed-object-embed-limits
// 1024 characters is the max length of a field value
if len(messageBefore) > 1024 {

// Math:
// "```\n" + "\n```" = 8 characters => 1024 - 8 = 1016 characters or less does not need to be truncated
// "```\n" + "...\n```" = 11 characters => 1024 - 8 = 1013 max characters if needs truncated
if len(messageBefore) > 1016 {
messageBefore = "```\n" + messageBefore[:1013] + "\n...```"
} else {
messageBefore = "```\n" + messageBefore + "\n...```"
messageBefore = "```\n" + messageBefore + "\n```"
}

if len(messageAfter) > 1024 {
if len(messageAfter) > 1016 {
messageAfter = "```\n" + messageAfter[:1013] + "\n...```"
} else {
messageAfter = "```\n" + messageAfter + "\n...```"
messageAfter = "```\n" + messageAfter + "\n```"
}

if len(difference) > 1013 {
if len(difference) > 1016 {
difference = "```\n" + difference[:1013] + "\n...```"
} else {
difference = "```\n" + difference + "\n...```"
difference = "```\n" + difference + "\n```"
}

_, err := s.ChannelMessageSendComplex(
Expand Down

0 comments on commit 26e9647

Please sign in to comment.