Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using custom description in brainery post #11

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/bwmarrin/discordgo v0.26.1
github.com/gin-gonic/gin v1.8.2
github.com/joho/godotenv v1.4.0
github.com/k0kubun/pp/v3 v3.2.0
github.com/sashabaranov/go-openai v1.10.1
github.com/shopspring/decimal v1.3.1
github.com/sirupsen/logrus v1.9.0
Expand All @@ -26,6 +27,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -168,6 +170,8 @@ github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
20 changes: 14 additions & 6 deletions pkg/discord/command/brainery/brainery.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ func New(l logger.Logger, svc service.Servicer, view view.Viewer, cfg *config.Co
}

var (
discordIDRegexPattern = "<@(\\d+)>"
tagRegexPattern = "#(\\w+)"
icyRewardRegexPattern = " (\\d+)"
urlRegexPattern = "((?:https?://)[^\\s]+)"
githubRegexPattern = "gh:(\\w+)"
discordIDRegexPattern = `<@(\d+)>`
tagRegexPattern = `#(\w+)`
icyRewardRegexPattern = ` (\d+)`
urlRegexPattern = `((?:https?://)[^\s]+)`
githubRegexPattern = `gh:(\w+)`
descriptionRegexPattern = `d:"(.*?)"`
)

const defaultBraineryReward = "10"
const defaultBraineryReward = "0"

func (e *Brainery) Post(message *model.DiscordMessage) error {
targetChannelID := constant.DiscordBraineryChannel
Expand All @@ -53,6 +54,7 @@ func (e *Brainery) Post(message *model.DiscordMessage) error {
extractTags := extractPattern(rawFormattedContent, tagRegexPattern)
extractReward := extractPattern(rawFormattedContent, icyRewardRegexPattern)
extractGithub := extractPattern(rawFormattedContent, githubRegexPattern)
extractDesc := extractPattern(rawFormattedContent, descriptionRegexPattern)

if len(extractURL) == 0 || len(extractURL) > 1 {
return e.view.Error().Raise(message, "There is no URL or more than one URL in your message.")
Expand All @@ -76,9 +78,15 @@ func (e *Brainery) Post(message *model.DiscordMessage) error {
gh = extractGithub[0]
}

desc := ""
if len(extractDesc) > 0 {
desc = extractDesc[0]
}

mbrainery := &brainery.PostInput{
URL: extractURL[0],
DiscordID: extractDiscordID[0],
Description: desc,
Reward: reward,
PublishedAt: &now,
Tags: extractTags,
Expand Down
29 changes: 25 additions & 4 deletions pkg/discord/service/brainery/brainery.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PostInput struct {
URL string
Author string
Reward string
Description string
PublishedAt *time.Time
Tags []string
Github string
Expand Down Expand Up @@ -77,10 +78,30 @@ func (e *Brainery) Post(in *PostInput) (*model.Brainery, error) {
in.Github = metadata["github_id"]
}

description, err := e.adapter.OpenAI().SummarizeBraineryPost(string(body)[:3000])
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return nil, err
if in.Reward == "" {
in.Github = metadata["github_id"]
}

if in.Reward == "0" {
in.Reward = "10"
if metadata["reward"] != "" {
in.Reward = metadata["reward"]
}

}

description := in.Description
if description == "" {
maxlength := 3000
if len(string(body)) < maxlength {
maxlength = len(string(body))
}

description, err = e.adapter.OpenAI().SummarizeBraineryPost(string(body)[:maxlength])
if err != nil {
fmt.Printf("ChatCompletion error: %v\n", err)
return nil, err
}
}

mBrainery := &model.Brainery{
Expand Down