Split command in seperate for vanity and mesa

This commit is contained in:
Joren 2024-07-18 14:28:20 +02:00
parent afca685481
commit 1e66f0d884
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

216
main.go
View File

@ -145,8 +145,8 @@ var (
},
},
{
Name: "reset",
Description: "Reset a user's HWID by username or UID",
Name: "resetvanity",
Description: "Reset a user's HWID for Vanity by username or UID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
@ -154,15 +154,17 @@ var (
Description: "Username or UID",
Required: true,
},
},
},
{
Name: "resetmesa",
Description: "Reset a user's HWID for Mesa by username or UID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "cheat_type",
Description: "Type of cheat (mesa or vanity)",
Name: "identifier",
Description: "Username or UID",
Required: true,
Choices: []*discordgo.ApplicationCommandOptionChoice{
{Name: "mesa", Value: "mesa"},
{Name: "vanity", Value: "vanity"},
},
},
},
},
@ -231,100 +233,11 @@ var (
log.Println("Error sending interaction response:", err)
}
},
"reset": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
hasAdminPermission := false
for _, roleID := range config.Discord.AdminRoles {
member, err := client.GuildMember(i.GuildID, i.Member.User.ID)
if err != nil {
log.Println("Error fetching member info:", err)
return
}
for _, role := range member.Roles {
if role == roleID {
hasAdminPermission = true
break
}
}
if hasAdminPermission {
break
}
}
if !hasAdminPermission {
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You do not have permission to run this command.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
identifier := i.ApplicationCommandData().Options[0].StringValue()
cheatType := i.ApplicationCommandData().Options[1].StringValue()
var userName string
var userUID int
if uid, err := strconv.Atoi(identifier); err == nil {
userUID = uid
} else {
userName = identifier
}
var tableName string
var baseURL string
var err error
if userName != "" {
_, baseURL = getTableNameAndBaseURL(cheatType)
userUID, err = fetchUserID(userName, baseURL)
if err != nil {
log.Println("Error fetching user ID:", err)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error fetching user ID.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
}
tableName, _ = getTableNameAndBaseURL(cheatType)
success, err := resetAndVerify(tableName, userUID)
if err != nil || !success {
log.Println("Error resetting user:", err)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error resetting user.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Successfully reset %s's HWID.", identifier),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
"resetvanity": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
resetCommandHandler(client, i, "vanity")
},
"resetmesa": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
resetCommandHandler(client, i, "mesa")
},
}
@ -564,6 +477,105 @@ var (
}
)
func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCreate, softwareType string) {
hasAdminPermission := false
var userName string
var userUID int
var err error
var tableName string
var baseURL string
for _, roleID := range config.Discord.AdminRoles {
member, err := client.GuildMember(i.GuildID, i.Member.User.ID)
if err != nil {
log.Println("Error fetching member info:", err)
return
}
for _, role := range member.Roles {
if role == roleID {
hasAdminPermission = true
break
}
}
if hasAdminPermission {
break
}
}
if !hasAdminPermission {
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "You do not have permission to run this command.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
identifier := i.ApplicationCommandData().Options[0].StringValue()
if uid, err := strconv.Atoi(identifier); err == nil {
userUID = uid
} else {
userName = identifier
}
if userName != "" {
_, baseURL = getTableNameAndBaseURL(softwareType)
userUID, err = fetchUserID(userName, baseURL)
if err != nil {
log.Println("Error fetching user ID:", err)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error fetching user ID.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
}
tableName, _ = getTableNameAndBaseURL(softwareType)
success, err := resetAndVerify(tableName, userUID)
if err != nil || !success {
log.Println("Error resetting user:", err)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Error resetting user.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
log.Printf("Reset the HWID of user %s with UID %d for %s", userName, userUID, softwareType)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Successfully reset %s's HWID.", identifier),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
}
func canCreateTicket(userName, softwareType string) bool {
guildChannels, err := client.GuildChannels(config.Discord.GuildID)
if err != nil {