-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
815 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Color-Chan.Discord.Commands/Attributes/SlashCommandPermissionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using Color_Chan.Discord.Core.Common.API.DataModels.Application; | ||
|
||
namespace Color_Chan.Discord.Commands.Attributes | ||
{ | ||
/// <summary> | ||
/// Makes the command available to a specific role or user. | ||
/// </summary> | ||
/// <remarks> | ||
/// Not compatible with global slash command! | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)] | ||
public class SlashCommandPermissionAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="SlashCommandPermissionAttribute" />. | ||
/// </summary> | ||
/// <param name="id">The id of the role or user.</param> | ||
/// <param name="type">Specifies the type that the ID belongs to.</param> | ||
/// <param name="allow">Whether to allow the user/role to use the command.</param> | ||
public SlashCommandPermissionAttribute(ulong id, DiscordApplicationCommandPermissionsType type, bool allow = true) | ||
{ | ||
Id = id; | ||
Type = type; | ||
Allow = allow; | ||
} | ||
|
||
/// <summary> | ||
/// The id of the role or user.. | ||
/// </summary> | ||
public ulong Id { get; init; } | ||
|
||
/// <summary> | ||
/// Specifies the type that the ID belongs to. | ||
/// </summary> | ||
public DiscordApplicationCommandPermissionsType Type { get; init; } | ||
|
||
/// <summary> | ||
/// The id of the role or user.. | ||
/// </summary> | ||
public bool Allow { get; init; } = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...r-Chan.Discord.Commands/Extensions/DiscordGuildApplicationCommandPermissionsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Color_Chan.Discord.Core.Common.API.Params.Application; | ||
using Color_Chan.Discord.Core.Common.Models.Guild; | ||
|
||
namespace Color_Chan.Discord.Commands.Extensions | ||
{ | ||
internal static class DiscordGuildApplicationCommandPermissionsExtensions | ||
{ | ||
internal static bool ShouldUpdatePermissions(this List<DiscordBatchEditApplicationCommandPermissions> localCommandPerms, | ||
IReadOnlyList<IDiscordGuildApplicationCommandPermissions> existingPerms) | ||
{ | ||
foreach (var localCommandPerm in localCommandPerms) | ||
{ | ||
var existingCommandPerm = existingPerms.FirstOrDefault(x => x.CommandId.Equals(localCommandPerm.CommandId)); | ||
|
||
if (existingCommandPerm is null) | ||
{ | ||
// New command perms found. | ||
return true; | ||
} | ||
|
||
// Found existing perm. | ||
|
||
if (ContainsNewOrUpdatedPerm(localCommandPerm, existingCommandPerm)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
foreach (var existingPerm in existingPerms) | ||
{ | ||
var localCommandPerm = localCommandPerms.FirstOrDefault(x => x.CommandId.Equals(existingPerm.CommandId)); | ||
|
||
if (localCommandPerm is null) | ||
{ | ||
// Deleted command perms found. | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool ContainsNewOrUpdatedPerm(DiscordBatchEditApplicationCommandPermissions localCommandPerm, IDiscordGuildApplicationCommandPermissions existingCommandPerm) | ||
{ | ||
if (localCommandPerm.Permissions.Count() != existingCommandPerm.Permissions.Count()) | ||
{ | ||
return true; | ||
} | ||
|
||
foreach (var localPerm in localCommandPerm.Permissions) | ||
{ | ||
var existingPerm = existingCommandPerm.Permissions.FirstOrDefault(x => x.Id == localPerm.Id); | ||
|
||
if (existingPerm is null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (localPerm.Type != existingPerm.Type) | ||
{ | ||
return true; | ||
} | ||
|
||
if (localPerm.Allow != existingPerm.Allow) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
foreach (var existingPerm in existingCommandPerm.Permissions) | ||
{ | ||
var localPerm = localCommandPerm.Permissions.FirstOrDefault(x => x.Id == existingPerm.Id); | ||
|
||
if (localPerm is null) | ||
{ | ||
return true; | ||
} | ||
|
||
if (existingPerm.Type != localPerm.Type) | ||
{ | ||
return true; | ||
} | ||
|
||
if (existingPerm.Allow != localPerm.Allow) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.