Skip to content

Commit

Permalink
143 refactor the switch case for the signins into its own function (#149
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oms125 authored Nov 8, 2024
1 parent 19ccd05 commit ce67cb0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 236 deletions.
112 changes: 32 additions & 80 deletions commands/slash/attendance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/ent/signin"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

//Attendance slash command
// Attendance slash command
func Attendance() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *discordgo.InteractionCreate)) {
return &discordgo.ApplicationCommand {
Name: "attendance",
Description: "Get signin history",
DefaultMemberPermissions: &permission.Member,
},
func(s *discordgo.Session, i *discordgo.InteractionCreate) {
span := tracer.StartSpan(
"commands.slash.attendance:Attendance",
tracer.ResourceName("/attendance"),
)
defer span.Finish()
return &discordgo.ApplicationCommand{
Name: "attendance",
Description: "Get signin history",
DefaultMemberPermissions: &permission.Member,
},
func(s *discordgo.Session, i *discordgo.InteractionCreate) {
span := tracer.StartSpan(
"commands.slash.attendance:Attendance",
tracer.ResourceName("/attendance"),
)
defer span.Finish()

logging.Debug(s, "Attendance command received", i.Member.User, span)
logging.Debug(s, "Attendance command received", i.Member.User, span)

err := s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse {
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData {
Content: attendanceMessage(i.Member.User.ID, span.Context()),
Flags: discordgo.MessageFlagsEphemeral,
err := s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: attendanceMessage(i.Member.User.ID, span.Context()),
Flags: discordgo.MessageFlagsEphemeral,
},
},
},
)
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
} else {
logging.Debug(s, "Signin History Given", i.Member.User, span)
)
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
} else {
logging.Debug(s, "Signin History Given", i.Member.User, span)
}
}
}
}

//returns the message sent to the user by the Attendance command
// returns the message sent to the user by the Attendance command
func attendanceMessage(userID string, ctx ddtrace.SpanContext) (message string) {
span := tracer.StartSpan(
"commands.slash.attendance:attendanceMessage",
Expand All @@ -56,23 +56,7 @@ func attendanceMessage(userID string, ctx ddtrace.SpanContext) (message string)
defer span.Finish()

message = "**Your Signins:**"
signinTypes := [...]string{
"General Meeting",
"Contagion",
"IR",
"Ops",
"Ops IG",
"Red Team",
"Red Team Recruiting",
"RVAPT",
"Reversing",
"Physical",
"Wireless",
"WiCyS",
"Vulnerability Research",
"Mentorship",
"Other",
}
signinTypes := helpers.SigninTypeArray()

totalSignins, err := data.Signin.GetSignins(userID, span.Context())
if err != nil {
Expand All @@ -82,39 +66,7 @@ func attendanceMessage(userID string, ctx ddtrace.SpanContext) (message string)
message += fmt.Sprintf("\n\tTotal Signins: `%d`", totalSignins)

for _, signinType := range signinTypes {
var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "IR":
entSigninType = signin.TypeIR
case "Ops":
entSigninType = signin.TypeOps
case "Ops IG":
entSigninType = signin.TypeOpsIG
case "Red Team":
entSigninType = signin.TypeRedTeam
case "Red Team Recruiting":
entSigninType = signin.TypeRedTeamRecruiting
case "RVAPT":
entSigninType = signin.TypeRVAPT
case "Reversing":
entSigninType = signin.TypeReversing
case "Physical":
entSigninType = signin.TypePhysical
case "Wireless":
entSigninType = signin.TypeWireless
case "WiCyS":
entSigninType = signin.TypeWiCyS
case "Vulnerability Research":
entSigninType = signin.TypeVulnerabilityResearch
case "Mentorship":
entSigninType = signin.TypeMentorship
case "Other":
entSigninType = signin.TypeOther
}
entSigninType := helpers.StringToType(signinType)
signins, err := data.Signin.GetSigninsByType(userID, entSigninType, span.Context())
if err != nil {
logging.Error(nil, err.Error(), nil, span)
Expand All @@ -126,4 +78,4 @@ func attendanceMessage(userID string, ctx ddtrace.SpanContext) (message string)
}

return message
}
}
54 changes: 3 additions & 51 deletions commands/slash/attendanceof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/ent/signin"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
Expand Down Expand Up @@ -66,23 +66,7 @@ func attendanceofMessage(u *discordgo.User, ctx ddtrace.SpanContext) (message st
defer span.Finish()

message = ("**Signins for " + u.Username + "**")
signinTypes := [...]string{
"General Meeting",
"Contagion",
"IR",
"Ops",
"Ops IG",
"Red Team",
"Red Team Recruiting",
"RVAPT",
"Reversing",
"Physical",
"Wireless",
"WiCyS",
"Vulnerability Research",
"Mentorship",
"Other",
}
signinTypes := helpers.SigninTypeArray()

totalSignins, err := data.Signin.GetSignins(u.ID, span.Context())
if err != nil {
Expand All @@ -92,39 +76,7 @@ func attendanceofMessage(u *discordgo.User, ctx ddtrace.SpanContext) (message st
message += fmt.Sprintf("\n\tTotal Signins: `%d`", totalSignins)

for _, signinType := range signinTypes {
var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "IR":
entSigninType = signin.TypeIR
case "Ops":
entSigninType = signin.TypeOps
case "Ops IG":
entSigninType = signin.TypeOpsIG
case "Red Team":
entSigninType = signin.TypeRedTeam
case "Red Team Recruiting":
entSigninType = signin.TypeRedTeamRecruiting
case "RVAPT":
entSigninType = signin.TypeRVAPT
case "Reversing":
entSigninType = signin.TypeReversing
case "Physical":
entSigninType = signin.TypePhysical
case "Wireless":
entSigninType = signin.TypeWireless
case "WiCyS":
entSigninType = signin.TypeWiCyS
case "Vulnerability Research":
entSigninType = signin.TypeVulnerabilityResearch
case "Mentorship":
entSigninType = signin.TypeMentorship
case "Other":
entSigninType = signin.TypeOther
}
entSigninType := helpers.StringToType(signinType)
signins, err := data.Signin.GetSigninsByType(u.ID, entSigninType, span.Context())
if err != nil {
logging.Error(nil, err.Error(), nil, span)
Expand Down
38 changes: 2 additions & 36 deletions commands/slash/dquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/ent/signin"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
Expand Down Expand Up @@ -110,41 +110,7 @@ func DQuery() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disc
signinType := i.ApplicationCommandData().Options[0].StringValue()
dateRequested := i.ApplicationCommandData().Options[1].StringValue()

var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "IR":
entSigninType = signin.TypeIR
case "Ops":
entSigninType = signin.TypeOps
case "Ops IG":
entSigninType = signin.TypeOpsIG
case "Red Team":
entSigninType = signin.TypeRedTeam
case "Red Team Recruiting":
entSigninType = signin.TypeRedTeamRecruiting
case "RVAPT":
entSigninType = signin.TypeRVAPT
case "Reversing":
entSigninType = signin.TypeReversing
case "Physical":
entSigninType = signin.TypePhysical
case "Wireless":
entSigninType = signin.TypeWireless
case "WiCyS":
entSigninType = signin.TypeWiCyS
case "Vulnerability Research":
entSigninType = signin.TypeVulnerabilityResearch
case "Mentorship":
entSigninType = signin.TypeMentorship
case "Other":
entSigninType = signin.TypeOther
case "All":
entSigninType = "All"
}
entSigninType := helpers.StringToType(signinType)

// Parsing the date as time.Time
dateToQuery, err := time.Parse("2006-01-02", dateRequested)
Expand Down
37 changes: 1 addition & 36 deletions commands/slash/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
"github.com/ritsec/ops-bot-iii/data"
"github.com/ritsec/ops-bot-iii/ent/signin"
"github.com/ritsec/ops-bot-iii/helpers"
"github.com/ritsec/ops-bot-iii/logging"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -145,41 +144,7 @@ func Query() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disco
}
}

var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "IR":
entSigninType = signin.TypeIR
case "Ops":
entSigninType = signin.TypeOps
case "Ops IG":
entSigninType = signin.TypeOpsIG
case "Red Team":
entSigninType = signin.TypeRedTeam
case "Red Team Recruiting":
entSigninType = signin.TypeRedTeamRecruiting
case "RVAPT":
entSigninType = signin.TypeRVAPT
case "Reversing":
entSigninType = signin.TypeReversing
case "Physical":
entSigninType = signin.TypePhysical
case "Wireless":
entSigninType = signin.TypeWireless
case "WiCyS":
entSigninType = signin.TypeWiCyS
case "Vulnerability Research":
entSigninType = signin.TypeVulnerabilityResearch
case "Mentorship":
entSigninType = signin.TypeMentorship
case "Other":
entSigninType = signin.TypeOther
case "All":
entSigninType = "All"
}
entSigninType := helpers.StringToType(signinType)

signins, err := data.Signin.Query(
time.Duration(hours)*time.Hour+time.Duration(days)*24*time.Hour+time.Duration(weeks)*7*24*time.Hour,
Expand Down
34 changes: 1 addition & 33 deletions commands/slash/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,39 +114,7 @@ func Signin() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *disc

signinSlug := uuid.New().String()

var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "IR":
entSigninType = signin.TypeIR
case "Ops":
entSigninType = signin.TypeOps
case "Ops IG":
entSigninType = signin.TypeOpsIG
case "Red Team":
entSigninType = signin.TypeRedTeam
case "Red Team Recruiting":
entSigninType = signin.TypeRedTeamRecruiting
case "RVAPT":
entSigninType = signin.TypeRVAPT
case "Reversing":
entSigninType = signin.TypeReversing
case "Physical":
entSigninType = signin.TypePhysical
case "Wireless":
entSigninType = signin.TypeWireless
case "WiCyS":
entSigninType = signin.TypeWiCyS
case "Vulnerability Research":
entSigninType = signin.TypeVulnerabilityResearch
case "Mentorship":
entSigninType = signin.TypeMentorship
case "Other":
entSigninType = signin.TypeOther
}
entSigninType := helpers.StringToType(signinType)

// Check if sign-in creator has already signed in
recentSignin, err := data.Signin.RecentSignin(i.Member.User.ID, entSigninType, span.Context())
Expand Down
Loading

0 comments on commit ce67cb0

Please sign in to comment.