diff --git a/DisCatSharp/Entities/Channel/Overwrite/DiscordOverwriteBuilder.cs b/DisCatSharp/Entities/Channel/Overwrite/DiscordOverwriteBuilder.cs
index 5a6f5a8b4..c0639b22c 100644
--- a/DisCatSharp/Entities/Channel/Overwrite/DiscordOverwriteBuilder.cs
+++ b/DisCatSharp/Entities/Channel/Overwrite/DiscordOverwriteBuilder.cs
@@ -1,269 +1,277 @@
// This file is part of the DisCatSharp project, based off DSharpPlus.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
-using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DisCatSharp.Enums;
using Newtonsoft.Json;
namespace DisCatSharp.Entities;
///
/// Represents a Discord permission overwrite builder.
///
public sealed class DiscordOverwriteBuilder
{
///
/// Gets or sets the allowed permissions for this overwrite.
///
public Permissions Allowed { get; set; }
///
/// Gets or sets the denied permissions for this overwrite.
///
public Permissions Denied { get; set; }
///
/// Sets all unset permissions for this overwrite.
///
public Permissions Unset
{
set => this.Remove(value);
}
///
/// Gets or sets the type of this overwrite's target.
///
public OverwriteType Type { get; set; }
///
/// Gets or sets the target for this overwrite.
///
public ulong Target { get; set; }
+ ///
+ /// Creates a new for a .
+ ///
+ public DiscordOverwriteBuilder(ulong id, OverwriteType type = OverwriteType.Member)
+ {
+ this.Target = id;
+ this.Type = type;
+ }
+
///
/// Creates a new for a .
///
public DiscordOverwriteBuilder(DiscordMember member)
{
this.Target = member.Id;
this.Type = OverwriteType.Member;
}
///
/// Creates a new for a .
///
public DiscordOverwriteBuilder(DiscordRole role)
{
this.Target = role.Id;
this.Type = OverwriteType.Role;
}
///
/// Creates a new from .
///
public DiscordOverwriteBuilder(DiscordOverwrite old)
{
this.Allowed = old.Allowed;
this.Denied = old.Denied;
this.Type = old.Type;
this.Target = old.Id;
}
///
/// Creates a new and empty .
///
public DiscordOverwriteBuilder()
{ }
///
/// Allows a permission for this overwrite.
///
/// Permission or permission set to allow for this overwrite.
/// This builder.
public DiscordOverwriteBuilder Allow(Permissions permission)
{
this.Allowed |= permission;
return this;
}
///
/// Denies a permission for this overwrite.
///
/// Permission or permission set to deny for this overwrite.
/// This builder.
public DiscordOverwriteBuilder Deny(Permissions permission)
{
this.Denied |= permission;
return this;
}
///
/// Unsets a permission for this overwrite.
///
/// Permission or permission set to unset for this overwrite.
/// This builder.
public DiscordOverwriteBuilder Remove(Permissions permission)
{
this.Allowed = this.Allowed.Revoke(permission);
this.Denied = this.Denied.Revoke(permission);
return this;
}
///
/// Sets the member to which this overwrite applies.
///
/// Member to which apply this overwrite's permissions.
/// This builder.
public DiscordOverwriteBuilder SetTarget(DiscordMember member)
{
this.Target = member.Id;
this.Type = OverwriteType.Member;
return this;
}
///
public DiscordOverwriteBuilder For(DiscordMember member)
=> this.SetTarget(member);
///
/// Sets the role to which this overwrite applies.
///
/// Role to which apply this overwrite's permissions.
/// This builder.
public DiscordOverwriteBuilder SetTarget(DiscordRole role)
{
this.Target = role.Id;
this.Type = OverwriteType.Role;
return this;
}
///
public DiscordOverwriteBuilder For(DiscordRole role)
=> this.SetTarget(role);
///
/// Builds this DiscordOverwrite.
///
/// Use this object for creation of new overwrites.
internal DiscordRestOverwrite Build()
{
return new()
{
Allow = this.Allowed,
Deny = this.Denied,
Id = this.Target,
Type = this.Type,
};
}
}
public static class DiscordOverwriteBuilderExtensions
{
///
/// Merges new permissions for a target with target's existing permissions.
///
/// The PermissionOverwrites to apply this to.
/// What type of overwrite you want to target.
/// The target's id.
/// The permissions to allow.
/// The permissions deny.
/// The permissions to unset.
/// A new containing the merged role.
public static List Merge(this IEnumerable builderList, OverwriteType type, ulong target, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
{
if (!builderList.Any(x => x.Target == target && x.Type == type))
builderList = builderList.Append(new DiscordOverwriteBuilder() { Type = type, Target = target });
var discordOverwriteBuilder = builderList.First(x => x.Target == target && x.Type == type);
discordOverwriteBuilder.Allow(allowed);
discordOverwriteBuilder.Deny(denied);
discordOverwriteBuilder.Remove(unset);
return builderList.Where(x => x.Target != target && x.Type != type).Append(discordOverwriteBuilder).ToList();
}
///
/// Merges new permissions for member with member's existing permissions.
///
/// The PermissionOverwrites to apply this to.
/// The member of which to modify their permissions.
/// The permissions to allow.
/// The permissions to deny.
/// The permissions to unset.
/// A new containing the merged member.
public static List Merge(this IEnumerable builderList, DiscordMember member, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
=> builderList.Merge(OverwriteType.Member, member.Id, allowed, denied, unset);
///
/// Merges new permissions for role with role's existing permissions.
///
/// The PermissionOverwrites to apply this to.
/// The role of which to modify their permissions.
/// The permissions to allow.
/// The permissions deny.
/// The permissions to unset.
/// A new containing the merged role.
public static List Merge(this IEnumerable builderList, DiscordRole role, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
=> builderList.Merge(OverwriteType.Role, role.Id, allowed, denied, unset);
///
public static List Merge(this IEnumerable builderList, DiscordMember member, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
=> builderList.Select(x => new DiscordOverwriteBuilder(x)).Merge(OverwriteType.Member, member.Id, allowed, denied, unset);
///
public static List Merge(this IEnumerable builderList, DiscordRole role, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
=> builderList.Select(x => new DiscordOverwriteBuilder(x)).Merge(OverwriteType.Role, role.Id, allowed, denied, unset);
///
public static List Merge(this IEnumerable builderList, OverwriteType type, ulong target, Permissions allowed, Permissions denied, Permissions unset = Permissions.None)
=> builderList.Select(x => new DiscordOverwriteBuilder(x)).Merge(type, target, allowed, denied, unset);
}
internal struct DiscordRestOverwrite
{
///
/// Determines what is allowed.
///
[JsonProperty("allow", NullValueHandling = NullValueHandling.Ignore)]
internal Permissions Allow { get; set; }
///
/// Determines what is denied.
///
[JsonProperty("deny", NullValueHandling = NullValueHandling.Ignore)]
internal Permissions Deny { get; set; }
///
/// Gets or sets the id.
///
[JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)]
internal ulong Id { get; set; }
///
/// Gets or sets the overwrite type.
///
[JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)]
internal OverwriteType Type { get; set; }
}