Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
201552d647 | |||
257a7aebde | |||
1e66f0d884 | |||
afca685481 | |||
bc6dc28b0a | |||
88ba1cea73 | |||
92f1e3c6ee | |||
2e4a30ea01 |
231
main.go
231
main.go
@ -122,10 +122,10 @@ func getUsernameFromMember(member *discordgo.Member) string {
|
|||||||
var userName string
|
var userName string
|
||||||
if member.Nick != "" {
|
if member.Nick != "" {
|
||||||
userName = member.Nick
|
userName = member.Nick
|
||||||
} else if member.User != nil && member.User.Username != "" {
|
} else if member.User.GlobalName != "" {
|
||||||
userName = member.User.Username
|
userName = member.User.GlobalName
|
||||||
} else {
|
} else {
|
||||||
userName = "UnknownUser"
|
userName = member.User.Username
|
||||||
}
|
}
|
||||||
return userName
|
return userName
|
||||||
}
|
}
|
||||||
@ -144,6 +144,30 @@ var (
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "resetvanity",
|
||||||
|
Description: "Reset a user's HWID for Vanity by username or UID",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "identifier",
|
||||||
|
Description: "Usernames or UIDs, separated by commas",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "resetmesa",
|
||||||
|
Description: "Reset a user's HWID for Mesa by username or UID",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "identifier",
|
||||||
|
Description: "Usernames or UIDs, separated by commas",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
@ -209,6 +233,12 @@ var (
|
|||||||
log.Println("Error sending interaction response:", err)
|
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")
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
componentsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
componentsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
@ -330,7 +360,7 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.ChannelMessageSendComplex(channel.ID, &discordgo.MessageSend{
|
_, err = client.ChannelMessageSendComplex(channel.ID, &discordgo.MessageSend{
|
||||||
Content: fmt.Sprintf("Reset request by %s for %s", userName, softwareType),
|
Content: fmt.Sprintf("Reset request by <@%s> for %s", userID, softwareType),
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
discordgo.ActionsRow{
|
discordgo.ActionsRow{
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
@ -374,10 +404,20 @@ 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 {
|
||||||
|
for _, err := range errors {
|
||||||
log.Println("Error resetting hwid:", err)
|
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)
|
||||||
|
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
@ -447,6 +487,122 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func resetCommandHandler(client *discordgo.Session, i *discordgo.InteractionCreate, softwareType string) {
|
||||||
|
hasAdminPermission := false
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var tableName 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()
|
||||||
|
identifierList := strings.Split(identifier, ",")
|
||||||
|
|
||||||
|
var userUIDs []int
|
||||||
|
|
||||||
|
for _, identifier := range identifierList {
|
||||||
|
identifier = strings.TrimSpace(identifier)
|
||||||
|
|
||||||
|
var userName string
|
||||||
|
var userUID int
|
||||||
|
var err error
|
||||||
|
|
||||||
|
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)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if userUID != 0 {
|
||||||
|
userUIDs = append(userUIDs, userUID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(userUIDs) == 0 {
|
||||||
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "No valid users found to reset.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending interaction response:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, uid := range userUIDs {
|
||||||
|
log.Printf("Reset the HWID of user with UID %d for %s", uid, softwareType)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: fmt.Sprintf("Successfully reset HWID for %d users.", len(userUIDs)),
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending interaction response:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func canCreateTicket(userName, softwareType string) bool {
|
func canCreateTicket(userName, softwareType string) bool {
|
||||||
guildChannels, err := client.GuildChannels(config.Discord.GuildID)
|
guildChannels, err := client.GuildChannels(config.Discord.GuildID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -528,34 +684,63 @@ func resetHWID(tableName string, uids []int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func resetAndVerify(tableName string, uid int) (bool, error) {
|
func resetAndVerify(tableName string, uids []int) ([]bool, []error) {
|
||||||
|
var successes []bool
|
||||||
|
var errorsSlice []error
|
||||||
|
|
||||||
|
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 before hashes: %v", err)
|
||||||
|
return nil, []error{err}
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
beforeHashesMap := make(map[int]string)
|
||||||
|
for rows.Next() {
|
||||||
|
var uid int
|
||||||
var beforeHash string
|
var beforeHash string
|
||||||
err := db.QueryRow(
|
err := rows.Scan(&uid, &beforeHash)
|
||||||
fmt.Sprintf(`SELECT MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = $1`, tableName),
|
|
||||||
uid,
|
|
||||||
).Scan(&beforeHash)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
log.Printf("Error scanning rows: %v", err)
|
||||||
return false, fmt.Errorf("no rows found for UID %d", uid)
|
errorsSlice = append(errorsSlice, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return false, fmt.Errorf("error querying database: %v", err)
|
beforeHashesMap[uid] = beforeHash
|
||||||
}
|
}
|
||||||
|
|
||||||
resetHWID(tableName, []int{uid})
|
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
|
var afterHash string
|
||||||
err = db.QueryRow(
|
err := rows.Scan(&uid, &afterHash)
|
||||||
fmt.Sprintf(`SELECT MD5(CONCAT("StorageIdentifier", "BootIdentifier")) FROM public.%q WHERE "UID" = $1`, tableName),
|
|
||||||
uid,
|
|
||||||
).Scan(&afterHash)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
log.Printf("Error scanning rows: %v", err)
|
||||||
return false, fmt.Errorf("no rows found for UID %d after reset", uid)
|
errorsSlice = append(errorsSlice, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
return false, fmt.Errorf("error querying database after reset: %v", err)
|
afterHashesMap[uid] = afterHash
|
||||||
}
|
}
|
||||||
|
|
||||||
return beforeHash != afterHash, nil
|
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
|
||||||
|
}
|
||||||
|
successes = append(successes, beforeHashesMap[uid] != afterHash || afterHash == "d41d8cd98f00b204e9800998ecf8427e")
|
||||||
|
}
|
||||||
|
|
||||||
|
return successes, errorsSlice
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user