Skip to content

Commit

Permalink
added dm to author of total signins after close
Browse files Browse the repository at this point in the history
  • Loading branch information
1nv8rzim committed Sep 14, 2023
1 parent 322f5c1 commit 2702f39
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
81 changes: 49 additions & 32 deletions commands/slash/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/data"
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/ent/signin"
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/google"
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/helpers"
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/logging"
"gitlab.ritsec.cloud/1nv8rZim/ops-bot-iii/structs"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand Down Expand Up @@ -109,6 +110,38 @@ func Signin() *structs.SlashCommand {

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

var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "DFIR":
entSigninType = signin.TypeDFIR
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 "Other":
entSigninType = signin.TypeOther
}

(*ComponentHandlers)[signinSlug] = func(s *discordgo.Session, j *discordgo.InteractionCreate) {
span_signinSlug := tracer.StartSpan(
"commands.slash.signin:Signin:signinSlug",
Expand All @@ -117,38 +150,6 @@ func Signin() *structs.SlashCommand {
)
defer span.Finish()

var entSigninType signin.Type
switch signinType {
case "General Meeting":
entSigninType = signin.TypeGeneralMeeting
case "Contagion":
entSigninType = signin.TypeContagion
case "DFIR":
entSigninType = signin.TypeDFIR
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 "Other":
entSigninType = signin.TypeOther
}

recentSignin, err := data.Signin.RecentSignin(j.Member.User.ID, entSigninType, span.Context())
if err != nil {
logging.Error(s, err.Error(), j.Member.User, span_signinSlug, logrus.Fields{"error": err})
Expand Down Expand Up @@ -236,6 +237,22 @@ func Signin() *structs.SlashCommand {
if err != nil {
logging.Error(s, "Error encounted while deleting message\n\n"+err.Error(), i.Member.User, span, logrus.Fields{"error": err})
}

users, err := data.Signin.QueryUsers(time.Duration(12)*time.Hour, entSigninType, span.Context())
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span, logrus.Fields{"error": err})
return
}

message := fmt.Sprintf("Signins for `%s`; %d users signed in:\n", signinType, len(users))
for _, user := range users {
message += fmt.Sprintf("- %s\n", helpers.AtUser(user.ID))
}

err = helpers.SendDirectMessage(s, i.Message.Author.ID, "", span.Context())
if err != nil {
logging.Error(s, "Error encounted while sending direct message\n\n"+err.Error(), i.Member.User, span, logrus.Fields{"error": err})
}
}()

time.Sleep(time.Duration(delay) * time.Hour)
Expand Down
18 changes: 18 additions & 0 deletions data/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,21 @@ func (*signin_s) RecentSignin(userID string, signinType signin.Type, ctx ddtrace
}
return ok, nil
}

func (*signin_s) QueryUsers(delta time.Duration, signinType signin.Type, ctx ddtrace.SpanContext) ([]*ent.User, error) {
span := tracer.StartSpan(
"data.signin:Query",
tracer.ResourceName("Data.Signin.Query"),
tracer.ChildOf(ctx),
)
defer span.Finish()

return Client.User.Query().
Where(
user.HasSigninsWith(
signin.TypeEQ(signinType),
signin.TimestampGTE(time.Now().Add(-delta)),
),
).
All(Ctx)
}

0 comments on commit 2702f39

Please sign in to comment.