Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staging #69

Merged
merged 39 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2c99224
ent birthday placeholder
1nv8rzim Oct 18, 2023
ee83c42
data birthday placeholder
1nv8rzim Oct 18, 2023
9b9fc7d
added birthday slash command placeholder
1nv8rzim Oct 18, 2023
4ae8058
place holder for birthday scheduled event
1nv8rzim Oct 18, 2023
ddc71e1
add span for birthday slash command
1nv8rzim Oct 18, 2023
6ef64d9
ent schema
1nv8rzim Oct 18, 2023
4b6cfb1
generate ent schema
1nv8rzim Oct 18, 2023
a84d745
added started code for data.Birthday
1nv8rzim Oct 18, 2023
9cec6b1
started code for scheduled eent
1nv8rzim Oct 18, 2023
9bafc14
starter code for birthday slash command
1nv8rzim Oct 18, 2023
c45fb34
Merge pull request #1 from 1nv8rzim/ent-schema
1nv8rzim Oct 18, 2023
ccc7c0e
Midnight loop
hfw8271 Oct 19, 2023
cdfb724
Cron-ed - still working on it
hfw8271 Oct 30, 2023
22aaad6
birthday abstractions
1nv8rzim Nov 8, 2023
ac5f264
Merge pull request #65 from 1nv8rzim/data-abstraction
1nv8rzim Nov 8, 2023
c53ca66
Merge pull request #2 from 1nv8rzim/data-abstraction
1nv8rzim Nov 8, 2023
e882bd4
Merge branch 'ritsec:main' into scheduled-event
1nv8rzim Nov 8, 2023
3929c03
bday add/remove
hfw8271 Nov 8, 2023
3e07fe5
updated slash command for birthday-add
praneeth919 Nov 8, 2023
a7faff3
remove unnessary comments
1nv8rzim Nov 15, 2023
e45ffc3
added removal functionality
hfw8271 Nov 15, 2023
192d02c
added comments
1nv8rzim Nov 15, 2023
06d8635
added error checking
1nv8rzim Nov 15, 2023
e3cc964
Merge pull request #3 from 1nv8rzim/slash-command
1nv8rzim Nov 15, 2023
79089e5
replaced birthday role ID with reference to config file
1nv8rzim Nov 15, 2023
427886a
fixed errors and cleaned up code
1nv8rzim Nov 15, 2023
5bb29dd
logging and logic clean up
1nv8rzim Nov 15, 2023
c8350dc
Merge pull request #4 from 1nv8rzim/scheduled-event
1nv8rzim Nov 15, 2023
c963c21
commited wacky code birthday.go
sophduke Dec 3, 2023
38e5daa
Clean Up Implementation
1nv8rzim Dec 3, 2023
48d6694
Merge pull request #5 from 1nv8rzim/sophduke-patch-1
1nv8rzim Dec 3, 2023
2c93a88
Add birthday date validation
1nv8rzim Dec 3, 2023
77b3569
Merge pull request #67 from 1nv8rzim/main
1nv8rzim Dec 3, 2023
b55ccbf
updated config example
1nv8rzim Dec 6, 2023
352a44e
combined birthday commands into subcommands
1nv8rzim Dec 6, 2023
4189b1d
fixed error checking and handled edge cases
1nv8rzim Dec 6, 2023
575eedc
Revert "Data abstraction"
1nv8rzim Nov 8, 2023
9f46a4d
Working post rebase
1nv8rzim Dec 6, 2023
13c6991
Merge pull request #68 from ritsec/birthday-patch
1nv8rzim Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions commands/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func populateSlashCommands(ctx ddtrace.SpanContext) {
SlashCommands["update"] = slash.Update
SlashCommands["query"] = slash.Query
SlashCommands["scoreboard"] = slash.Scoreboard
SlashCommands["birthday"] = slash.Birthday
}

// populateHandlers populates the Handlers map with all of the handlers
Expand Down Expand Up @@ -68,4 +69,5 @@ func populateScheduledEvents(ctx ddtrace.SpanContext) {
ScheduledEvents["heartbeat"] = scheduled.Heartbeat
ScheduledEvents["status"] = scheduled.Status
ScheduledEvents["update"] = scheduled.Update
ScheduledEvents["birthday"] = scheduled.Birthday
}
116 changes: 116 additions & 0 deletions commands/scheduled/birthday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package scheduled

import (
"time"

"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/config"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/robfig/cron"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

var (
birthdayRoleID string = config.GetString("commands.birthday.role_id")
)

// removeBirthday removes the birthday role from a user
func removeBirthday(s *discordgo.Session, UserID string, ctx ddtrace.SpanContext) error {
span := tracer.StartSpan(
"commands.scheduled.birthday:removeBirthday",
tracer.ResourceName("Scheduled.Birthday.removeBirthday"),
tracer.ChildOf(ctx),
)
defer span.Finish()

return s.GuildMemberRoleRemove(config.GuildID, UserID, birthdayRoleID)
}

// addBirthday adds the birthday role to a user
func addBirthday(s *discordgo.Session, UserID string, ctx ddtrace.SpanContext) error {
span := tracer.StartSpan(
"commands.scheduled.birthday:addBirthday",
tracer.ResourceName("Scheduled.Birthday.addBirthday"),
tracer.ChildOf(ctx),
)
defer span.Finish()

return s.GuildMemberRoleAdd(config.GuildID, UserID, birthdayRoleID)
}

// Birthday is a scheduled event that runs at midnight to remove existing birthday roles and add new ones
func Birthday(s *discordgo.Session, quit chan interface{}) error {
span := tracer.StartSpan(
"commands.scheduled.birthday:Birthday",
tracer.ResourceName("Scheduled.Birthday"),
)
defer span.Finish()

// Set the cron job to run at EST
est, err := time.LoadLocation("America/New_York")
if err != nil {
logging.Error(s, err.Error(), nil, span)
return err
}

c := cron.NewWithLocation(est)

// Run at midnight
err = c.AddFunc("0 0 0 * * *", func() {
internalSpan := tracer.StartSpan(
"commands.scheduled.birthday:Birthday.Cron",
tracer.ResourceName("Scheduled.Birthday.Cron"),
tracer.ChildOf(span.Context()),
)
defer internalSpan.Finish()

today := time.Now()
yesterday := today.Add(-24 * time.Hour)

// Get yesterday's birthdays
entRemoveBirthdays, err := data.Birthday.GetBirthdays(yesterday.Day(), int(yesterday.Month()), internalSpan.Context())
if err != nil {
logging.Error(s, "failed to get yesterday's birthdays", nil, span, logrus.Fields{"error": err})
return
}

// Remove yesterday's birthdays
for _, entRemoveBirthday := range entRemoveBirthdays {
err = removeBirthday(s, entRemoveBirthday.Edges.User.ID, internalSpan.Context())
if err != nil {
logging.Error(s, "failed to remove birthday for "+helpers.AtUser(entRemoveBirthday.Edges.User.ID), nil, span, logrus.Fields{"error": err})
return
}
}
Comment on lines +75 to +88
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edge/Exploit Case: I move my birthday on my birthday. I wouldn't come up here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the scheduled task that adds and removes birthdays at midnight.

This edge case is covered in the snippets commented below


// Get today's birthdays
entAddBirthday, err := data.Birthday.GetBirthdays(today.Day(), int(today.Month()), internalSpan.Context())
if err != nil {
logging.Error(s, "failed to get today's birthdays", nil, span, logrus.Fields{"error": err})
return
}

// Add today's birthdays
for _, entAddBirthday := range entAddBirthday {
err = addBirthday(s, entAddBirthday.Edges.User.ID, internalSpan.Context())
if err != nil {
logging.Error(s, "failed to add birthday for "+helpers.AtUser(entAddBirthday.Edges.User.ID), nil, span, logrus.Fields{"error": err})
return
}
}
})
if err != nil {
logging.Error(s, "failed to create cron job", nil, span, logrus.Fields{"error": err})
return err
}

c.Start()
<-quit
c.Stop()

return nil
}
Loading
Loading