Impl Basc System

This commit is contained in:
Joren 2024-07-06 19:22:38 +02:00
parent 3abc4f3265
commit 71bec93b58
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

139
main.go
View File

@ -6,6 +6,7 @@ import (
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/BurntSushi/toml"
@ -22,8 +23,9 @@ var (
)
type Config struct {
Discord Discord `toml:"discord"`
Database Database `toml:"database"`
Discord Discord `toml:"discord"`
Database Database `toml:"database"`
AdminRoles []string `toml:"admin_roles"`
}
type Discord struct {
@ -92,6 +94,10 @@ func init() {
}
}
func reset(userNickname, softwareType string) {
log.Printf("Resetting %s for user %s\n", softwareType, userNickname)
}
var (
commands = []discordgo.ApplicationCommand{
{
@ -120,7 +126,6 @@ var (
globalCategoryID = categoryOption.ID
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
Content: "Click the button below to request a HWID reset:",
Components: []discordgo.MessageComponent{
@ -139,7 +144,6 @@ var (
log.Println("Error sending message to the specified channel:", err)
}
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
@ -156,7 +160,7 @@ var (
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "For what would u like a HWID reset:",
Content: "For what would you like a HWID reset:",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
@ -199,7 +203,6 @@ var (
userID := i.Member.User.ID
userName := i.Member.User.Username
channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
Type: discordgo.ChannelTypeGuildText,
@ -210,11 +213,6 @@ var (
Type: discordgo.PermissionOverwriteTypeRole,
Deny: discordgo.PermissionViewChannel,
},
{
ID: userID,
Type: discordgo.PermissionOverwriteTypeMember,
Allow: discordgo.PermissionViewChannel,
},
},
})
if err != nil {
@ -222,15 +220,125 @@ var (
return
}
for _, roleID := range config.AdminRoles {
client.ChannelPermissionSet(channel.ID, roleID, discordgo.PermissionOverwriteTypeRole, discordgo.PermissionViewChannel, 0)
}
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Request created: %s", channel.Mention()),
Content: "Request submitted, you'll get a PM when it has been processed",
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
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)
}
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)
}
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)
}
},
}
@ -263,8 +371,13 @@ func main() {
h(client, i)
}
} else if i.Type == discordgo.InteractionMessageComponent {
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
customID := i.MessageComponentData().CustomID
if h, ok := componentsHandlers[customID]; ok {
h(client, i)
} else if strings.HasPrefix(customID, "accept_") {
componentsHandlers["accept"](client, i)
} else if strings.HasPrefix(customID, "decline_") {
componentsHandlers["decline"](client, i)
}
}
})