| @@ -81,23 +81,28 @@ namespace Discord.Commands | |||||
| foreach (var attribute in attributes) | foreach (var attribute in attributes) | ||||
| { | { | ||||
| // TODO: C#7 type switch | |||||
| if (attribute is NameAttribute) | |||||
| builder.Name = (attribute as NameAttribute).Text; | |||||
| else if (attribute is SummaryAttribute) | |||||
| builder.Summary = (attribute as SummaryAttribute).Text; | |||||
| else if (attribute is RemarksAttribute) | |||||
| builder.Remarks = (attribute as RemarksAttribute).Text; | |||||
| else if (attribute is AliasAttribute) | |||||
| builder.AddAliases((attribute as AliasAttribute).Aliases); | |||||
| else if (attribute is GroupAttribute) | |||||
| switch (attribute) | |||||
| { | { | ||||
| var groupAttr = attribute as GroupAttribute; | |||||
| builder.Name = builder.Name ?? groupAttr.Prefix; | |||||
| builder.AddAliases(groupAttr.Prefix); | |||||
| case NameAttribute name: | |||||
| builder.Name = name.Text; | |||||
| break; | |||||
| case SummaryAttribute summary: | |||||
| builder.Summary = summary.Text; | |||||
| break; | |||||
| case RemarksAttribute remarks: | |||||
| builder.Remarks = remarks.Text; | |||||
| break; | |||||
| case AliasAttribute alias: | |||||
| builder.AddAliases(alias.Aliases); | |||||
| break; | |||||
| case GroupAttribute group: | |||||
| builder.Name = builder.Name ?? group.Prefix; | |||||
| builder.AddAliases(group.Prefix); | |||||
| break; | |||||
| case PreconditionAttribute precondition: | |||||
| builder.AddPrecondition(precondition); | |||||
| break; | |||||
| } | } | ||||
| else if (attribute is PreconditionAttribute) | |||||
| builder.AddPrecondition(attribute as PreconditionAttribute); | |||||
| } | } | ||||
| //Check for unspecified info | //Check for unspecified info | ||||
| @@ -20,13 +20,14 @@ namespace Discord | |||||
| /// <summary> Gets a ChannelPermissions that grants all permissions for a given channelType. </summary> | /// <summary> Gets a ChannelPermissions that grants all permissions for a given channelType. </summary> | ||||
| public static ChannelPermissions All(IChannel channel) | public static ChannelPermissions All(IChannel channel) | ||||
| { | { | ||||
| //TODO: C#7 Candidate for typeswitch | |||||
| 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)); | |||||
| switch (channel) | |||||
| { | |||||
| case ITextChannel _: return Text; | |||||
| case IVoiceChannel _: return Voice; | |||||
| case IDMChannel _: return DM; | |||||
| case IGroupChannel _: return Group; | |||||
| default: throw new ArgumentException("Unknown channel type", nameof(channel)); | |||||
| } | |||||
| } | } | ||||
| /// <summary> Gets a packed value representing all the permissions in this ChannelPermissions. </summary> | /// <summary> Gets a packed value representing all the permissions in this ChannelPermissions. </summary> | ||||
| @@ -150,9 +150,7 @@ namespace Discord | |||||
| if (perms != null) | if (perms != null) | ||||
| resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; | resolvedPermissions = (resolvedPermissions & ~perms.Value.DenyValue) | perms.Value.AllowValue; | ||||
| //TODO: C#7 Typeswitch candidate | |||||
| var textChannel = channel as ITextChannel; | |||||
| if (textChannel != null) | |||||
| if (channel is ITextChannel textChannel) | |||||
| { | { | ||||
| if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages)) | if (!GetValue(resolvedPermissions, ChannelPermission.ReadMessages)) | ||||
| { | { | ||||
| @@ -87,29 +87,26 @@ namespace Discord.Net.Rest | |||||
| { | { | ||||
| foreach (var p in multipartParams) | foreach (var p in multipartParams) | ||||
| { | { | ||||
| //TODO: C#7 Typeswitch candidate | |||||
| var stringValue = p.Value as string; | |||||
| if (stringValue != null) { content.Add(new StringContent(stringValue), p.Key); continue; } | |||||
| var byteArrayValue = p.Value as byte[]; | |||||
| if (byteArrayValue != null) { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; } | |||||
| var streamValue = p.Value as Stream; | |||||
| if (streamValue != null) { content.Add(new StreamContent(streamValue), p.Key); continue; } | |||||
| if (p.Value is MultipartFile) | |||||
| switch (p.Value) | |||||
| { | { | ||||
| var fileValue = (MultipartFile)p.Value; | |||||
| var stream = fileValue.Stream; | |||||
| if (!stream.CanSeek) | |||||
| case string stringValue: { content.Add(new StringContent(stringValue), p.Key); continue; } | |||||
| case byte[] byteArrayValue: { content.Add(new ByteArrayContent(byteArrayValue), p.Key); continue; } | |||||
| case Stream streamValue: { content.Add(new StreamContent(streamValue), p.Key); continue; } | |||||
| case MultipartFile fileValue: | |||||
| { | { | ||||
| var memoryStream = new MemoryStream(); | |||||
| await stream.CopyToAsync(memoryStream).ConfigureAwait(false); | |||||
| memoryStream.Position = 0; | |||||
| stream = memoryStream; | |||||
| var stream = fileValue.Stream; | |||||
| if (!stream.CanSeek) | |||||
| { | |||||
| var memoryStream = new MemoryStream(); | |||||
| await stream.CopyToAsync(memoryStream).ConfigureAwait(false); | |||||
| memoryStream.Position = 0; | |||||
| stream = memoryStream; | |||||
| } | |||||
| content.Add(new StreamContent(stream), p.Key, fileValue.Filename); | |||||
| continue; | |||||
| } | } | ||||
| content.Add(new StreamContent(stream), p.Key, fileValue.Filename); | |||||
| continue; | |||||
| default: throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\""); | |||||
| } | } | ||||
| throw new InvalidOperationException($"Unsupported param type \"{p.Value.GetType().Name}\""); | |||||
| } | } | ||||
| } | } | ||||
| restRequest.Content = content; | restRequest.Content = content; | ||||
| @@ -61,28 +61,24 @@ namespace Discord.WebSocket | |||||
| public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | public static void AddMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | ||||
| SocketMessage msg) | SocketMessage msg) | ||||
| { | { | ||||
| //TODO: C#7 Candidate for pattern matching | |||||
| if (channel is SocketDMChannel) | |||||
| (channel as SocketDMChannel).AddMessage(msg); | |||||
| else if (channel is SocketGroupChannel) | |||||
| (channel as SocketGroupChannel).AddMessage(msg); | |||||
| else if (channel is SocketTextChannel) | |||||
| (channel as SocketTextChannel).AddMessage(msg); | |||||
| else | |||||
| throw new NotSupportedException("Unexpected ISocketMessageChannel type"); | |||||
| switch (channel) | |||||
| { | |||||
| case SocketDMChannel dmChannel: dmChannel.AddMessage(msg); break; | |||||
| case SocketGroupChannel groupChannel: groupChannel.AddMessage(msg); break; | |||||
| case SocketTextChannel textChannel: textChannel.AddMessage(msg); break; | |||||
| default: throw new NotSupportedException("Unexpected ISocketMessageChannel type"); | |||||
| } | |||||
| } | } | ||||
| public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | ||||
| ulong id) | ulong id) | ||||
| { | { | ||||
| //TODO: C#7 Candidate for pattern matching | |||||
| if (channel is SocketDMChannel) | |||||
| return (channel as SocketDMChannel).RemoveMessage(id); | |||||
| else if (channel is SocketGroupChannel) | |||||
| return (channel as SocketGroupChannel).RemoveMessage(id); | |||||
| else if (channel is SocketTextChannel) | |||||
| return (channel as SocketTextChannel).RemoveMessage(id); | |||||
| else | |||||
| throw new NotSupportedException("Unexpected ISocketMessageChannel type"); | |||||
| switch (channel) | |||||
| { | |||||
| case SocketDMChannel dmChannel: return dmChannel.RemoveMessage(id); | |||||
| case SocketGroupChannel groupChannel: return groupChannel.RemoveMessage(id); | |||||
| case SocketTextChannel textChannel: return textChannel.RemoveMessage(id); | |||||
| default: throw new NotSupportedException("Unexpected ISocketMessageChannel type"); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||