Compare commits

..

No commits in common. "d7c884bf1d5d696d3461161f6d64860bef1958a7" and "3abc4f32652b7fc7a3e5bc1ed0d9d8e405d049cb" have entirely different histories.

152
main.go
View File

@ -6,7 +6,6 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -23,9 +22,8 @@ var (
) )
type Config struct { type Config struct {
Discord Discord `toml:"discord"` Discord Discord `toml:"discord"`
Database Database `toml:"database"` Database Database `toml:"database"`
AdminRoles []string `toml:"admin_roles"`
} }
type Discord struct { type Discord struct {
@ -94,10 +92,6 @@ func init() {
} }
} }
func reset(userNickname, softwareType string) {
log.Printf("Resetting %s for user %s\n", softwareType, userNickname)
}
var ( var (
commands = []discordgo.ApplicationCommand{ commands = []discordgo.ApplicationCommand{
{ {
@ -126,6 +120,7 @@ var (
globalCategoryID = categoryOption.ID globalCategoryID = categoryOption.ID
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{ _, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
Content: "Click the button below to request a HWID reset:", Content: "Click the button below to request a HWID reset:",
Components: []discordgo.MessageComponent{ Components: []discordgo.MessageComponent{
@ -144,6 +139,7 @@ var (
log.Println("Error sending message to the specified channel:", err) log.Println("Error sending message to the specified channel:", err)
} }
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{
@ -160,7 +156,7 @@ var (
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: "For what would you like a HWID reset:", Content: "For what would u like a HWID reset:",
Components: []discordgo.MessageComponent{ Components: []discordgo.MessageComponent{
discordgo.ActionsRow{ discordgo.ActionsRow{
Components: []discordgo.MessageComponent{ Components: []discordgo.MessageComponent{
@ -203,6 +199,7 @@ var (
userID := i.Member.User.ID userID := i.Member.User.ID
userName := i.Member.User.Username userName := i.Member.User.Username
channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{ channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName), Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
Type: discordgo.ChannelTypeGuildText, Type: discordgo.ChannelTypeGuildText,
@ -213,6 +210,11 @@ var (
Type: discordgo.PermissionOverwriteTypeRole, Type: discordgo.PermissionOverwriteTypeRole,
Deny: discordgo.PermissionViewChannel, Deny: discordgo.PermissionViewChannel,
}, },
{
ID: userID,
Type: discordgo.PermissionOverwriteTypeMember,
Allow: discordgo.PermissionViewChannel,
},
}, },
}) })
if err != nil { if err != nil {
@ -220,138 +222,15 @@ var (
return return
} }
for _, roleID := range config.AdminRoles {
client.ChannelPermissionSet(channel.ID, roleID, discordgo.PermissionOverwriteTypeRole, discordgo.PermissionViewChannel, 0)
}
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: "Request submitted, you'll get a PM when it has been processed", Content: fmt.Sprintf("Request created: %s", channel.Mention()),
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral,
}, },
}) })
if err != nil { if err != nil {
log.Println("Error sending interaction response:", err) log.Println("Error sending interaction response:", err)
return
}
_, err = client.ChannelMessageSendComplex(channel.ID, &discordgo.MessageSend{
Content: fmt.Sprintf("Reset request by %s for %s", userName, softwareType),
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.Button{
Label: "Accept",
Style: discordgo.PrimaryButton,
CustomID: fmt.Sprintf("accept_%s_%s", userID, softwareType),
},
discordgo.Button{
Label: "Decline",
Style: discordgo.DangerButton,
CustomID: fmt.Sprintf("decline_%s_%s", userID, softwareType),
},
},
},
},
})
if err != nil {
log.Println("Error sending message to the ticket channel:", err)
}
},
"accept": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.MessageComponentData().CustomID
parts := strings.Split(data, "_")
if len(parts) != 3 {
log.Println("Invalid accept button custom ID")
return
}
userID := parts[1]
softwareType := parts[2]
member, err := client.GuildMember(i.GuildID, userID)
if err != nil {
log.Println("Error fetching member info:", err)
return
}
var userName string
if member.Nick != "" {
userName = member.Nick
} else if member.User.GlobalName != "" {
userName = member.User.GlobalName
} else {
userName = member.User.Username
}
reset(userName, softwareType)
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Request accepted and processed.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
_,err = client.ChannelDelete(i.ChannelID)
if err != nil {
log.Println("Error deleting channel:", err)
}
dmChannel, err := client.UserChannelCreate(userID)
if err != nil {
log.Println("Error creating DM channel:", err)
return
}
_, err = client.ChannelMessageSend(dmChannel.ID, fmt.Sprintf("Your reset request for %s has been accepted and processed.", softwareType))
if err != nil {
log.Println("Error sending DM:", err)
}
},
"decline": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
data := i.MessageComponentData().CustomID
parts := strings.Split(data, "_")
if len(parts) != 3 {
log.Println("Invalid decline button custom ID")
return
}
userID := parts[1]
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Request declined.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
_,err = client.ChannelDelete(i.ChannelID)
if err != nil {
log.Println("Error deleting channel:", err)
}
dmChannel, err := client.UserChannelCreate(userID)
if err != nil {
log.Println("Error creating DM channel:", err)
return
}
_, err = client.ChannelMessageSend(dmChannel.ID, "Your reset request has been declined.")
if err != nil {
log.Println("Error sending DM:", err)
} }
}, },
} }
@ -384,13 +263,8 @@ func main() {
h(client, i) h(client, i)
} }
} else if i.Type == discordgo.InteractionMessageComponent { } else if i.Type == discordgo.InteractionMessageComponent {
customID := i.MessageComponentData().CustomID if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
if h, ok := componentsHandlers[customID]; ok {
h(client, i) h(client, i)
} else if strings.HasPrefix(customID, "accept_") {
componentsHandlers["accept"](client, i)
} else if strings.HasPrefix(customID, "decline_") {
componentsHandlers["decline"](client, i)
} }
} }
}) })