Skip to content

Commit

Permalink
Tech-News Moderation (#38)
Browse files Browse the repository at this point in the history
* feat: send original message as md codeblock

* test: tested and added additional formatting
  • Loading branch information
lcox74 authored Aug 7, 2024
1 parent 6cd00e5 commit 8c70952
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
9 changes: 7 additions & 2 deletions 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 @@ -72,7 +74,10 @@ func (r ModerateNewsRule) OnMessage(ctx framework.MessageContext, channel *disco
}

// Send a direct message to the user
ctx.Session().ChannelMessageSend(dmChannel.ID, violation.Error())
ctx.Session().ChannelMessageSend(dmChannel.ID, "**Error**: "+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
9 changes: 7 additions & 2 deletions 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 @@ -72,7 +74,10 @@ func (r ModerateRSSRule) OnMessage(ctx framework.MessageContext, channel *discor
}

// Send a direct message to the user
ctx.Session().ChannelMessageSend(dmChannel.ID, violation.Error())
ctx.Session().ChannelMessageSend(dmChannel.ID, "**Error**: "+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 8c70952

Please sign in to comment.