hnbot/discord.go

132 lines
3.0 KiB
Go
Raw Normal View History

2023-04-19 20:48:24 +00:00
package main
import (
2023-04-24 22:42:11 +00:00
"errors"
"fmt"
2023-04-19 20:48:24 +00:00
"log"
"github.com/bwmarrin/discordgo"
2023-04-24 22:42:11 +00:00
"github.com/dustin/go-humanize"
2023-04-19 20:48:24 +00:00
"github.com/joho/godotenv"
"github.com/namsral/flag"
)
var (
2023-04-24 23:57:37 +00:00
DiscordToken string
DiscordClient *discordgo.Session
DiscordChannelId string
2023-04-19 20:48:24 +00:00
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
flag.StringVar(&DiscordToken, "DISCORD_TOKEN", "", "Discord bot token")
2023-04-24 23:57:37 +00:00
flag.StringVar(&DiscordChannelId, "DISCORD_CHANNEL_ID", "", "Discord channel id")
2023-04-19 20:48:24 +00:00
flag.Parse()
if DiscordToken == "" {
log.Fatal("DISCORD_TOKEN not set")
}
2023-04-24 23:57:37 +00:00
if DiscordChannelId == "" {
log.Fatal("DISCORD_CHANNEL_ID not set")
}
initBot()
}
func initBot() {
var err error
2023-04-24 23:57:37 +00:00
DiscordClient, err = discordgo.New("Bot " + DiscordToken)
if err != nil {
log.Fatal("error creating discord session:", err)
}
DiscordClient.AddHandler(func(s *discordgo.Session, event *discordgo.Ready) {
log.Println("Logged in as", event.User.Username)
})
DiscordClient.AddHandler(onMessage)
DiscordClient.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent
err = DiscordClient.Open()
if err != nil {
log.Fatal("error opening connection to discord: ", err, " -- Is your token correct?")
}
}
func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
id, err := ParseHackerNewsLink(m.Content)
if err != nil {
return
}
story := FetchStoryById(id)
2023-04-24 22:42:11 +00:00
id, err = PostStoryToStackerNews(&story)
if err != nil {
var dupesErr *DupesError
if errors.As(err, &dupesErr) {
SendDupesErrorToDiscord(dupesErr)
} else {
log.Fatal("unexpected error returned")
}
}
}
func SendDupesErrorToDiscord(dupesErr *DupesError) {
title := fmt.Sprintf("%d dupe(s) found for %s:", len(dupesErr.Dupes), dupesErr.Url)
color := 0xffc107
2023-04-24 23:57:37 +00:00
var fields []*discordgo.MessageEmbedField
2023-04-24 22:42:11 +00:00
for _, dupe := range dupesErr.Dupes {
fields = append(fields,
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Title",
Value: dupe.Title,
Inline: false,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Id",
Value: StackerNewsItemLink(dupe.Id),
Inline: true,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Url",
Value: dupe.Url,
Inline: true,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "User",
Value: dupe.User.Name,
Inline: true,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Created",
Value: humanize.Time(dupe.CreatedAt),
Inline: true,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Sats",
Value: fmt.Sprint(dupe.Sats),
Inline: true,
},
2023-04-24 23:57:37 +00:00
&discordgo.MessageEmbedField{
2023-04-24 22:42:11 +00:00
Name: "Comments",
Value: fmt.Sprint(dupe.NComments),
Inline: true,
},
)
}
2023-04-24 23:57:37 +00:00
embed := discordgo.MessageEmbed{
2023-04-24 22:42:11 +00:00
Title: title,
Color: color,
Fields: fields,
}
2023-04-24 23:57:37 +00:00
SendEmbedToDiscord(&embed)
2023-04-19 20:48:24 +00:00
}
2023-04-24 23:57:37 +00:00
func SendEmbedToDiscord(embed *discordgo.MessageEmbed) {
_, err := DiscordClient.ChannelMessageSendEmbed(DiscordChannelId, embed)
2023-04-19 20:48:24 +00:00
if err != nil {
log.Fatal("Error during json.Marshal:", err)
}
}