diff --git a/internal/cache/cache.go b/internal/cache/cache.go index c27a97f..d1cb68c 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -66,6 +66,30 @@ func Role(s *discordgo.Session, guildID, roleID string) (*discordgo.Role, error) return role, nil } +// Channel gets a discord channel from the cache. If it doesn't exist in the cache, it +// gets it from discord and adds it to the cache. +func Channel(s *discordgo.Session, guildID, channelID string) (*discordgo.Channel, error) { + role, err := s.State.Channel(channelID) + if errors.Is(err, discordgo.ErrStateNotFound) { + // If the role wasn't found in the state struct, + // get the guild roles from discord and add them. + channels, err := s.GuildChannels(guildID) + if err != nil { + return nil, err + } + for _, channel := range channels { + err = s.State.ChannelAdd(channel) + if err != nil { + return nil, err + } + } + return s.State.Channel(channelID) + } else if err != nil { + return nil, err + } + return role, nil +} + // Roles gets a list of roles in a discord guild from the cache. If it doesn't // exist in the cache, it gets it from discord and adds it to the cache. func Roles(s *discordgo.Session, guildID string) ([]*discordgo.Role, error) { diff --git a/internal/systems/tickets/tickets.go b/internal/systems/tickets/tickets.go index d020796..c03a212 100644 --- a/internal/systems/tickets/tickets.go +++ b/internal/systems/tickets/tickets.go @@ -9,6 +9,7 @@ import ( "github.com/bwmarrin/discordgo" "go.elara.ws/logger/log" + "go.elara.ws/owobot/internal/cache" "go.elara.ws/owobot/internal/db" "go.elara.ws/owobot/internal/systems/commands" "go.elara.ws/owobot/internal/systems/eventlog" @@ -139,15 +140,29 @@ func Open(s *discordgo.Session, guildID string, user, executor *discordgo.User) return "", err } + overwrites := []*discordgo.PermissionOverwrite{{ + ID: user.ID, + Type: discordgo.PermissionOverwriteTypeMember, + Allow: ticketPermissions, + }} + + if guild.TicketCategoryID != "" { + category, err := cache.Channel(s, guildID, guild.TicketCategoryID) + if err != nil { + log.Error("Error getting ticket category").Err(err).Send() + // If we can't get the ticket category, set it to empty string + // so that ChannelCreate doesn't try to use it. + guild.TicketCategoryID = "" + } else { + overwrites = append(overwrites, category.PermissionOverwrites...) + } + } + c, err := s.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{ - Name: "ticket-" + user.Username, - Type: discordgo.ChannelTypeGuildText, - ParentID: guild.TicketCategoryID, - PermissionOverwrites: []*discordgo.PermissionOverwrite{{ - ID: user.ID, - Type: discordgo.PermissionOverwriteTypeMember, - Allow: ticketPermissions, - }}, + Name: "ticket-" + user.Username, + Type: discordgo.ChannelTypeGuildText, + ParentID: guild.TicketCategoryID, + PermissionOverwrites: overwrites, }) if err != nil { return "", err diff --git a/internal/systems/vetting/handlers.go b/internal/systems/vetting/handlers.go index 7ab5483..89b2dd5 100644 --- a/internal/systems/vetting/handlers.go +++ b/internal/systems/vetting/handlers.go @@ -232,10 +232,10 @@ func onVettingRequest(s *discordgo.Session, i *discordgo.InteractionCreate) erro return util.RespondEphemeral(s, i.Interaction, "Successfully sent your vetting request!") } -// onApprove approves a user in vetting. It removes their vetting role, assigns a +// approveCmd approves a user in vetting. It removes their vetting role, assigns a // role of the approver's choosing, closes the user's vetting ticket, and logs // the approval. -func onApprove(s *discordgo.Session, i *discordgo.InteractionCreate) error { +func approveCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error { guild, err := db.GuildByID(i.GuildID) if err != nil { return err @@ -287,7 +287,7 @@ func onApprove(s *discordgo.Session, i *discordgo.InteractionCreate) error { err = eventlog.Log(s, i.GuildID, eventlog.Entry{ Title: "New Member Approved!", - Description: fmt.Sprintf("User: %s\nRole: %s\nApproved By: %s", user.Mention(), role.Mention(), i.Member.User.Mention()), + Description: fmt.Sprintf("**User:** %s\n**Role:** %s\n**Approved By:** %s", user.Mention(), role.Mention(), i.Member.User.Mention()), Author: user, }) if err != nil { diff --git a/internal/systems/vetting/vetting.go b/internal/systems/vetting/vetting.go index d9afe4f..1f8b278 100644 --- a/internal/systems/vetting/vetting.go +++ b/internal/systems/vetting/vetting.go @@ -103,7 +103,7 @@ func Init(s *discordgo.Session) error { }, }) - commands.Register(s, onApprove, &discordgo.ApplicationCommand{ + commands.Register(s, approveCmd, &discordgo.ApplicationCommand{ Name: "approve", Description: "Approve a member in vetting", DefaultMemberPermissions: util.Pointer[int64](discordgo.PermissionKickMembers), diff --git a/main.go b/main.go index 3792325..e970cec 100644 --- a/main.go +++ b/main.go @@ -68,6 +68,7 @@ func main() { s.StateEnabled = true s.State.TrackMembers = true s.State.TrackRoles = true + s.State.TrackChannels = true s.Identify.Intents |= discordgo.IntentMessageContent | discordgo.IntentGuildMembers err = s.Open()