Skip to content

Commit

Permalink
feat(examples/slash_commands): application command permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
FedorLap2006 committed Jan 19, 2022
1 parent 30976bf commit 80a5189
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions examples/slash_commands/main.go
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -40,6 +41,12 @@ var (
// of the command.
Description: "Basic command",
},
{
Name: "permission-overview",
Description: "Gated application command",
DefaultMemberPermissions: discordgo.PermissionManageServer,
DMPermission: true,
},
{
Name: "basic-command-with-files",
Description: "Basic command with files",
Expand Down Expand Up @@ -224,6 +231,82 @@ var (
},
})
},
"permission-overview": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
perms, err := s.ApplicationCommandPermissions(s.State.User.ID, i.GuildID, i.ApplicationCommandData().ID)

var restError *discordgo.RESTError
if errors.As(err, &restError) && restError.Message != nil && restError.Message.Code == discordgo.ErrCodeUnknownApplicationCommandPermissions {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: ":x: No permission overwrites",
},
})
return
} else if err != nil {
panic(err)
}

if err != nil {
panic(err)
}
format := "- %s %s\n"

channels := ""
users := ""
roles := ""

for _, o := range perms.Permissions {
emoji := "❌"
if o.Permission {
emoji = "☑"
}

switch o.Type {
case discordgo.ApplicationCommandPermissionUser:
users += fmt.Sprintf(format, emoji, "<@!"+o.ID+">")
case discordgo.ApplicationCommandPermissionChannel:
if o.ID == discordgo.GuildAllChannelsID(i.GuildID) {
channels += fmt.Sprintf(format, emoji, "All channels")
} else {
channels += fmt.Sprintf(format, emoji, "<#"+o.ID+">")
}
case discordgo.ApplicationCommandPermissionRole:
if o.ID == i.GuildID {
roles += fmt.Sprintf(format, emoji, "@everyone")
} else {
roles += fmt.Sprintf(format, emoji, "<@&"+o.ID+">")
}
}
}

s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Permissions overview",
Description: "Overview of permissions for this command",
Fields: []*discordgo.MessageEmbedField{
{
Name: "Users",
Value: users,
},
{
Name: "Channels",
Value: channels,
},
{
Name: "Roles",
Value: roles,
},
},
},
},
AllowedMentions: &discordgo.MessageAllowedMentions{},
},
})
},
"subcommands": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
content := ""

Expand Down

0 comments on commit 80a5189

Please sign in to comment.