Skip to content

Commit

Permalink
Merge pull request #3 from 1nv8rzim/slash-command
Browse files Browse the repository at this point in the history
Slash command
  • Loading branch information
1nv8rzim authored Nov 15, 2023
2 parents c53ca66 + 06d8635 commit e3cc964
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions commands/slash/birthday.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ 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/logging"
"github.com/sirupsen/logrus"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

Expand Down Expand Up @@ -87,14 +90,70 @@ func Birthday() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *di
)
defer span.Finish()

// get user parameters
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)
// check is birthday already exists
exists, err := data.Birthday.Exists(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "encounted error when checking if birthday exists", i.Member.User, span, logrus.Fields{"err": err.Error()})
}

// code here
if exists {
// birthday exists, update existing one

// get current birthday
entBirthday, err := data.Birthday.Get(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, "encounted error when getting birthday from user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}

// update birthday
_, err = entBirthday.Update().SetDay(day).SetMonth(month).Save(data.Ctx)
if err != nil {
logging.Error(s, "encounted error when updating birthday", i.Member.User, span, logrus.Fields{"err": err.Error()})
}

// log that birthday has been updated
logging.Debug(s, "Updated birthday for "+i.Member.User.Username, i.Member.User, span)

// send user message that birthday was successfully updated
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Changed birtday to %d/%d", month, day),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}

} else {
// birthday does not exist create new one

// create new birthday for user
_, err = data.Birthday.Create(i.Member.User.ID, day, month, span.Context())
if err != nil {
logging.Error(s, "encounted error when creating birthday", i.Member.User, span, logrus.Fields{"err": err.Error()})
}

// log that birthday has been updated
logging.Debug(s, "Created birthday for "+i.Member.User.Username, i.Member.User, span)

// send user message that birthday was successfully updated
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Created birthday for " + i.Member.User.Username + " ",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
logging.Error(s, "encounted error when responding to user", i.Member.User, span, logrus.Fields{"err": err.Error()})
}
}
}
}

Expand Down

0 comments on commit e3cc964

Please sign in to comment.