Support for multiple users using ,
This commit is contained in:
parent
1e66f0d884
commit
257a7aebde
173
main.go
173
main.go
@ -151,7 +151,7 @@ var (
|
|||||||
{
|
{
|
||||||
Type: discordgo.ApplicationCommandOptionString,
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
Name: "identifier",
|
Name: "identifier",
|
||||||
Description: "Username or UID",
|
Description: "Usernames or UIDs, separated by commas",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -163,7 +163,7 @@ var (
|
|||||||
{
|
{
|
||||||
Type: discordgo.ApplicationCommandOptionString,
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
Name: "identifier",
|
Name: "identifier",
|
||||||
Description: "Username or UID",
|
Description: "Usernames or UIDs, separated by commas",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -404,9 +404,19 @@ var (
|
|||||||
reset(userName, softwareType)
|
reset(userName, softwareType)
|
||||||
tableName, _ := getTableNameAndBaseURL(strings.ToLower(softwareType))
|
tableName, _ := getTableNameAndBaseURL(strings.ToLower(softwareType))
|
||||||
|
|
||||||
_, err = resetAndVerify(tableName, int(userUID))
|
successes, errors := resetAndVerify(tableName, []int{userUID})
|
||||||
if err != nil {
|
if len(errors) > 0 {
|
||||||
log.Println("Error resetting hwid:", err)
|
for _, err := range errors {
|
||||||
|
log.Println("Error resetting hwid:", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, success := range successes {
|
||||||
|
if success {
|
||||||
|
log.Printf("Reset successful for UID %d", userUID)
|
||||||
|
} else {
|
||||||
|
log.Printf("Reset unsuccessful for UID %d", userUID)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.Printf("Reset the HWID of user %s with UID %d for %s", userName, userUID, softwareType)
|
log.Printf("Reset the HWID of user %s with UID %d for %s", userName, userUID, softwareType)
|
||||||
|
|
||||||
@ -480,11 +490,8 @@ var (
|
|||||||
func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCreate, softwareType string) {
|
func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCreate, softwareType string) {
|
||||||
hasAdminPermission := false
|
hasAdminPermission := false
|
||||||
|
|
||||||
var userName string
|
|
||||||
var userUID int
|
|
||||||
var err error
|
var err error
|
||||||
var tableName string
|
var tableName string
|
||||||
var baseURL string
|
|
||||||
|
|
||||||
for _, roleID := range config.Discord.AdminRoles {
|
for _, roleID := range config.Discord.AdminRoles {
|
||||||
member, err := client.GuildMember(i.GuildID, i.Member.User.ID)
|
member, err := client.GuildMember(i.GuildID, i.Member.User.ID)
|
||||||
@ -519,40 +526,42 @@ func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCrea
|
|||||||
}
|
}
|
||||||
|
|
||||||
identifier := i.ApplicationCommandData().Options[0].StringValue()
|
identifier := i.ApplicationCommandData().Options[0].StringValue()
|
||||||
|
identifierList := strings.Split(identifier, ",")
|
||||||
|
|
||||||
if uid, err := strconv.Atoi(identifier); err == nil {
|
var userUIDs []int
|
||||||
userUID = uid
|
|
||||||
} else {
|
|
||||||
userName = identifier
|
|
||||||
}
|
|
||||||
|
|
||||||
if userName != "" {
|
for _, identifier := range identifierList {
|
||||||
_, baseURL = getTableNameAndBaseURL(softwareType)
|
identifier = strings.TrimSpace(identifier)
|
||||||
userUID, err = fetchUserID(userName, baseURL)
|
|
||||||
if err != nil {
|
var userName string
|
||||||
log.Println("Error fetching user ID:", err)
|
var userUID int
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
var err error
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
if uid, err := strconv.Atoi(identifier); err == nil {
|
||||||
Content: "Error fetching user ID.",
|
userUID = uid
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
} else {
|
||||||
},
|
userName = identifier
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if userName != "" {
|
||||||
|
_, baseURL := getTableNameAndBaseURL(softwareType)
|
||||||
|
userUID, err = fetchUserID(userName, baseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error sending interaction response:", err)
|
log.Println("Error fetching user ID:", err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return
|
}
|
||||||
|
|
||||||
|
if userUID != 0 {
|
||||||
|
userUIDs = append(userUIDs, userUID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tableName, _ = getTableNameAndBaseURL(softwareType)
|
if len(userUIDs) == 0 {
|
||||||
success, err := resetAndVerify(tableName, userUID)
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
if err != nil || !success {
|
|
||||||
log.Println("Error resetting user:", err)
|
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: "Error resetting user.",
|
Content: "No valid users found to reset.",
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -562,12 +571,26 @@ func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCrea
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Reset the HWID of user %s with UID %d for %s", userName, userUID, softwareType)
|
tableName, _ = getTableNameAndBaseURL(softwareType)
|
||||||
|
successes, errors := resetAndVerify(tableName, userUIDs)
|
||||||
|
if len(errors) > 0 {
|
||||||
|
for _, err := range errors {
|
||||||
|
log.Println("Error:", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for i, success := range successes {
|
||||||
|
if success {
|
||||||
|
log.Printf("Reset successful for UID %d", userUIDs[i])
|
||||||
|
} else {
|
||||||
|
log.Printf("Reset unsuccessful for UID %d", userUIDs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: fmt.Sprintf("Successfully reset %s's HWID.", identifier),
|
Content: fmt.Sprintf("Successfully reset HWID for %d users.", len(userUIDs)),
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -657,34 +680,70 @@ func resetHWID(tableName string, uids []int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetAndVerify(tableName string, uid int) (bool, error) {
|
func resetAndVerify(tableName string, uids []int) ([]bool, []error) {
|
||||||
var beforeHash string
|
var beforeHashes []string
|
||||||
err := db.QueryRow(
|
var afterHashes []string
|
||||||
fmt.Sprintf(`SELECT MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = $1`, tableName),
|
var successes []bool
|
||||||
uid,
|
var errorsSlice []error
|
||||||
).Scan(&beforeHash)
|
|
||||||
|
rows, err := db.Query(fmt.Sprintf(`SELECT "UID", MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = ANY($1)`, tableName), pq.Array(uids))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
log.Printf("Error querying database for before hashes: %v", err)
|
||||||
return false, fmt.Errorf("no rows found for UID %d", uid)
|
return nil, []error{err}
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
beforeHashesMap := make(map[int]string)
|
||||||
|
for rows.Next() {
|
||||||
|
var uid int
|
||||||
|
var beforeHash string
|
||||||
|
err := rows.Scan(&uid, &beforeHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error scanning rows: %v", err)
|
||||||
|
errorsSlice = append(errorsSlice, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return false, fmt.Errorf("error querying database: %v", err)
|
beforeHashesMap[uid] = beforeHash
|
||||||
}
|
}
|
||||||
|
|
||||||
resetHWID(tableName, []int{uid})
|
for _, uid := range uids {
|
||||||
|
beforeHashes = append(beforeHashes, beforeHashesMap[uid])
|
||||||
var afterHash string
|
|
||||||
err = db.QueryRow(
|
|
||||||
fmt.Sprintf(`SELECT MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = $1`, tableName),
|
|
||||||
uid,
|
|
||||||
).Scan(&afterHash)
|
|
||||||
if err != nil {
|
|
||||||
if err == sql.ErrNoRows {
|
|
||||||
return false, fmt.Errorf("no rows found for UID %d after reset", uid)
|
|
||||||
}
|
|
||||||
return false, fmt.Errorf("error querying database after reset: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return beforeHash != afterHash || afterHash == "d41d8cd98f00b204e9800998ecf8427e", nil
|
resetHWID(tableName, uids)
|
||||||
|
|
||||||
|
rows, err = db.Query(fmt.Sprintf(`SELECT "UID", MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = ANY($1)`, tableName), pq.Array(uids))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error querying database for after hashes: %v", err)
|
||||||
|
return nil, []error{err}
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
afterHashesMap := make(map[int]string)
|
||||||
|
for rows.Next() {
|
||||||
|
var uid int
|
||||||
|
var afterHash string
|
||||||
|
err := rows.Scan(&uid, &afterHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error scanning rows: %v", err)
|
||||||
|
errorsSlice = append(errorsSlice, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
afterHashesMap[uid] = afterHash
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uid := range uids {
|
||||||
|
afterHash, ok := afterHashesMap[uid]
|
||||||
|
if !ok {
|
||||||
|
errorsSlice = append(errorsSlice, fmt.Errorf("no rows found for UID %d after reset", uid))
|
||||||
|
successes = append(successes, false)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
afterHashes = append(afterHashes, afterHash)
|
||||||
|
successes = append(successes, beforeHashesMap[uid] != afterHash || afterHash == "d41d8cd98f00b204e9800998ecf8427e")
|
||||||
|
}
|
||||||
|
|
||||||
|
return successes, errorsSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user