Skip to content

Commit

Permalink
fix: forward create pr event (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
quanghuynguyen1902 authored Jul 29, 2024
1 parent 48ad335 commit 22ed95f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type DiscordIds struct {
FortressBot string
DwarvesGuild string
RepostDoneChannel string
RandomChannel string
DevChannel string
}

type ApiServer struct {
Expand Down Expand Up @@ -83,6 +85,8 @@ func Generate(v ENV) *Config {
FortressBot: v.GetString("DISCORD_ID_FORTRESS_BOT"),
DwarvesGuild: v.GetString("DISCORD_ID_DWARVES_GUILD"),
RepostDoneChannel: v.GetString("DISCORD_ID_REPOST_DONE"),
RandomChannel: v.GetString("DISCORD_ID_RANDOM_CHANNEL"),
DevChannel: v.GetString("DISCORD_ID_DEV_CHANNEL"),
},
WhiteListedChannels: v.GetString("DISCORD_WHITELISTED_CHANNELS"),
},
Expand Down
50 changes: 50 additions & 0 deletions pkg/discord/listener.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package discord

import (
"log"
"strings"

"github.com/bwmarrin/discordgo"
Expand All @@ -10,6 +11,10 @@ import (

// onMessageCreate is an entry point for discord message create (chat) event
func (d *Discord) onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {

// Forward pull request messages from the "dev" channel to the "random" channel
d.forwardPullRequestMessage(s, m)

// this will ignore message send from bot itself
if m.Author.ID == s.State.User.ID {
return
Expand Down Expand Up @@ -148,3 +153,48 @@ func (d *Discord) onGuildScheduledEventCreate(s *discordgo.Session, m *discordgo
d.L.Error(err, "failed to create a scheduled event on discord")
}
}

func (d *Discord) forwardPullRequestMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Check if message is from the "dev" channel
if m.ChannelID != d.Cfg.Discord.ID.DevChannel {
return
}

// check message create from bot
if m.Author != nil && !m.Author.Bot {
return
}

content := getDiscordEmbedTitle(m.Message)

// Check if message content indicates a new pull request
if !strings.Contains(content, "Pull request opened:") {
return
}

newMessage := &discordgo.MessageSend{
Embeds: m.Embeds,
}

// Send the message to the "random" channel
_, err := s.ChannelMessageSendComplex(d.Cfg.Discord.ID.RandomChannel, newMessage)
if err != nil {
log.Printf("Error sending message to random channel: %v", err)
}
}

func getDiscordEmbedTitle(m *discordgo.Message) string {
if len(m.Embeds) == 0 {
return ""
}

// Assuming we're interested in the first embed
embed := m.Embeds[0]

// If no description, check for title
if embed.Title != "" {
return embed.Title
}

return ""
}

0 comments on commit 22ed95f

Please sign in to comment.