Skip to content

Commit

Permalink
Refactored with new helpers and cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
c0untingNumbers committed Nov 10, 2024
1 parent d73784e commit df548d2
Showing 1 changed file with 22 additions and 50 deletions.
72 changes: 22 additions & 50 deletions commands/slash/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package slash

import (
"fmt"
"strings"

"github.com/bwmarrin/discordgo"
"github.com/ritsec/ops-bot-iii/commands/slash/permission"
Expand Down Expand Up @@ -43,88 +44,67 @@ func Openstack() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *d
defer span.Finish()

ssOption := i.ApplicationCommandData().Options[0].StringValue()
helpers.InitialMessage(s, i, fmt.Sprintf("You ran the /openstack command to %s your account!", strings.ToLower(ssOption)))

// Initialize the environment variables for Openstack CLI
helpers.SetOpenstackRC()

// CHECK IF USER IS DM'ABLE
err := helpers.SendDirectMessage(s, i.Member.User.ID, "Checking to see if your DMs are open... your openstack account username and password will be sent here!", span.Context())
err := helpers.UpdateMessage(s, i, "Checking if your DMs are open...")
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}

// Checking if the user is DM'able
err = helpers.SendDirectMessage(s, i.Member.User.ID, "Checking to see if your DMs are open... your openstack account username and password will be sent here!", span.Context())
if err != nil {
logging.Debug(s, "User's DMs are not open", i.Member.User, span)
err = s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Your DMs are not open! Please open your DMs and run the command again.",
Flags: discordgo.MessageFlagsEphemeral,
},
},
)
err = helpers.UpdateMessage(s, i, "Your DMs are not open! Please open your DMs and run the command again.")
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}
return
}

// GET EMAIL AND CHECK IF IT IS VALID
// Get email and check if it is an actual email
email, err := data.User.GetEmail(i.Member.User.ID, span.Context())
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}
if email == "" {
logging.Debug(s, "User has no email", i.Member.User, span)
err = s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You have no verified email. Run /member and verify your email and run this command again.",
Flags: discordgo.MessageFlagsEphemeral,
},
},
)
err = helpers.UpdateMessage(s, i, "You have no verified email. Run /member and verify your email and run this command again.")
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}
return
}

// CHECK IF USER EXISTS ON OPENSTACK ALREADY
// Check if user exists on Openstack already
exists, err := helpers.CheckIfExists(email)
logging.Debug(s, fmt.Sprintf("Email: %s\nExists: %t", email, exists), i.Member.User, span)
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
return
}

if ssOption == "Create" {
// USER TRYING TO CREATE ACCOUNT WHEN ALREADY HAS ONE
// Check if user trying to create an account when it already has one
if exists {
logging.Debug(s, "User already has an openstack account and is trying to create one", i.Member.User, span)
err = s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Openstack account already exisits. Run the reset option if you forgot your password.",
Flags: discordgo.MessageFlagsEphemeral,
},
},
)
err = helpers.UpdateMessage(s, i, "Openstack account already exisits. Run the reset option if you forgot your password.")
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}
return
}

// CREATE THE ACCOUNT
// Create the account
username, password, err := helpers.Create(email)
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
return
}

// SEND THE USERNAME AND PASSWORD TO THE USER VIA DM
// Send the username and password to the usuer via DM
message := fmt.Sprintf("Thank you for reaching out to us!\nHere are your credentials for RITSEC's Openstack:\n\nUsername: %s\nTemporary Password: %s\n\nPlease change the password\nOpenstack link: stack.ritsec.cloud", username, password)
logging.Debug(s, "Sent username and password to member", i.Member.User, span)
err = helpers.SendDirectMessage(s, i.Member.User.ID, message, span.Context())
Expand All @@ -133,27 +113,19 @@ func Openstack() (*discordgo.ApplicationCommand, func(s *discordgo.Session, i *d
return
}
} else if ssOption == "Reset" {
// CHECK IF USER IS TRYING TO RESET PASSWORD ON NON-EXISTENT ACCOUNT
// Check if the user is trying to reset password on non-existent account
if !exists {
logging.Debug(s, "User does not have an openstack account and is trying to reset the password on it", i.Member.User, span)
err = s.InteractionRespond(
i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Openstack account does not exist and you are trying to reset it.",
Flags: discordgo.MessageFlagsEphemeral,
},
},
)
err = helpers.UpdateMessage(s, i, "Openstack account does not exist and you are trying to reset it.")
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
}
return
}

// RESET THE PASSWORD OF THE ACCOUNT
// Reset the password of the account
username, password, err := helpers.Reset(email)
logging.Debug(s, "User has the openstack account password reset", i.Member.User, span)
if err != nil {
logging.Error(s, err.Error(), i.Member.User, span)
return
Expand Down

0 comments on commit df548d2

Please sign in to comment.