Implement channel making system

This commit is contained in:
Joren 2024-07-06 20:37:14 +02:00
parent d7c884bf1d
commit 849f592fc0
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

85
main.go
View File

@ -18,8 +18,6 @@ var (
config Config config Config
client *discordgo.Session client *discordgo.Session
db *sql.DB db *sql.DB
globalCategoryID string
) )
type Config struct { type Config struct {
@ -32,6 +30,7 @@ type Discord struct {
Token string `toml:"token"` Token string `toml:"token"`
AppID string `toml:"appid"` AppID string `toml:"appid"`
GuildID string `toml:"guildid"` GuildID string `toml:"guildid"`
CategoryID string `toml:"category_id"`
} }
type Database struct { type Database struct {
@ -98,6 +97,23 @@ func reset(userNickname, softwareType string) {
log.Printf("Resetting %s for user %s\n", softwareType, userNickname) log.Printf("Resetting %s for user %s\n", softwareType, userNickname)
} }
func getUsernameFromMember(member *discordgo.Member) string {
if member == nil {
return "UnknownUser"
}
var userName string
if member.Nick != "" {
userName = member.Nick
} else if member.User != nil && member.User.Username != "" {
userName = member.User.Username
} else {
userName = "UnknownUser"
}
return userName
}
var ( var (
commands = []discordgo.ApplicationCommand{ commands = []discordgo.ApplicationCommand{
{ {
@ -110,22 +126,12 @@ var (
Description: "Channel for ticket creation", Description: "Channel for ticket creation",
Required: true, Required: true,
}, },
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "category",
Description: "Category for tickets",
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) {
channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client) channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client)
categoryOption := i.ApplicationCommandData().Options[1].ChannelValue(client)
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{
@ -199,14 +205,29 @@ var (
softwareType = "Mesa" softwareType = "Mesa"
} }
categoryID := config.Discord.CategoryID
guildID := i.GuildID guildID := i.GuildID
userID := i.Member.User.ID userID := i.Member.User.ID
userName := i.Member.User.Username userName := getUsernameFromMember(i.Member)
if !canCreateTicket(userName, selectedOption) {
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("You already have an active %s ticket. Please wait for the administrators to process it.", softwareType),
Flags: discordgo.MessageFlagsEphemeral,
},
})
if err != nil {
log.Println("Error sending interaction response:", err)
}
return
}
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,
ParentID: globalCategoryID, ParentID: categoryID,
PermissionOverwrites: []*discordgo.PermissionOverwrite{ PermissionOverwrites: []*discordgo.PermissionOverwrite{
{ {
ID: guildID, ID: guildID,
@ -275,14 +296,7 @@ var (
log.Println("Error fetching member info:", err) log.Println("Error fetching member info:", err)
return return
} }
var userName string userName := getUsernameFromMember(member)
if member.Nick != "" {
userName = member.Nick
} else if member.User.GlobalName != "" {
userName = member.User.GlobalName
} else {
userName = member.User.Username
}
reset(userName, softwareType) reset(userName, softwareType)
@ -297,13 +311,11 @@ var (
log.Println("Error sending interaction response:", err) log.Println("Error sending interaction response:", err)
} }
_, err = client.ChannelDelete(i.ChannelID)
_,err = client.ChannelDelete(i.ChannelID)
if err != nil { if err != nil {
log.Println("Error deleting channel:", err) log.Println("Error deleting channel:", err)
} }
dmChannel, err := client.UserChannelCreate(userID) dmChannel, err := client.UserChannelCreate(userID)
if err != nil { if err != nil {
log.Println("Error creating DM channel:", err) log.Println("Error creating DM channel:", err)
@ -336,13 +348,11 @@ var (
log.Println("Error sending interaction response:", err) log.Println("Error sending interaction response:", err)
} }
_, err = client.ChannelDelete(i.ChannelID)
_,err = client.ChannelDelete(i.ChannelID)
if err != nil { if err != nil {
log.Println("Error deleting channel:", err) log.Println("Error deleting channel:", err)
} }
dmChannel, err := client.UserChannelCreate(userID) dmChannel, err := client.UserChannelCreate(userID)
if err != nil { if err != nil {
log.Println("Error creating DM channel:", err) log.Println("Error creating DM channel:", err)
@ -357,6 +367,25 @@ var (
} }
) )
func canCreateTicket(userName, softwareType string) bool {
guildChannels, err := client.GuildChannels(config.Discord.GuildID)
if err != nil {
log.Println("Error fetching guild channels:", err)
return false
}
for _, channel := range guildChannels {
if channel.Type == discordgo.ChannelTypeGuildText && channel.ParentID == config.Discord.CategoryID {
expectedName := fmt.Sprintf("reset-%s-%s", softwareType, userName)
if channel.Name == expectedName {
return false
}
}
}
return true
}
func main() { func main() {
if client == nil { if client == nil {
log.Println("Bot client is not initialized") log.Println("Bot client is not initialized")