Skip to content

Commit

Permalink
Merge pull request #3 from dustin-ward/Dev
Browse files Browse the repository at this point in the history
2023 Update
  • Loading branch information
dustin-ward committed Nov 26, 2023
2 parents d3a18d4 + 5317355 commit 680e619
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
8 changes: 5 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"time"

"dustin-ward/AdventOfCodeBot/data"

Expand All @@ -14,7 +15,7 @@ import (
)

var s *discordgo.Session
var C *map[string]*data.Channel
var C map[string]*data.Channel
var crn *cron.Cron
var adminPerm int64 = 0

Expand Down Expand Up @@ -107,7 +108,7 @@ func InitSession() (*discordgo.Session, error) {
if _, err := os.Stat("./channels.json"); errors.Is(err, os.ErrNotExist) {
log.Println("Info: no channel config file found")

*C = make(map[string]*data.Channel, 3)
C = make(map[string]*data.Channel, 3)
} else {
// Read channel configs from file (Not an ideal storage method...)
b, err := os.ReadFile("./channels.json")
Expand All @@ -126,6 +127,7 @@ func InitSession() (*discordgo.Session, error) {
}

func TakeDown() error {
log.Println("Shutting Down...")
crn.Stop()

// Save channel configurations
Expand Down Expand Up @@ -153,7 +155,7 @@ func RegisterCommands() ([]*discordgo.ApplicationCommand, error) {
}

func SetupNotifications() error {
crn = cron.New()
crn = cron.NewWithLocation(time.UTC)

// Cronjob for 4:30am UTC (11:30pm EST)
if err := crn.AddFunc("0 30 4 * * *", problemNotification); err != nil {
Expand Down
33 changes: 26 additions & 7 deletions bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func leaderboard(s *discordgo.Session, i *discordgo.InteractionCreate) {
channel, err := getChannel(i.GuildID)
if err != nil {
log.Println("Error:", fmt.Errorf("leaderboard: %v", err))
respondWithError(s, i, "Your server has not been correctly configured!")
respondWithError(s, i, "Your server has not been correctly configured! 🛠️ Use /configure-server")
return
}

Expand Down Expand Up @@ -84,9 +84,9 @@ func leaderboard(s *discordgo.Session, i *discordgo.InteractionCreate) {
// Create embed object
embeds := make([]*discordgo.MessageEmbed, 1)
embeds[0] = &discordgo.MessageEmbed{
URL: "https://adventofcode.com/2022/private/view/" + channel.Leaderboard,
URL: "https://adventofcode.com/2023/leaderboard/private/view/" + channel.Leaderboard,
Type: discordgo.EmbedTypeRich,
Title: "🎄 2022 Leaderboard 🎄",
Title: "🎄 2023 Leaderboard 🎄",
Color: 0x127C06,
Footer: &discordgo.MessageEmbedFooter{
Text: "Leaderboard as of " + time.Now().Format("2006/01/02 3:4:5pm"),
Expand Down Expand Up @@ -126,7 +126,7 @@ func configure(s *discordgo.Session, i *discordgo.InteractionCreate) {
}

// Add to local memory
(*C)[i.GuildID] = &ch
C[i.GuildID] = &ch

// Write to file
b, err := json.Marshal(C)
Expand All @@ -142,6 +142,13 @@ func configure(s *discordgo.Session, i *discordgo.InteractionCreate) {
return
}

log.Println("Attempting to fetch data for leaderboard " + ch.Leaderboard + "...")
if err := data.FetchData(ch.Leaderboard, ch.SessionToken, ch.Leaderboard); err != nil {
log.Println("Error:", fmt.Errorf("fetch: %w", err))
} else {
log.Println(ch.Leaderboard, "success!")
}

respond(s, i, "Server successfully configured!")
}

Expand All @@ -151,7 +158,8 @@ func startCountdown(s *discordgo.Session, i *discordgo.InteractionCreate) {
ch, err := getChannel(i.GuildID)
if err != nil {
log.Println("Error:", fmt.Errorf("start-notifications: %v", err))
respondWithError(s, i, "Error: Internal server error...")
respondWithError(s, i, "Your server has not been correctly configured! 🛠️ Use /configure-server")
return
}
ch.NotificationsOn = true

Expand All @@ -164,7 +172,8 @@ func stopCountdown(s *discordgo.Session, i *discordgo.InteractionCreate) {
ch, err := getChannel(i.GuildID)
if err != nil {
log.Println("Error:", fmt.Errorf("start-notifications: %v", err))
respondWithError(s, i, "Error: Internal server error...")
respondWithError(s, i, "Your server has not been correctly configured! 🛠️ Use /configure-server")
return
}
ch.NotificationsOn = false

Expand All @@ -176,11 +185,21 @@ func checkCountdown(s *discordgo.Session, i *discordgo.InteractionCreate) {
ch, err := getChannel(i.GuildID)
if err != nil {
log.Println("Error:", fmt.Errorf("check-notifications: %w", err))
respondWithError(s, i, "Your server has not been correctly configured! 🛠️ Use /configure-server")
return
}

next, err := NextNotification()
if err != nil {
log.Println("Error:", fmt.Errorf("check-notifications: %w", err))
respondWithError(s, i, "Internal Error 💀 Please contact @shrublord")
return
}
day := time.Now().AddDate(0, 0, 1).Day()

var message string
if ch.NotificationsOn {
message = fmt.Sprintf("Notifications for server id: %s are enabled in channel: %s!", ch.GuildId, ch.ChannelId)
message = fmt.Sprintf("Notifications for server id: %s are enabled in channel: %s!\n\n⏰ Next notification: <t:%d:R> (Day %d)", ch.GuildId, ch.ChannelId, next.Unix(), day)
} else {
message = "Notifications are not enabled currently..."
}
Expand Down
16 changes: 12 additions & 4 deletions bot/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import (
)

const (
ProblemUrl = "https://adventofcode.com/2022/day/"
ProblemUrl = "https://adventofcode.com/2023/day/"
)

func problemNotification() {
day := time.Now().AddDate(0, 0, 1).Day() - 1
day := time.Now().AddDate(0, 0, 1).Day()

// For each registered channel
for _, ch := range *C {
for _, ch := range C {
if ch.NotificationsOn {
log.Println("Info: sending day", day, "notification in channel", ch.ChannelId)

// Create message object
messageString := fmt.Sprintf(
"🎄 <@&%s> 🎄\nThe problem for Day %d will be released soon! (<t:%d:R>)\nYou can see the problem statement here when its up: https://adventofcode.com/2022/day/%d",
"🎄 <@&%s> 🎄\nThe problem for Day %d will be released soon! (<t:%d:R>)\nYou can see the problem statement here when its up: https://adventofcode.com/2023/day/%d",
ch.RoleId,
day,
time.Now().Unix()+(int64(30)*60),
Expand All @@ -37,3 +37,11 @@ func problemNotification() {
}
}
}

func NextNotification() (time.Time, error) {
entries := crn.Entries()
if len(entries) != 1 {
return time.Now(), fmt.Errorf("invalid number of cron entries")
}
return (*entries[0]).Next, nil
}
2 changes: 1 addition & 1 deletion bot/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func getChannel(guildId string) (*data.Channel, error) {
ch, ok := (*C)[guildId]
ch, ok := C[guildId]
if !ok {
return nil, fmt.Errorf("channel not found")
}
Expand Down
2 changes: 1 addition & 1 deletion data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
AocURL = "https://adventofcode.com/2022/leaderboard/private/view/"
AocURL = "https://adventofcode.com/2023/leaderboard/private/view/"
)

func GetData(boardId string) (*Data, error) {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
log.Fatal("Fatal:", fmt.Errorf("main: %w", err))
}
defer Session.Close()
log.Println("Session initialized for", len(*bot.C), "servers")
log.Println("Session initialized for", len(bot.C), "servers")

// Register commands
r, err := bot.RegisterCommands()
Expand All @@ -51,7 +51,7 @@ func main() {
}

// Continually fetch advent of code data every 15 minutes
for _, ch := range *bot.C {
for _, ch := range bot.C {
go func(channel *data.Channel) {
for {
log.Println("Attempting to fetch data for leaderboard " + channel.Leaderboard + "...")
Expand Down

0 comments on commit 680e619

Please sign in to comment.