Simplified ToString, added debug strings

This commit is contained in:
RogueException
2016-05-15 03:22:40 -03:00
parent 4365aced85
commit 7b68b4a860
30 changed files with 146 additions and 66 deletions

View File

@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
namespace Discord.API
{
//Based on https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Nullable.cs
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct Optional<T> : IOptional
{
private readonly T _value;
@@ -40,7 +42,8 @@ namespace Discord.API
}
public override int GetHashCode() => IsSpecified ? _value.GetHashCode() : 0;
public override string ToString() => IsSpecified ? _value.ToString() : "";
public override string ToString() => IsSpecified ? _value?.ToString() : null;
private string DebuggerDisplay => IsSpecified ? _value.ToString() : "<unspecified>";
public static implicit operator Optional<T>(T value) => new Optional<T>(value);
public static implicit operator T(Optional<T> value) => value.Value;

View File

@@ -1,5 +1,8 @@
namespace Discord
using System.Diagnostics;
namespace Discord
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public struct IntegrationAccount
{
/// <inheritdoc />
@@ -8,6 +11,7 @@
/// <inheritdoc />
public string Name { get; private set; }
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
}
}

View File

@@ -1,7 +1,9 @@
using Model = Discord.API.VoiceRegion;
using System.Diagnostics;
using Model = Discord.API.VoiceRegion;
namespace Discord.Rest
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class VoiceRegion : IVoiceRegion
{
/// <inheritdoc />
@@ -27,6 +29,7 @@ namespace Discord.Rest
SamplePort = model.SamplePort;
}
public override string ToString() => $"{Name ?? Id.ToString()}";
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}{(IsVip ? ", VIP" : "")}{(IsOptimal ? ", Optimal" : "")})";
}
}

View File

@@ -1,8 +1,10 @@
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Invite;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Invite : IInvite
{
/// <inheritdoc />
@@ -56,6 +58,7 @@ namespace Discord
/// <inheritdoc />
public override string ToString() => XkcdUrl ?? Url;
private string DebuggerDisplay => $"{XkcdUrl ?? Url} ({GuildName} / {ChannelName})";
string IEntity<string>.Id => Code;
}

View File

@@ -1,6 +1,6 @@
namespace Discord
{
internal enum ChannelPermission : byte
public enum ChannelPermission : byte
{
//General
CreateInstantInvite = 0,

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Discord
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public struct ChannelPermissions
{
private static ChannelPermissions _allDM { get; } = new ChannelPermissions(0b000100_000000_0011111111_0000011001);
@@ -117,20 +119,19 @@ namespace Discord
embedLinks, attachFiles, readMessageHistory, mentionEveryone, connect, speak, muteMembers, deafenMembers,
moveMembers, useVoiceActivation, managePermissions);
/// <inheritdoc />
public override string ToString()
public List<ChannelPermission> ToList()
{
var perms = new List<string>();
var perms = new List<ChannelPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)
{
if ((RawValue & x) != 0)
{
if (Enum.IsDefined(typeof(ChannelPermission), i))
perms.Add($"{(ChannelPermission)i}");
}
perms.Add((ChannelPermission)i);
}
return string.Join(", ", perms);
return perms;
}
/// <inheritdoc />
public override string ToString() => RawValue.ToString();
private string DebuggerDisplay => $"{RawValue} ({string.Join(", ", ToList())})";
}
}

View File

@@ -1,6 +1,6 @@
namespace Discord
{
internal enum GuildPermission : byte
public enum GuildPermission : byte
{
//General
CreateInstantInvite = 0,

View File

@@ -1,7 +1,10 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct GuildPermissions
{
/// <summary> Gets a blank GuildPermissions that grants no permissions. </summary>
@@ -125,21 +128,20 @@ namespace Discord
=> new GuildPermissions(RawValue, createInstantInvite, manageRoles, kickMembers, banMembers, manageChannels, manageGuild, readMessages,
sendMessages, sendTTSMessages, manageMessages, embedLinks, attachFiles, mentionEveryone, connect, speak, muteMembers, deafenMembers,
moveMembers, useVoiceActivation, changeNickname, manageNicknames, manageRoles);
/// <inheritdoc />
public override string ToString()
public List<GuildPermission> ToList()
{
var perms = new List<string>();
var perms = new List<GuildPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)
{
if ((RawValue & x) != 0)
{
if (System.Enum.IsDefined(typeof(GuildPermission), i))
perms.Add($"{(GuildPermission)i}");
}
perms.Add((GuildPermission)i);
}
return string.Join(", ", perms);
return perms;
}
/// <inheritdoc />
public override string ToString() => RawValue.ToString();
private string DebuggerDisplay => $"{RawValue} ({string.Join(", ", ToList())})";
}
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct OverwritePermissions
{
/// <summary> Gets a blank OverwritePermissions that inherits all permissions. </summary>
@@ -111,25 +113,32 @@ namespace Discord
embedLinks, attachFiles, readMessageHistory, mentionEveryone, connect, speak, muteMembers, deafenMembers,
moveMembers, useVoiceActivation, managePermissions);
/// <inheritdoc />
public override string ToString()
public List<ChannelPermission> ToAllowList()
{
var perms = new List<string>();
var perms = new List<ChannelPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)
{
if ((AllowValue & x) != 0)
{
if (Enum.IsDefined(typeof(GuildPermission), i))
perms.Add($"+{(GuildPermission)i}");
}
else if ((DenyValue & x) != 0)
{
if (Enum.IsDefined(typeof(GuildPermission), i))
perms.Add($"-{(GuildPermission)i}");
}
perms.Add((ChannelPermission)i);
}
return string.Join(", ", perms);
return perms;
}
public List<ChannelPermission> ToDenyList()
{
var perms = new List<ChannelPermission>();
ulong x = 1;
for (byte i = 0; i < Permissions.MaxBits; i++, x <<= 1)
{
if ((DenyValue & x) != 0)
perms.Add((ChannelPermission)i);
}
return perms;
}
/// <inheritdoc />
public override string ToString() => $"Allow {AllowValue}, Deny {DenyValue}";
private string DebuggerDisplay =>
$"Allow {AllowValue} ({string.Join(", ", ToAllowList())})\n" +
$"Deny {DenyValue} ({string.Join(", ", ToDenyList())})";
}
}

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic;
using System.Diagnostics;
using Model = Discord.API.Connection;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Connection : IConnection
{
public string Id { get; }
@@ -23,6 +25,7 @@ namespace Discord.Rest
IntegrationIds = model.Integrations;
}
public override string ToString() => $"{Name ?? Id.ToString()} ({Type})";
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}, Type = {Type}{(IsRevoked ? ", Revoked" : "")})";
}
}

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -9,6 +10,7 @@ using Model = Discord.API.Channel;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class DMChannel : IDMChannel
{
/// <inheritdoc />
@@ -121,8 +123,9 @@ namespace Discord.Rest
}
/// <inheritdoc />
public override string ToString() => $"@{Recipient} [DM]";
public override string ToString() => '@' + Recipient.ToString();
private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
IDMUser IDMChannel.Recipient => Recipient;
IEnumerable<IMessage> IMessageChannel.CachedMessages => Array.Empty<Message>();

View File

@@ -148,6 +148,9 @@ namespace Discord.Rest
Update(model);
}
/// <inheritdoc />
public override string ToString() => Name;
IGuild IGuildChannel.Guild => Guild;
async Task<IInviteMetadata> IGuildChannel.CreateInvite(int? maxAge, int? maxUses, bool isTemporary, bool withXkcd)
=> await CreateInvite(maxAge, maxUses, isTemporary, withXkcd).ConfigureAwait(false);

View File

@@ -1,6 +1,7 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -8,6 +9,7 @@ using Model = Discord.API.Channel;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class TextChannel : GuildChannel, ITextChannel
{
/// <inheritdoc />
@@ -103,8 +105,7 @@ namespace Discord.Rest
await Discord.BaseClient.TriggerTypingIndicator(Id).ConfigureAwait(false);
}
/// <inheritdoc />
public override string ToString() => $"{base.ToString()} [Text]";
private string DebuggerDisplay => $"{Name} ({Id}, Text)";
IEnumerable<IMessage> IMessageChannel.CachedMessages => Array.Empty<Message>();

View File

@@ -1,11 +1,13 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class VoiceChannel : GuildChannel, IVoiceChannel
{
/// <inheritdoc />
@@ -35,7 +37,6 @@ namespace Discord.Rest
public override Task<GuildUser> GetUser(ulong id) { throw new NotSupportedException(); }
public override Task<IEnumerable<GuildUser>> GetUsers() { throw new NotSupportedException(); }
/// <inheritdoc />
public override string ToString() => $"{base.ToString()} [Voice]";
private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
}
}

View File

@@ -8,10 +8,12 @@ using System.Threading.Tasks;
using Model = Discord.API.Guild;
using EmbedModel = Discord.API.GuildEmbed;
using RoleModel = Discord.API.Role;
using System.Diagnostics;
namespace Discord.Rest
{
/// <summary> Represents a Discord guild (called a server in the official client). </summary>
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Guild : IGuild
{
private ConcurrentDictionary<ulong, Role> _roles;
@@ -332,7 +334,8 @@ namespace Discord.Rest
}
}
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
IEnumerable<Emoji> IGuild.Emojis => Emojis;
ulong IGuild.EveryoneRoleId => EveryoneRole.Id;

View File

@@ -1,8 +1,10 @@
using System;
using System.Diagnostics;
using Model = Discord.API.GuildEmbed;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class GuildEmbed : IGuildEmbed
{
/// <inheritdoc />
@@ -26,6 +28,7 @@ namespace Discord
IsEnabled = model.Enabled;
}
public override string ToString() => $"{Id} ({(IsEnabled ? "Enabled" : "Disabled")})";
public override string ToString() => Id.ToString();
private string DebuggerDisplay => $"{Id}{(IsEnabled ? " (Enabled)" : "")}";
}
}

View File

@@ -1,10 +1,12 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Integration;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class GuildIntegration : IGuildIntegration
{
/// <inheritdoc />
@@ -77,7 +79,8 @@ namespace Discord.Rest
await Discord.BaseClient.SyncGuildIntegration(Guild.Id, Id).ConfigureAwait(false);
}
public override string ToString() => $"{Name ?? Id.ToString()} ({(IsEnabled ? "Enabled" : "Disabled")})";
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})";
IGuild IGuildIntegration.Guild => Guild;
IRole IGuildIntegration.Role => Role;

View File

@@ -1,9 +1,11 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.UserGuild;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class UserGuild : IUserGuild
{
private string _iconId;
@@ -48,6 +50,7 @@ namespace Discord
await Discord.BaseClient.DeleteGuild(Id).ConfigureAwait(false);
}
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}{(IsOwner ? ", Owned" : "")})";
}
}

View File

@@ -2,11 +2,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Message;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Message : IMessage
{
/// <inheritdoc />
@@ -135,7 +137,8 @@ namespace Discord.Rest
await Discord.BaseClient.DeleteMessage(Channel.Id, Id).ConfigureAwait(false);
}
public override string ToString() => $"{Author.ToString()}: {Text}";
public override string ToString() => Text;
private string DebuggerDisplay => $"{Author}: {Text}";
IUser IMessage.Author => Author;
IReadOnlyList<Attachment> IMessage.Attachments => Attachments;

View File

@@ -1,12 +1,14 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Role;
namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Role : IRole, IMentionable
{
/// <inheritdoc />
@@ -66,8 +68,9 @@ namespace Discord.Rest
=> await Discord.BaseClient.DeleteGuildRole(Guild.Id, Id).ConfigureAwait(false);
/// <inheritdoc />
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
ulong IRole.GuildId => Guild.Id;
async Task<IEnumerable<IGuildUser>> IRole.GetUsers()

View File

@@ -1,10 +1,12 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
namespace Discord.Rest
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class User : IUser
{
private string _avatarId;
@@ -51,7 +53,8 @@ namespace Discord.Rest
return new DMChannel(Discord, model);
}
public override string ToString() => $"{Username ?? Id.ToString()}";
public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id})";
/// <inheritdoc />
Game? IUser.CurrentGame => null;

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -9,6 +10,7 @@ using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class DMChannel : IDMChannel
{
private readonly MessageCache _messages;
@@ -114,8 +116,9 @@ namespace Discord.WebSocket
}
/// <inheritdoc />
public override string ToString() => $"@{Recipient} [DM]";
public override string ToString() => '@' + Recipient.ToString();
private string DebuggerDisplay => $"@{Recipient} ({Id}, DM)";
IDMUser IDMChannel.Recipient => Recipient;
IEnumerable<IMessage> IMessageChannel.CachedMessages => CachedMessages;

View File

@@ -133,6 +133,9 @@ namespace Discord.WebSocket
await Discord.BaseClient.DeleteChannel(Id).ConfigureAwait(false);
}
/// <inheritdoc />
public override string ToString() => Name;
IGuild IGuildChannel.Guild => Guild;
async Task<IInviteMetadata> IGuildChannel.CreateInvite(int? maxAge, int? maxUses, bool isTemporary, bool withXkcd)
=> await CreateInvite(maxAge, maxUses, isTemporary, withXkcd).ConfigureAwait(false);

View File

@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -9,6 +10,7 @@ using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class TextChannel : GuildChannel, ITextChannel
{
private readonly MessageCache _messages;
@@ -105,8 +107,7 @@ namespace Discord.WebSocket
await Discord.BaseClient.TriggerTypingIndicator(Id).ConfigureAwait(false);
}
/// <inheritdoc />
public override string ToString() => $"{base.ToString()} [Text]";
private string DebuggerDisplay => $"{Name} ({Id}, Text)";
IEnumerable<IMessage> IMessageChannel.CachedMessages => CachedMessages;

View File

@@ -1,12 +1,14 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class VoiceChannel : GuildChannel, IVoiceChannel
{
/// <inheritdoc />
@@ -43,7 +45,6 @@ namespace Discord.WebSocket
return null;
}
/// <inheritdoc />
public override string ToString() => $"{base.ToString()} [Voice]";
private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
}
}

View File

@@ -8,10 +8,12 @@ using System.Threading.Tasks;
using Model = Discord.API.Guild;
using EmbedModel = Discord.API.GuildEmbed;
using RoleModel = Discord.API.Role;
using System.Diagnostics;
namespace Discord.WebSocket
{
/// <summary> Represents a Discord guild (called a server in the official client). </summary>
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Guild : IGuild
{
private ConcurrentDictionary<ulong, Role> _roles;
@@ -325,7 +327,8 @@ namespace Discord.WebSocket
}
}
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
IEnumerable<Emoji> IGuild.Emojis => Emojis;
ulong IGuild.EveryoneRoleId => EveryoneRole.Id;

View File

@@ -1,10 +1,12 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Integration;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class GuildIntegration : IGuildIntegration
{
/// <inheritdoc />
@@ -75,7 +77,8 @@ namespace Discord.WebSocket
await Discord.BaseClient.SyncGuildIntegration(Guild.Id, Id).ConfigureAwait(false);
}
public override string ToString() => $"{Name ?? Id.ToString()} ({(IsEnabled ? "Enabled" : "Disabled")})";
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})";
IGuild IGuildIntegration.Guild => Guild;
IRole IGuildIntegration.Role => Role;

View File

@@ -2,11 +2,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.Message;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Message : IMessage
{
/// <inheritdoc />
@@ -133,7 +135,8 @@ namespace Discord.WebSocket
await Discord.BaseClient.DeleteMessage(Channel.Id, Id).ConfigureAwait(false);
}
public override string ToString() => $"{Author.ToString()}: {Text}";
public override string ToString() => Text;
private string DebuggerDisplay => $"{Author}: {Text}";
IUser IMessage.Author => Author;
IReadOnlyList<Attachment> IMessage.Attachments => Attachments;

View File

@@ -1,12 +1,14 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Role;
namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class Role : IRole, IMentionable
{
/// <inheritdoc />
@@ -65,8 +67,9 @@ namespace Discord.WebSocket
=> await Discord.BaseClient.DeleteGuildRole(Guild.Id, Id).ConfigureAwait(false);
/// <inheritdoc />
public override string ToString() => Name ?? Id.ToString();
public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})";
ulong IRole.GuildId => Guild.Id;
async Task<IEnumerable<IGuildUser>> IRole.GetUsers()

View File

@@ -1,10 +1,12 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
namespace Discord.WebSocket
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public abstract class User : IUser
{
private string _avatarId;
@@ -51,7 +53,8 @@ namespace Discord.WebSocket
return new DMChannel(Discord, model);
}
public override string ToString() => $"{Username ?? Id.ToString()}";
public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id})";
/// <inheritdoc />
Game? IUser.CurrentGame => null;