Skip dupes check on skip reaction
This commit is contained in:
parent
8eaaaeab3e
commit
c2b6e77751
48
discord.go
48
discord.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
|
@ -44,7 +45,8 @@ func initBot() {
|
||||||
log.Println("Logged in as", event.User.Username)
|
log.Println("Logged in as", event.User.Username)
|
||||||
})
|
})
|
||||||
dg.AddHandler(onMessage)
|
dg.AddHandler(onMessage)
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent
|
dg.AddHandler(onMessageReact)
|
||||||
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent | discordgo.IntentGuildMessageReactions
|
||||||
err = dg.Open()
|
err = dg.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("error opening connection to discord: ", err, " -- Is your token correct?")
|
log.Fatal("error opening connection to discord: ", err, " -- Is your token correct?")
|
||||||
|
@ -56,23 +58,53 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
id, err := ParseHackerNewsLink(m.Content)
|
hackerNewsId, err := ParseHackerNewsLink(m.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
story := FetchStoryById(id)
|
story := FetchStoryById(hackerNewsId)
|
||||||
id, err = PostStoryToStackerNews(&story)
|
_, err = PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: false})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
var dupesErr *DupesError
|
var dupesErr *DupesError
|
||||||
if errors.As(err, &dupesErr) {
|
if errors.As(err, &dupesErr) {
|
||||||
SendDupesErrorToDiscord(dupesErr)
|
SendDupesErrorToDiscord(hackerNewsId, dupesErr)
|
||||||
} else {
|
} else {
|
||||||
log.Fatal("unexpected error returned")
|
log.Fatal("unexpected error returned")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendDupesErrorToDiscord(dupesErr *DupesError) {
|
func onMessageReact(s *discordgo.Session, reaction *discordgo.MessageReactionAdd) {
|
||||||
|
if reaction.UserID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if reaction.Emoji.Name != "⏭️" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m, err := s.ChannelMessage(reaction.ChannelID, reaction.MessageID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(m.Embeds) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
embed := m.Embeds[0]
|
||||||
|
if !strings.Contains(embed.Title, "dupe(s) found for") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
id, err := ParseHackerNewsLink(embed.Footer.Text)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
story := FetchStoryById(id)
|
||||||
|
id, err = PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: true})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("unexpected error returned")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendDupesErrorToDiscord(hackerNewsId int, dupesErr *DupesError) {
|
||||||
title := fmt.Sprintf("%d dupe(s) found for %s:", len(dupesErr.Dupes), dupesErr.Url)
|
title := fmt.Sprintf("%d dupe(s) found for %s:", len(dupesErr.Dupes), dupesErr.Url)
|
||||||
color := 0xffc107
|
color := 0xffc107
|
||||||
var fields []*discordgo.MessageEmbedField
|
var fields []*discordgo.MessageEmbedField
|
||||||
|
@ -119,6 +151,10 @@ func SendDupesErrorToDiscord(dupesErr *DupesError) {
|
||||||
Title: title,
|
Title: title,
|
||||||
Color: color,
|
Color: color,
|
||||||
Fields: fields,
|
Fields: fields,
|
||||||
|
Footer: &discordgo.MessageEmbedFooter{
|
||||||
|
Text: HackerNewsItemLink(hackerNewsId),
|
||||||
|
IconURL: "https://news.ycombinator.com/y18.gif",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
SendEmbedToDiscord(&embed)
|
SendEmbedToDiscord(&embed)
|
||||||
}
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -7,7 +7,7 @@ func main() {
|
||||||
stories := FetchHackerNewsTopStories()
|
stories := FetchHackerNewsTopStories()
|
||||||
filtered := CurateContentForStackerNews(&stories)
|
filtered := CurateContentForStackerNews(&stories)
|
||||||
for _, story := range *filtered {
|
for _, story := range *filtered {
|
||||||
PostStoryToStackerNews(&story)
|
PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: false})
|
||||||
}
|
}
|
||||||
time.Sleep(time.Hour)
|
time.Sleep(time.Hour)
|
||||||
}
|
}
|
||||||
|
|
16
sn.go
16
sn.go
|
@ -164,11 +164,17 @@ func FetchStackerNewsDupes(url string) *[]Dupe {
|
||||||
return &dupesResp.Data.Dupes
|
return &dupesResp.Data.Dupes
|
||||||
}
|
}
|
||||||
|
|
||||||
func PostStoryToStackerNews(story *Story) (int, error) {
|
type PostStoryOptions struct {
|
||||||
dupes := FetchStackerNewsDupes(story.Url)
|
SkipDupes bool
|
||||||
if len(*dupes) > 0 {
|
}
|
||||||
log.Printf("%s was already posted. Skipping.\n", story.Url)
|
|
||||||
return -1, &DupesError{story.Url, *dupes}
|
func PostStoryToStackerNews(story *Story, options PostStoryOptions) (int, error) {
|
||||||
|
if !options.SkipDupes {
|
||||||
|
dupes := FetchStackerNewsDupes(story.Url)
|
||||||
|
if len(*dupes) > 0 {
|
||||||
|
log.Printf("%s was already posted. Skipping.\n", story.Url)
|
||||||
|
return -1, &DupesError{story.Url, *dupes}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body := GraphQLPayload{
|
body := GraphQLPayload{
|
||||||
|
|
Loading…
Reference in New Issue