Skip to content

Commit

Permalink
Merge pull request #65 from 1nv8rzim/data-abstraction
Browse files Browse the repository at this point in the history
Data abstraction
  • Loading branch information
1nv8rzim authored Nov 8, 2023
2 parents 565112f + 22aaad6 commit ac5f264
Show file tree
Hide file tree
Showing 28 changed files with 3,192 additions and 19 deletions.
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
}
19 changes: 19 additions & 0 deletions commands/scheduled/birthday.go
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
}
118 changes: 118 additions & 0 deletions commands/slash/birthday.go
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

}
}
108 changes: 108 additions & 0 deletions data/birthday.go
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)
}
3 changes: 3 additions & 0 deletions data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ var (

// Shitposts is the struct reference shitposts
Shitposts *shitpost_s = &shitpost_s{}

// Birthday is the struct reference birthday
Birthday *birthday_s = &birthday_s{}
)

func init() {
Expand Down
Loading

0 comments on commit ac5f264

Please sign in to comment.