Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
BrammyS committed Aug 5, 2021
2 parents 0558a70 + 8f5b5ce commit 8baafd4
Show file tree
Hide file tree
Showing 32 changed files with 815 additions and 114 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ Color-Chan.Discord is available on [NuGet](https://www.nuget.org/packages/Color-
* [Color-Chan.Discord](https://www.nuget.org/packages/Color-Chan.Discord)


```powershell
Install-Package Color-Chan.Discord
```
```powershell
Install-Package Color-Chan.Discord
```

OR
OR

```powershell
dotnet add package Color-Chan.Discord
```
```powershell
dotnet add package Color-Chan.Discord
```

The induvidial components are also available on NuGet:
* [Color-Chan.Discord.Rest](https://www.nuget.org/packages/Color-Chan.Discord.Rest)
Expand Down Expand Up @@ -218,6 +218,7 @@ public class PongCommands : SlashCommandModule
}
```
_For more examples, please refer to the [Samples folder](https://github.com/Color-Chan/Color-Chan.Discord/tree/main/samples)_
### URL
Expand All @@ -226,9 +227,6 @@ You will need to add this URL to you [application](https://discord.com/developer
 
[![interactionUrlSetup](https://cdn.colorchan.com/examples/interactionUrlExample.png)](https://discord.com/developers/applications/)
_For more examples, please refer to the [Samples folder](https://github.com/Color-Chan/Color-Chan.Discord/tree/main/samples)_
<!-- ROADMAP -->
## Roadmap
Expand Down
10 changes: 8 additions & 2 deletions docs/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
{
"src": [
{
"files": [ "**/*.sln", "src/DotnetNew/*.csproj" ],
"exclude": [ "**/bin/**", "**/obj/**" ],
"files": [
"src/**.sln",
"src/**.csproj"
],
"exclude": [
"**/bin/**",
"**/obj/**"
],
"src": "../"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SlashCommandAttribute : Attribute
/// </summary>
/// <param name="name">The name of the command.</param>
/// <param name="description">The description of what the command does.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild. Default: true.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="name" /> or <paramref name="description" /> doesn't
/// match the command name requirements.
Expand All @@ -24,7 +25,7 @@ public class SlashCommandAttribute : Attribute
/// Thrown when <paramref name="name" /> or <paramref name="description" /> is
/// null.
/// </exception>
public SlashCommandAttribute(string name, string description)
public SlashCommandAttribute(string name, string description, bool defaultPermission = true)
{
if (name.Length is < 1 or > 32) throw new ArgumentException("Command names must be between 1 and 32 characters.");

Expand All @@ -36,6 +37,7 @@ public SlashCommandAttribute(string name, string description)

Name = name ?? throw new ArgumentNullException(nameof(name), "Command name can not be null");
Description = description ?? throw new ArgumentNullException(nameof(description), "Command description can not be null");
DefaultPermission = defaultPermission;
}

/// <summary>
Expand All @@ -47,5 +49,10 @@ public SlashCommandAttribute(string name, string description)
/// The description of what the command does.
/// </summary>
public string Description { get; }

/// <summary>
/// Whether the command is enabled by default when the app is added to a guild.
/// </summary>
public bool DefaultPermission { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SlashCommandGroupAttribute : Attribute
/// </summary>
/// <param name="name">The name of the command group.</param>
/// <param name="description">The description of what the command group does.</param>
/// <param name="defaultPermission">Whether the command is enabled by default when the app is added to a guild. Default: true.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="name" /> or <paramref name="description" /> doesn't
/// match the command group name requirements.
Expand All @@ -24,7 +25,7 @@ public class SlashCommandGroupAttribute : Attribute
/// Thrown when <paramref name="name" /> or <paramref name="description" /> is
/// null.
/// </exception>
public SlashCommandGroupAttribute(string name, string description)
public SlashCommandGroupAttribute(string name, string description, bool defaultPermission = true)
{
if (name.Length is < 1 or > 32) throw new ArgumentException("Command group names must be between 1 and 32 characters.");

Expand All @@ -36,6 +37,7 @@ public SlashCommandGroupAttribute(string name, string description)

Name = name ?? throw new ArgumentNullException(nameof(name), "Command group name can not be null");
Description = description ?? throw new ArgumentNullException(nameof(description), "Command group description can not be null");
DefaultPermission = defaultPermission;
}

/// <summary>
Expand All @@ -47,5 +49,10 @@ public SlashCommandGroupAttribute(string name, string description)
/// The description of what the command group is.
/// </summary>
public string Description { get; }

/// <summary>
/// Whether the command is enabled by default when the app is added to a guild.
/// </summary>
public bool DefaultPermission { get; }
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class SlashCommandConfiguration
/// <summary>
/// Whether or not the slash commands auto sync feature is enabled. Default: false.
/// </summary>
public bool EnableAutoSync { get; set; } = false;
public bool EnableAutoSync { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Color_Chan.Discord.Core.Common.API.Params;
using Color_Chan.Discord.Core.Common.API.Params.Application;
using Color_Chan.Discord.Core.Common.Models.Application;

namespace Color_Chan.Discord.Commands.Extensions
{
internal static class DiscordCreateApplicationCommandDataExtensions
{
internal static List<DiscordCreateApplicationCommand> GetUpdatedOrNewCommands(this IEnumerable<DiscordCreateApplicationCommand> newCommands,
IReadOnlyCollection<IDiscordApplicationCommand> existingCommands)
/// <summary>
/// Filters out any commands that haven't been updated or are not new.
/// </summary>
/// <param name="newCommands">The local commands.</param>
/// <param name="existingCommands">The commands pulled from discords api.</param>
/// <returns>
/// A <see cref="Tuple"/> of <see cref="DiscordCreateApplicationCommand"/> and <see cref="bool"/> and <see cref="ulong"/>
/// False means that the command is not new but is updated, and True means that the command is new.
/// The <see cref="ulong"/> contains the command ID if the command was not new.
/// </returns>
internal static List<Tuple<DiscordCreateApplicationCommand, bool, ulong?>> GetUpdatedOrNewCommands(this IEnumerable<DiscordCreateApplicationCommand> newCommands,
IReadOnlyCollection<IDiscordApplicationCommand> existingCommands)
{
var updatedCommands = new List<DiscordCreateApplicationCommand>();
var updatedCommands = new List<Tuple<DiscordCreateApplicationCommand, bool, ulong?>>();

foreach (var newCommand in newCommands)
{
Expand All @@ -19,7 +30,7 @@ internal static List<DiscordCreateApplicationCommand> GetUpdatedOrNewCommands(th
if (existingCommand is null)
{
// New command found.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, true));
continue;
}

Expand All @@ -28,14 +39,14 @@ internal static List<DiscordCreateApplicationCommand> GetUpdatedOrNewCommands(th
if (!newCommand.Description.Equals(existingCommand.Description))
{
// Description has been updated.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, false, existingCommand.Id));
continue;
}

if (!newCommand.DefaultPermission == existingCommand.DefaultPermission)
{
// DefaultPermission has been updated.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, false, existingCommand.Id));
continue;
}

Expand All @@ -44,21 +55,30 @@ internal static List<DiscordCreateApplicationCommand> GetUpdatedOrNewCommands(th
if (existingCommand.Options is null)
{
// Options has been updated.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, false, existingCommand.Id));
continue;
}

if (newCommand.Options!.HasNewOrUpdatedOptions(existingCommand.Options.ToList()))
{
// Options has been updated.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, false, existingCommand.Id));
}
}

if (newCommand.Options!.Any() != existingCommand.Options is not null)
{
// New command doesnt have any options but the existing does.
updatedCommands.Add(newCommand);
updatedCommands.Add(CreateTuple(newCommand, false, existingCommand.Id));
}
}

return updatedCommands;
}

private static Tuple<DiscordCreateApplicationCommand, bool, ulong?> CreateTuple(DiscordCreateApplicationCommand command, bool isNew, ulong? commandId = null)
{
return new Tuple<DiscordCreateApplicationCommand, bool, ulong?>(command, isNew, commandId);
}
}
}
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;
}
}
}
Loading

0 comments on commit 8baafd4

Please sign in to comment.