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

91
main.go
View File

@ -18,8 +18,6 @@ var (
config Config
client *discordgo.Session
db *sql.DB
globalCategoryID string
)
type Config struct {
@ -29,9 +27,10 @@ type Config struct {
}
type Discord struct {
Token string `toml:"token"`
AppID string `toml:"appid"`
GuildID string `toml:"guildid"`
Token string `toml:"token"`
AppID string `toml:"appid"`
GuildID string `toml:"guildid"`
CategoryID string `toml:"category_id"`
}
type Database struct {
@ -98,6 +97,23 @@ func reset(userNickname, softwareType string) {
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 (
commands = []discordgo.ApplicationCommand{
{
@ -110,22 +126,12 @@ var (
Description: "Channel for ticket creation",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "category",
Description: "Category for tickets",
Required: true,
},
},
},
}
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client)
categoryOption := i.ApplicationCommandData().Options[1].ChannelValue(client)
globalCategoryID = categoryOption.ID
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
Content: "Click the button below to request a HWID reset:",
Components: []discordgo.MessageComponent{
@ -199,14 +205,29 @@ var (
softwareType = "Mesa"
}
categoryID := config.Discord.CategoryID
guildID := i.GuildID
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{
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
Type: discordgo.ChannelTypeGuildText,
ParentID: globalCategoryID,
ParentID: categoryID,
PermissionOverwrites: []*discordgo.PermissionOverwrite{
{
ID: guildID,
@ -275,14 +296,7 @@ var (
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
}
userName := getUsernameFromMember(member)
reset(userName, softwareType)
@ -297,13 +311,11 @@ var (
log.Println("Error sending interaction response:", err)
}
_,err = client.ChannelDelete(i.ChannelID)
_, 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)
@ -336,13 +348,11 @@ var (
log.Println("Error sending interaction response:", err)
}
_,err = client.ChannelDelete(i.ChannelID)
_, 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)
@ -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() {
if client == nil {
log.Println("Bot client is not initialized")