-
Notifications
You must be signed in to change notification settings - Fork 3
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
Staging #69
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
2c99224
ent birthday placeholder
1nv8rzim ee83c42
data birthday placeholder
1nv8rzim 9b9fc7d
added birthday slash command placeholder
1nv8rzim 4ae8058
place holder for birthday scheduled event
1nv8rzim ddc71e1
add span for birthday slash command
1nv8rzim 6ef64d9
ent schema
1nv8rzim 4b6cfb1
generate ent schema
1nv8rzim a84d745
added started code for data.Birthday
1nv8rzim 9cec6b1
started code for scheduled eent
1nv8rzim 9bafc14
starter code for birthday slash command
1nv8rzim c45fb34
Merge pull request #1 from 1nv8rzim/ent-schema
1nv8rzim ccc7c0e
Midnight loop
hfw8271 cdfb724
Cron-ed - still working on it
hfw8271 22aaad6
birthday abstractions
1nv8rzim ac5f264
Merge pull request #65 from 1nv8rzim/data-abstraction
1nv8rzim c53ca66
Merge pull request #2 from 1nv8rzim/data-abstraction
1nv8rzim e882bd4
Merge branch 'ritsec:main' into scheduled-event
1nv8rzim 3929c03
bday add/remove
hfw8271 3e07fe5
updated slash command for birthday-add
praneeth919 a7faff3
remove unnessary comments
1nv8rzim e45ffc3
added removal functionality
hfw8271 192d02c
added comments
1nv8rzim 06d8635
added error checking
1nv8rzim e3cc964
Merge pull request #3 from 1nv8rzim/slash-command
1nv8rzim 79089e5
replaced birthday role ID with reference to config file
1nv8rzim 427886a
fixed errors and cleaned up code
1nv8rzim 5bb29dd
logging and logic clean up
1nv8rzim c8350dc
Merge pull request #4 from 1nv8rzim/scheduled-event
1nv8rzim c963c21
commited wacky code birthday.go
sophduke 38e5daa
Clean Up Implementation
1nv8rzim 48d6694
Merge pull request #5 from 1nv8rzim/sophduke-patch-1
1nv8rzim 2c93a88
Add birthday date validation
1nv8rzim 77b3569
Merge pull request #67 from 1nv8rzim/main
1nv8rzim b55ccbf
updated config example
1nv8rzim 352a44e
combined birthday commands into subcommands
1nv8rzim 4189b1d
fixed error checking and handled edge cases
1nv8rzim 575eedc
Revert "Data abstraction"
1nv8rzim 9f46a4d
Working post rebase
1nv8rzim 13c6991
Merge pull request #68 from ritsec/birthday-patch
1nv8rzim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
// 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 | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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