diff --git a/commands/slash/attendance.go b/commands/slash/attendance.go index 405d038..a4f1289 100644 --- a/commands/slash/attendance.go +++ b/commands/slash/attendance.go @@ -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", @@ -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 { @@ -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) @@ -126,4 +78,4 @@ func attendanceMessage(userID string, ctx ddtrace.SpanContext) (message string) } return message -} \ No newline at end of file +} diff --git a/commands/slash/attendanceof.go b/commands/slash/attendanceof.go index 198f7f7..189e3d9 100644 --- a/commands/slash/attendanceof.go +++ b/commands/slash/attendanceof.go @@ -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" @@ -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 { @@ -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) diff --git a/commands/slash/dquery.go b/commands/slash/dquery.go index e64ec5c..93e3df0 100644 --- a/commands/slash/dquery.go +++ b/commands/slash/dquery.go @@ -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" @@ -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) diff --git a/commands/slash/query.go b/commands/slash/query.go index 3272ecd..3ea1db2 100644 --- a/commands/slash/query.go +++ b/commands/slash/query.go @@ -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" @@ -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, diff --git a/commands/slash/signin.go b/commands/slash/signin.go index c41240f..0397b2a 100644 --- a/commands/slash/signin.go +++ b/commands/slash/signin.go @@ -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()) diff --git a/helpers/signintype.go b/helpers/signintype.go new file mode 100644 index 0000000..9a548c9 --- /dev/null +++ b/helpers/signintype.go @@ -0,0 +1,65 @@ +package helpers + +import "github.com/ritsec/ops-bot-iii/ent/signin" + +//Converts signin type string to signin type +func StringToType(signinString string) signin.Type { + var entSigninType signin.Type + switch signinString { + 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" + } + return entSigninType +} + +//Return array of all signin types (update return array length) +func SigninTypeArray() [15]string { + return [...]string{ + "General Meeting", + "Contagion", + "IR", + "Ops", + "Ops IG", + "Red Team", + "Red Team Recruiting", + "RVAPT", + "Reversing", + "Physical", + "Wireless", + "WiCyS", + "Vulnerability Research", + "Mentorship", + "Other", + } + +} \ No newline at end of file