Added IWebhookUser and MessageSource

This commit is contained in:
RogueException
2017-03-30 23:29:30 -03:00
parent e7401eda68
commit bf0be82d15
32 changed files with 371 additions and 96 deletions

View File

@@ -7,12 +7,12 @@ namespace Discord
{
/// <summary> Gets the type of this system message. </summary>
MessageType Type { get; }
/// <summary> Gets the source of this message. </summary>
MessageSource Source { get; }
/// <summary> Returns true if this message was sent as a text-to-speech message. </summary>
bool IsTTS { get; }
/// <summary> Returns true if this message was added to its channel's pinned messages. </summary>
bool IsPinned { get; }
/// <summary> Returns true if this message was created using a webhook. </summary>
bool IsWebhook { get; }
/// <summary> Returns the content for this message. </summary>
string Content { get; }
/// <summary> Gets the time this message was sent. </summary>
@@ -24,8 +24,6 @@ namespace Discord
IMessageChannel Channel { get; }
/// <summary> Gets the author of this message. </summary>
IUser Author { get; }
/// <summary> Gets the id of the webhook used to created this message, if any. </summary>
ulong? WebhookId { get; }
/// <summary> Returns all attachments included in this message. </summary>
IReadOnlyCollection<IAttachment> Attachments { get; }

View File

@@ -0,0 +1,10 @@
namespace Discord
{
public enum MessageSource
{
System,
User,
Bot,
Webhook
}
}

View File

@@ -7,22 +7,24 @@ namespace Discord
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public struct ChannelPermissions
{
//TODO: C#7 Candidate for binary literals
private static ChannelPermissions _allDM { get; } = new ChannelPermissions(Convert.ToUInt64("00000000000001011100110000000000", 2));
private static ChannelPermissions _allVoice { get; } = new ChannelPermissions(Convert.ToUInt64("00010011111100000000000000010001", 2));
private static ChannelPermissions _allText { get; } = new ChannelPermissions(Convert.ToUInt64("00010000000001111111110001010001", 2));
private static ChannelPermissions _allGroup { get; } = new ChannelPermissions(Convert.ToUInt64("00000000000001111110110000000000", 2));
/// <summary> Gets a blank ChannelPermissions that grants no permissions. </summary>
public static ChannelPermissions None { get; } = new ChannelPermissions();
public static readonly ChannelPermissions None = new ChannelPermissions();
/// <summary> Gets a ChannelPermissions that grants all permissions for text channels. </summary>
public static readonly ChannelPermissions Text = new ChannelPermissions(0b00100_0000000_1111111110001_010001);
/// <summary> Gets a ChannelPermissions that grants all permissions for voice channels. </summary>
public static readonly ChannelPermissions Voice = new ChannelPermissions(0b00100_1111110_0000000000000_010001);
/// <summary> Gets a ChannelPermissions that grants all permissions for direct message channels. </summary>
public static readonly ChannelPermissions DM = new ChannelPermissions(0b00000_1000110_1011100110000_000000);
/// <summary> Gets a ChannelPermissions that grants all permissions for group channels. </summary>
public static readonly ChannelPermissions Group = new ChannelPermissions(0b00000_1000110_0001101100000_000000);
/// <summary> Gets a ChannelPermissions that grants all permissions for a given channelType. </summary>
public static ChannelPermissions All(IChannel channel)
{
//TODO: C#7 Candidate for typeswitch
if (channel is ITextChannel) return _allText;
if (channel is IVoiceChannel) return _allVoice;
if (channel is IDMChannel) return _allDM;
if (channel is IGroupChannel) return _allGroup;
if (channel is ITextChannel) return Text;
if (channel is IVoiceChannel) return Voice;
if (channel is IDMChannel) return DM;
if (channel is IGroupChannel) return Group;
throw new ArgumentException("Unknown channel type", nameof(channel));
}
@@ -77,7 +79,7 @@ namespace Discord
/// <summary> Creates a new ChannelPermissions with the provided packed value. </summary>
public ChannelPermissions(ulong rawValue) { RawValue = rawValue; }
private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null, bool? manageChannel = null,
private ChannelPermissions(ulong initialValue, bool? createInstantInvite = null, bool? manageChannel = null,
bool? addReactions = null,
bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null,
@@ -111,25 +113,26 @@ namespace Discord
}
/// <summary> Creates a new ChannelPermissions with the provided permissions. </summary>
public ChannelPermissions(bool createInstantInvite = false, bool manageChannel = false,
public ChannelPermissions(bool createInstantInvite = false, bool manageChannel = false,
bool addReactions = false,
bool readMessages = false, bool sendMessages = false, bool sendTTSMessages = false, bool manageMessages = false,
bool embedLinks = false, bool attachFiles = false, bool readMessageHistory = false, bool mentionEveryone = false,
bool useExternalEmojis = false, bool connect = false, bool speak = false, bool muteMembers = false, bool deafenMembers = false,
bool moveMembers = false, bool useVoiceActivation = false, bool managePermissions = false, bool manageWebhooks = false)
: this(0, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions, manageWebhooks) { }
: this(0, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions, manageWebhooks)
{ }
/// <summary> Creates a new ChannelPermissions from this one, changing the provided non-null permissions. </summary>
public ChannelPermissions Modify(bool? createInstantInvite = null, bool? manageChannel = null,
public ChannelPermissions Modify(bool? createInstantInvite = null, bool? manageChannel = null,
bool? addReactions = null,
bool? readMessages = null, bool? sendMessages = null, bool? sendTTSMessages = null, bool? manageMessages = null,
bool? embedLinks = null, bool? attachFiles = null, bool? readMessageHistory = null, bool? mentionEveryone = null,
bool useExternalEmojis = false, bool? connect = null, bool? speak = null, bool? muteMembers = null, bool? deafenMembers = null,
bool? moveMembers = null, bool? useVoiceActivation = null, bool? managePermissions = null, bool? manageWebhooks = null)
=> new ChannelPermissions(RawValue, createInstantInvite, manageChannel, addReactions, readMessages, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, managePermissions, manageWebhooks);
public bool Has(ChannelPermission permission) => Permissions.GetValue(RawValue, permission);

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
namespace Discord
@@ -9,9 +8,10 @@ namespace Discord
{
/// <summary> Gets a blank GuildPermissions that grants no permissions. </summary>
public static readonly GuildPermissions None = new GuildPermissions();
/// <summary> Gets a GuildPermissions that grants all permissions. </summary>
//TODO: C#7 Candidate for binary literals
public static readonly GuildPermissions All = new GuildPermissions(Convert.ToUInt64("01111111111100111111110001111111", 2));
/// <summary> Gets a GuildPermissions that grants all guild permissions for webhook users. </summary>
public static readonly GuildPermissions Webhook = new GuildPermissions(0b00000_0000000_0001101100000_000000);
/// <summary> Gets a GuildPermissions that grants all guild permissions. </summary>
public static readonly GuildPermissions All = new GuildPermissions(0b11111_1111110_0111111110001_111111);
/// <summary> Gets a packed value representing all the permissions in this GuildPermissions. </summary>
public ulong RawValue { get; }

View File

@@ -12,8 +12,10 @@ namespace Discord
string Discriminator { get; }
/// <summary> Gets the per-username unique id for this user. </summary>
ushort DiscriminatorValue { get; }
/// <summary> Returns true if this user is a bot account. </summary>
/// <summary> Returns true if this user is a bot user. </summary>
bool IsBot { get; }
/// <summary> Returns true if this user is a webhook user. </summary>
bool IsWebhook { get; }
/// <summary> Gets the username for this user. </summary>
string Username { get; }

View File

@@ -0,0 +1,8 @@
namespace Discord
{
//TODO: Add webhook endpoints
public interface IWebhookUser : IGuildUser
{
ulong WebhookId { get; }
}
}

View File

@@ -37,11 +37,8 @@ namespace Discord
public static async Task InvokeAsync(this AsyncEvent<Func<Task>> eventHandler)
{
var subscribers = eventHandler.Subscriptions;
if (subscribers.Count > 0)
{
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke().ConfigureAwait(false);
}
for (int i = 0; i < subscribers.Count; i++)
await subscribers[i].Invoke().ConfigureAwait(false);
}
public static async Task InvokeAsync<T>(this AsyncEvent<Func<T, Task>> eventHandler, T arg)
{

View File

@@ -51,5 +51,9 @@ namespace Discord
{
public static Optional<T> Create<T>() => Optional<T>.Unspecified;
public static Optional<T> Create<T>(T value) => new Optional<T>(value);
public static T? ToNullable<T>(this Optional<T> val)
where T : struct
=> val.IsSpecified ? val.Value : (T?)null;
}
}

View File

@@ -86,12 +86,16 @@ namespace Discord
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UnsetBit(ref ulong value, byte bit) => value &= ~(1U << bit);
public static ChannelPermissions ToChannelPerms(IGuildChannel channel, ulong guildPermissions)
=> new ChannelPermissions(guildPermissions & ChannelPermissions.All(channel).RawValue);
public static ulong ResolveGuild(IGuild guild, IGuildUser user)
{
ulong resolvedPermissions = 0;
if (user.Id == guild.OwnerId)
resolvedPermissions = GuildPermissions.All.RawValue; //Owners always have all permissions
else if (user.IsWebhook)
resolvedPermissions = GuildPermissions.Webhook.RawValue;
else
{
foreach (var roleId in user.RoleIds)