Remove discord code

This commit is contained in:
ekzyis 2024-04-07 05:42:05 +02:00
parent 24909f5d88
commit 35a4112c22
3 changed files with 5 additions and 229 deletions

View File

@ -1,188 +0,0 @@
package main
import (
"errors"
"fmt"
"log"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
sn "github.com/ekzyis/snappy"
"github.com/joho/godotenv"
"github.com/namsral/flag"
)
var (
DiscordToken string
dg *discordgo.Session
DiscordChannelId string
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
flag.StringVar(&DiscordToken, "DISCORD_TOKEN", "", "Discord bot token")
flag.StringVar(&DiscordChannelId, "DISCORD_CHANNEL_ID", "", "Discord channel id")
flag.Parse()
if DiscordToken == "" {
log.Fatal("DISCORD_TOKEN not set")
}
if DiscordChannelId == "" {
log.Fatal("DISCORD_CHANNEL_ID not set")
}
initBot()
}
func initBot() {
var err error
dg, err = discordgo.New("Bot " + DiscordToken)
if err != nil {
log.Fatal("error creating discord session:", err)
}
dg.AddHandler(func(s *discordgo.Session, event *discordgo.Ready) {
log.Println("Logged in as", event.User.Username)
})
dg.AddHandler(onMessage)
dg.AddHandler(onMessageReact)
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent | discordgo.IntentGuildMessageReactions
err = dg.Open()
if err != nil {
log.Fatal("error opening connection to discord: ", err, " -- Is your token correct?")
}
}
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
hackerNewsId, err := ParseHackerNewsLink(m.Content)
if err != nil {
return
}
story, err := FetchStoryById(hackerNewsId)
_, err = PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: false})
if err != nil {
var dupesErr *sn.DupesError
if errors.As(err, &dupesErr) {
SendDupesErrorToDiscord(hackerNewsId, dupesErr)
return
}
SendErrorToDiscord(err)
}
}
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 {
SendErrorToDiscord(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, err := FetchStoryById(id)
if err != nil {
SendErrorToDiscord(err)
return
}
id, err = PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: true})
if err != nil {
SendErrorToDiscord(err)
}
}
func SendDupesErrorToDiscord(hackerNewsId int, dupesErr *sn.DupesError) {
msg := fmt.Sprint(dupesErr)
log.Println(msg)
title := fmt.Sprintf("%d dupe(s) found for %s:", len(dupesErr.Dupes), dupesErr.Url)
color := 0xffc107
var fields []*discordgo.MessageEmbedField
for _, dupe := range dupesErr.Dupes {
fields = append(fields,
&discordgo.MessageEmbedField{
Name: "Title",
Value: dupe.Title,
Inline: false,
},
&discordgo.MessageEmbedField{
Name: "Id",
Value: fmt.Sprintf("https://stacker.news/items/%d", dupe.Id),
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Url",
Value: dupe.Url,
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "User",
Value: dupe.User.Name,
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Created",
Value: humanize.Time(dupe.CreatedAt),
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Sats",
Value: fmt.Sprint(dupe.Sats),
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Comments",
Value: fmt.Sprint(dupe.NComments),
Inline: true,
},
)
}
embed := discordgo.MessageEmbed{
Title: title,
Color: color,
Fields: fields,
Footer: &discordgo.MessageEmbedFooter{
Text: HackerNewsItemLink(hackerNewsId),
IconURL: "https://news.ycombinator.com/y18.gif",
},
}
SendEmbedToDiscord(&embed)
}
func SendEmbedToDiscord(embed *discordgo.MessageEmbed) {
_, err := dg.ChannelMessageSendEmbed(DiscordChannelId, embed)
if err != nil {
err = fmt.Errorf("error during sending embed: %w", err)
log.Println(err)
}
}
func SendErrorToDiscord(err error) {
msg := fmt.Sprint(err)
log.Println(msg)
embed := discordgo.MessageEmbed{
Title: "Error",
Color: 0xff0000,
Description: msg,
}
SendEmbedToDiscord(&embed)
}

10
main.go
View File

@ -17,11 +17,11 @@ func SyncStories() {
stories, err := FetchHackerNewsTopStories()
if err != nil {
SendErrorToDiscord(err)
log.Println(err)
continue
}
if err := SaveStories(&stories); err != nil {
SendErrorToDiscord(err)
log.Println(err)
continue
}
}
@ -41,7 +41,7 @@ func main() {
time.Sleep(dur)
if filtered, err = CurateContentForStackerNews(); err != nil {
SendErrorToDiscord(err)
log.Println(err)
continue
}
@ -55,11 +55,11 @@ func main() {
// save dupe in db to prevent retries
parentId := dupesErr.Dupes[0].Id
if err := SaveSnItem(parentId, story.ID); err != nil {
SendErrorToDiscord(err)
log.Println(err)
}
continue
}
SendErrorToDiscord(err)
log.Println(err)
continue
}
}

36
sn.go
View File

@ -6,7 +6,6 @@ import (
"log"
"time"
"github.com/bwmarrin/discordgo"
"github.com/dustin/go-humanize"
sn "github.com/ekzyis/snappy"
)
@ -95,40 +94,5 @@ func PostStoryToStackerNews(story *Story, options PostStoryOptions) (int, error)
return -1, err
}
SendStackerNewsEmbedToDiscord(story.Title, parentId)
return parentId, nil
}
func SendStackerNewsEmbedToDiscord(title string, id int) {
Timestamp := time.Now().Format(time.RFC3339)
url := fmt.Sprintf("https://stacker.news/items/%d", id)
color := 0xffc107
embed := discordgo.MessageEmbed{
Title: title,
URL: url,
Color: color,
Footer: &discordgo.MessageEmbedFooter{
Text: "Stacker News",
IconURL: "https://stacker.news/favicon.png",
},
Timestamp: Timestamp,
}
SendEmbedToDiscord(&embed)
}
func SendNotificationsEmbedToDiscord() {
Timestamp := time.Now().Format(time.RFC3339)
color := 0xffc107
embed := discordgo.MessageEmbed{
Title: "new notifications",
URL: "https://stacker.news/hn/posts",
Color: color,
Footer: &discordgo.MessageEmbedFooter{
Text: "Stacker News",
IconURL: "https://stacker.news/favicon-notify.png",
},
Timestamp: Timestamp,
}
SendEmbedToDiscord(&embed)
}