discord: Add permission fields to Command (#326)

* api/discord: add DefaultMemberPermissions and DMPermission to commands
* api/discord: invert DMPermission to match NoDefaultPermission
This commit is contained in:
Juby210 2022-05-08 12:33:56 +02:00 committed by GitHub
parent df0fa66c29
commit 4b6bc657dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View File

@ -26,6 +26,8 @@ type CreateCommandData struct {
Description string `json:"description"`
DescriptionLocalizations discord.StringLocales `json:"description_localizations,omitempty"`
Options discord.CommandOptions `json:"options,omitempty"`
DefaultMemberPermissions *discord.Permissions `json:"default_member_permissions,string,omitempty"`
NoDMPermission bool `json:"-"`
NoDefaultPermission bool `json:"-"`
Type discord.CommandType `json:"type,omitempty"`
}
@ -34,6 +36,7 @@ func (c CreateCommandData) MarshalJSON() ([]byte, error) {
type RawCreateCommandData CreateCommandData
cmd := struct {
RawCreateCommandData
DMPermission bool `json:"dm_permission"`
DefaultPermission bool `json:"default_permission"`
}{RawCreateCommandData: (RawCreateCommandData)(c)}
@ -41,6 +44,7 @@ func (c CreateCommandData) MarshalJSON() ([]byte, error) {
// meaning of the field (>No<DefaultPermission) to match Go's default
// value, false.
cmd.DefaultPermission = !c.NoDefaultPermission
cmd.DMPermission = !c.NoDMPermission
return json.Marshal(cmd)
}
@ -49,6 +53,7 @@ func (c *CreateCommandData) UnmarshalJSON(data []byte) error {
type RawCreateCommandData CreateCommandData
cmd := struct {
*RawCreateCommandData
DMPermission bool `json:"dm_permission"`
DefaultPermission bool `json:"default_permission"`
}{RawCreateCommandData: (*RawCreateCommandData)(c)}
if err := json.Unmarshal(data, &cmd); err != nil {
@ -59,6 +64,7 @@ func (c *CreateCommandData) UnmarshalJSON(data []byte) error {
// meaning of the field (>No<DefaultPermission) to match Go's default
// value, false.
c.NoDefaultPermission = !cmd.DefaultPermission
c.NoDMPermission = !cmd.DMPermission
// Discord defaults type to 1 if omitted.
if c.Type == 0 {

View File

@ -52,6 +52,11 @@ type Command struct {
//
// It is only present on ChatInputCommands.
Options CommandOptions `json:"options,omitempty"`
// DefaultMemberPermissions is set of permissions.
DefaultMemberPermissions *Permissions `json:"default_member_permissions,string,omitempty"`
// NoDMPermission indicates whether the command is NOT available in DMs with
// the app, only for globally-scoped commands. By default, commands are visible.
NoDMPermission bool `json:"-"`
// NoDefaultPermissions defines whether the command is NOT enabled by
// default when the app is added to a guild.
NoDefaultPermission bool `json:"-"`
@ -112,6 +117,7 @@ func (c *Command) MarshalJSON() ([]byte, error) {
type RawCommand Command
cmd := struct {
*RawCommand
DMPermission bool `json:"dm_permission"`
DefaultPermission bool `json:"default_permission"`
}{RawCommand: (*RawCommand)(c)}
@ -119,6 +125,7 @@ func (c *Command) MarshalJSON() ([]byte, error) {
// meaning of the field (>No<DefaultPermission) to match Go's default
// value, false.
cmd.DefaultPermission = !c.NoDefaultPermission
cmd.DMPermission = !c.NoDMPermission
return json.Marshal(cmd)
}
@ -128,6 +135,7 @@ func (c *Command) UnmarshalJSON(data []byte) error {
cmd := struct {
*rawCommand
DMPermission bool `json:"dm_permission"`
DefaultPermission bool `json:"default_permission"`
}{
rawCommand: (*rawCommand)(c),
@ -141,6 +149,7 @@ func (c *Command) UnmarshalJSON(data []byte) error {
// meaning of the field (>No<DefaultPermission) to match Go's default
// value, false.
c.NoDefaultPermission = !cmd.DefaultPermission
c.NoDMPermission = !cmd.DMPermission
// Discord defaults type to 1 if omitted.
if c.Type == 0 {

View File

@ -144,6 +144,14 @@ const (
PermissionManageEvents
)
func NewPermissions(p ...Permissions) *Permissions {
var perm Permissions
for _, permission := range p {
perm |= permission
}
return &perm
}
func (p Permissions) Has(perm Permissions) bool {
return HasFlag(uint64(p), uint64(perm))
}