Skip to content

Commit

Permalink
feat: send original message as md codeblock
Browse files Browse the repository at this point in the history
  • Loading branch information
lcox74 committed Aug 7, 2024
1 parent 6cd00e5 commit a1cb311
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion applications/news_moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ func (r ModerateNewsRule) OnMessage(ctx framework.MessageContext, channel *disco
return
}

content := ctx.Message().Content

// Test the message content
violation := r.test(ctx.Message().Content)
violation := r.test(content)
if violation == nil {
return
}
Expand All @@ -73,6 +75,9 @@ func (r ModerateNewsRule) OnMessage(ctx framework.MessageContext, channel *disco

// Send a direct message to the user
ctx.Session().ChannelMessageSend(dmChannel.ID, violation.Error())

// Send a copy of the original content to the user
sendOriginalMessageAsCodeblock(ctx, dmChannel.ID, content)
}

// Tests the rule against the content
Expand Down
7 changes: 6 additions & 1 deletion applications/rss_moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ func (r ModerateRSSRule) OnMessage(ctx framework.MessageContext, channel *discor
return
}

content := ctx.Message().Content

// Test the message content
violation := r.test(ctx.Message().Content)
violation := r.test(content)
if violation == nil {
return
}
Expand All @@ -73,6 +75,9 @@ func (r ModerateRSSRule) OnMessage(ctx framework.MessageContext, channel *discor

// Send a direct message to the user
ctx.Session().ChannelMessageSend(dmChannel.ID, violation.Error())

// Send a copy of the original content to the user
sendOriginalMessageAsCodeblock(ctx, dmChannel.ID, content)
}

// Tests the rule against the content
Expand Down
23 changes: 23 additions & 0 deletions applications/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package applications

import (
"strings"

"github.com/aussiebroadwan/tony/framework"
)

func sendOriginalMessageAsCodeblock(ctx framework.MessageContext, channelId, content string) {
// Convert all ` to ' to avoid code block formatting
content = strings.ReplaceAll(content, "`", "'")

// truncate the content to 2000 characters - the code block will add 6 characters plus the markdown label (2) and the newlines (2) = 10
if len(content) > 2000-10 {
content = content[:2000-10]
}

// Convert the content to a code block
content = "```md\n" + content + "\n```"

// Send a copy of the original content to the user
ctx.Session().ChannelMessageSend(channelId, content)
}

0 comments on commit a1cb311

Please sign in to comment.