-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from 1nv8rzim/data-abstraction
Data abstraction
- Loading branch information
Showing
28 changed files
with
3,192 additions
and
19 deletions.
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,19 @@ | ||
package scheduled | ||
|
||
import ( | ||
"github.com/bwmarrin/discordgo" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// 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() | ||
|
||
// code here | ||
|
||
return nil | ||
} |
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,118 @@ | ||
package slash | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/ritsec/ops-bot-iii/commands/slash/permission" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// Birthday is a slash command that allows users to add or edit their birthday | ||
func Birthday() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) { | ||
var minValue float64 = 1 | ||
return &discordgo.ApplicationCommand{ | ||
Name: "birthday", | ||
Description: "Add or edit birthday", | ||
DefaultMemberPermissions: &permission.Member, | ||
Options: []*discordgo.ApplicationCommandOption{ | ||
{ | ||
Type: discordgo.ApplicationCommandOptionInteger, | ||
Name: "month", | ||
Required: true, | ||
Choices: []*discordgo.ApplicationCommandOptionChoice{ | ||
{ | ||
Name: "January", | ||
Value: 1, | ||
}, | ||
{ | ||
Name: "February", | ||
Value: 2, | ||
}, | ||
{ | ||
Name: "March", | ||
Value: 3, | ||
}, | ||
{ | ||
Name: "April", | ||
Value: 4, | ||
}, | ||
{ | ||
Name: "May", | ||
Value: 5, | ||
}, | ||
{ | ||
Name: "June", | ||
Value: 6, | ||
}, | ||
{ | ||
Name: "July", | ||
Value: 7, | ||
}, | ||
{ | ||
Name: "August", | ||
Value: 8, | ||
}, | ||
{ | ||
Name: "September", | ||
Value: 9, | ||
}, | ||
{ | ||
Name: "October", | ||
Value: 10, | ||
}, | ||
{ | ||
Name: "November", | ||
Value: 11, | ||
}, | ||
{ | ||
Name: "December", | ||
Value: 12, | ||
}, | ||
}, | ||
}, | ||
{ | ||
Type: discordgo.ApplicationCommandOptionInteger, | ||
Name: "day", | ||
Required: true, | ||
MinValue: &minValue, | ||
MaxValue: 31, | ||
}, | ||
}, | ||
}, | ||
func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
span := tracer.StartSpan( | ||
"commands.slash.birthday:Birthday", | ||
tracer.ResourceName("/birthday"), | ||
) | ||
defer span.Finish() | ||
|
||
month := int(i.ApplicationCommandData().Options[0].IntValue()) | ||
day := int(i.ApplicationCommandData().Options[1].IntValue()) | ||
|
||
// remove print statement (just here not to cause unused variable error) | ||
fmt.Println(month, day) | ||
|
||
// code here | ||
|
||
} | ||
} | ||
|
||
// BirthdayRemove is a slash command that allows users to remove their birthday | ||
func BirthdayRemove() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) { | ||
return &discordgo.ApplicationCommand{ | ||
Name: "birthday_remove", | ||
Description: "Remove a birthday", | ||
DefaultMemberPermissions: &permission.Member, | ||
}, | ||
func(s *discordgo.Session, i *discordgo.InteractionCreate) { | ||
span := tracer.StartSpan( | ||
"commands.slash.birthday:BirthdayRemove", | ||
tracer.ResourceName("/birthday remove"), | ||
) | ||
defer span.Finish() | ||
|
||
// code here | ||
|
||
} | ||
} |
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,108 @@ | ||
package data | ||
|
||
import ( | ||
"github.com/ritsec/ops-bot-iii/ent" | ||
"github.com/ritsec/ops-bot-iii/ent/birthday" | ||
"github.com/ritsec/ops-bot-iii/ent/user" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
// Birthday is the interface for interacting with the birthday table | ||
type birthday_s struct{} | ||
|
||
// Exists checks if a birthday exists for a user | ||
func (*birthday_s) Exists(user_id string, ctx ddtrace.SpanContext) (bool, error) { | ||
span := tracer.StartSpan( | ||
"data.birthday:Exists", | ||
tracer.ResourceName("Data.Birthday.Exists"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
return Client.Birthday.Query(). | ||
Where( | ||
birthday.HasUserWith( | ||
user.ID(user_id), | ||
), | ||
). | ||
Exist(Ctx) | ||
} | ||
|
||
// GetBirthdays gets all birthdays for a given day and month | ||
func (*birthday_s) GetBirthdays(day int, month int, ctx ddtrace.SpanContext) ([]*ent.Birthday, error) { | ||
span := tracer.StartSpan( | ||
"data.birthday:GetBirthdays", | ||
tracer.ResourceName("Data.Birthday.GetBirthdays"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
return Client.Birthday.Query(). | ||
Where( | ||
birthday.Day(day), | ||
birthday.Month(month), | ||
). | ||
WithUser(). | ||
All(Ctx) | ||
} | ||
|
||
// Create creates a new birthday for a user | ||
func (*birthday_s) Create(user_id string, day int, month int, ctx ddtrace.SpanContext) (*ent.Birthday, error) { | ||
span := tracer.StartSpan( | ||
"data.birthday:Create", | ||
tracer.ResourceName("Data.Birthday.Create"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
entUser, err := User.Get(user_id, ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return Client.Birthday.Create(). | ||
SetDay(day). | ||
SetMonth(month). | ||
SetUser( | ||
entUser, | ||
). | ||
Save(Ctx) | ||
} | ||
|
||
// Get gets a birthday for a user | ||
func (*birthday_s) Get(user_id string, ctx ddtrace.SpanContext) (*ent.Birthday, error) { | ||
span := tracer.StartSpan( | ||
"data.birthday:Get", | ||
tracer.ResourceName("Data.Birthday.Get"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
return Client.Birthday.Query(). | ||
Where( | ||
birthday.HasUserWith( | ||
user.ID(user_id), | ||
), | ||
). | ||
WithUser(). | ||
Only(Ctx) | ||
} | ||
|
||
// Delete deletes a birthday for a user | ||
func (*birthday_s) Delete(user_id string, ctx ddtrace.SpanContext) (int, error) { | ||
span := tracer.StartSpan( | ||
"data.birthday:Delete", | ||
tracer.ResourceName("Data.Birthday.Delete"), | ||
tracer.ChildOf(ctx), | ||
) | ||
defer span.Finish() | ||
|
||
return Client.Birthday.Delete(). | ||
Where( | ||
birthday.HasUserWith( | ||
user.ID(user_id), | ||
), | ||
). | ||
Exec(Ctx) | ||
} |
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
Oops, something went wrong.