Manually inherit permissions in ticket category
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2023-12-06 07:16:12 -08:00
parent c0a27c8a52
commit 9c2bba193a
5 changed files with 52 additions and 12 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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 {

View File

@ -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),

View File

@ -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()