* Use 'switch' expression * Reverted it to the old switch casepull/1923/head
@@ -190,37 +190,26 @@ namespace Discord | |||||
private static string FormatToExtension(StickerFormatType format) | private static string FormatToExtension(StickerFormatType format) | ||||
{ | { | ||||
switch (format) | |||||
return format switch | |||||
{ | { | ||||
case StickerFormatType.None: | |||||
case StickerFormatType.Png: | |||||
case StickerFormatType.Apng: // In the case of the Sticker endpoint, the sticker will be available as PNG if its format_type is PNG or APNG, and as Lottie if its format_type is LOTTIE. | |||||
return "png"; | |||||
case StickerFormatType.Lottie: | |||||
return "lottie"; | |||||
default: | |||||
throw new ArgumentException(nameof(format)); | |||||
} | |||||
StickerFormatType.None or StickerFormatType.Png or StickerFormatType.Apng => "png", // In the case of the Sticker endpoint, the sticker will be available as PNG if its format_type is PNG or APNG, and as Lottie if its format_type is LOTTIE. | |||||
StickerFormatType.Lottie => "lottie", | |||||
_ => throw new ArgumentException(nameof(format)), | |||||
}; | |||||
} | } | ||||
private static string FormatToExtension(ImageFormat format, string imageId) | private static string FormatToExtension(ImageFormat format, string imageId) | ||||
{ | { | ||||
if (format == ImageFormat.Auto) | if (format == ImageFormat.Auto) | ||||
format = imageId.StartsWith("a_") ? ImageFormat.Gif : ImageFormat.Png; | format = imageId.StartsWith("a_") ? ImageFormat.Gif : ImageFormat.Png; | ||||
switch (format) | |||||
return format switch | |||||
{ | { | ||||
case ImageFormat.Gif: | |||||
return "gif"; | |||||
case ImageFormat.Jpeg: | |||||
return "jpeg"; | |||||
case ImageFormat.Png: | |||||
return "png"; | |||||
case ImageFormat.WebP: | |||||
return "webp"; | |||||
default: | |||||
throw new ArgumentException(nameof(format)); | |||||
} | |||||
ImageFormat.Gif => "gif", | |||||
ImageFormat.Jpeg => "jpeg", | |||||
ImageFormat.Png => "png", | |||||
ImageFormat.WebP => "webp", | |||||
_ => throw new ArgumentException(nameof(format)), | |||||
}; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -53,21 +53,13 @@ namespace Discord | |||||
if (value == null) | if (value == null) | ||||
throw new ArgumentNullException("Value cannot be null"); | throw new ArgumentNullException("Value cannot be null"); | ||||
switch (value) | |||||
_value = value switch | |||||
{ | { | ||||
case string str: | |||||
_value = str; | |||||
break; | |||||
case int integer: | |||||
_value = integer; | |||||
break; | |||||
case double number: | |||||
_value = number; | |||||
break; | |||||
default: | |||||
throw new ArgumentException($"Type {value.GetType().Name} cannot be set as a value! Only string, int, and double allowed!"); | |||||
} | |||||
string str => str, | |||||
int integer => integer, | |||||
double number => number, | |||||
_ => throw new ArgumentException($"Type {value.GetType().Name} cannot be set as a value! Only string, int, and double allowed!"), | |||||
}; | |||||
} | } | ||||
} | } | ||||
@@ -24,15 +24,15 @@ namespace Discord | |||||
/// <exception cref="ArgumentException">Unknown channel type.</exception> | /// <exception cref="ArgumentException">Unknown channel type.</exception> | ||||
public static ChannelPermissions All(IChannel channel) | public static ChannelPermissions All(IChannel channel) | ||||
{ | { | ||||
switch (channel) | |||||
return channel switch | |||||
{ | { | ||||
case ITextChannel _: return Text; | |||||
case IVoiceChannel _: return Voice; | |||||
case ICategoryChannel _: return Category; | |||||
case IDMChannel _: return DM; | |||||
case IGroupChannel _: return Group; | |||||
default: throw new ArgumentException(message: "Unknown channel type.", paramName: nameof(channel)); | |||||
} | |||||
ITextChannel _ => Text, | |||||
IVoiceChannel _ => Voice, | |||||
ICategoryChannel _ => Category, | |||||
IDMChannel _ => DM, | |||||
IGroupChannel _ => Group, | |||||
_ => throw new ArgumentException(message: "Unknown channel type.", paramName: nameof(channel)), | |||||
}; | |||||
} | } | ||||
/// <summary> Gets a packed value representing all the permissions in this <see cref="ChannelPermissions"/>. </summary> | /// <summary> Gets a packed value representing all the permissions in this <see cref="ChannelPermissions"/>. </summary> | ||||
@@ -22,15 +22,12 @@ namespace Discord.API | |||||
Type = c.Type; | Type = c.Type; | ||||
Components = c.Components?.Select<IMessageComponent, IMessageComponent>(x => | Components = c.Components?.Select<IMessageComponent, IMessageComponent>(x => | ||||
{ | { | ||||
switch (x.Type) | |||||
return x.Type switch | |||||
{ | { | ||||
case ComponentType.Button: | |||||
return new ButtonComponent(x as Discord.ButtonComponent); | |||||
case ComponentType.SelectMenu: | |||||
return new SelectMenuComponent(x as Discord.SelectMenuComponent); | |||||
default: return null; | |||||
} | |||||
ComponentType.Button => new ButtonComponent(x as Discord.ButtonComponent), | |||||
ComponentType.SelectMenu => new SelectMenuComponent(x as Discord.SelectMenuComponent), | |||||
_ => null, | |||||
}; | |||||
}).ToArray(); | }).ToArray(); | ||||
} | } | ||||
@@ -715,22 +715,12 @@ namespace Discord.API | |||||
int limit = args.Limit.GetValueOrDefault(DiscordConfig.MaxMessagesPerBatch); | int limit = args.Limit.GetValueOrDefault(DiscordConfig.MaxMessagesPerBatch); | ||||
ulong? relativeId = args.RelativeMessageId.IsSpecified ? args.RelativeMessageId.Value : (ulong?)null; | ulong? relativeId = args.RelativeMessageId.IsSpecified ? args.RelativeMessageId.Value : (ulong?)null; | ||||
string relativeDir; | |||||
switch (args.RelativeDirection.GetValueOrDefault(Direction.Before)) | |||||
var relativeDir = args.RelativeDirection.GetValueOrDefault(Direction.Before) switch | |||||
{ | { | ||||
case Direction.Before: | |||||
default: | |||||
relativeDir = "before"; | |||||
break; | |||||
case Direction.After: | |||||
relativeDir = "after"; | |||||
break; | |||||
case Direction.Around: | |||||
relativeDir = "around"; | |||||
break; | |||||
} | |||||
Direction.After => "after", | |||||
Direction.Around => "around", | |||||
_ => "before", | |||||
}; | |||||
var ids = new BucketIds(channelId: channelId); | var ids = new BucketIds(channelId: channelId); | ||||
Expression<Func<string>> endpoint; | Expression<Func<string>> endpoint; | ||||
if (relativeId != null) | if (relativeId != null) | ||||
@@ -2181,15 +2171,14 @@ namespace Discord.API | |||||
internal static int? GetIndex(string name) | internal static int? GetIndex(string name) | ||||
{ | { | ||||
switch (name) | |||||
return name switch | |||||
{ | { | ||||
case "httpMethod": return 0; | |||||
case "guildId": return 1; | |||||
case "channelId": return 2; | |||||
case "webhookId": return 3; | |||||
default: | |||||
return null; | |||||
} | |||||
"httpMethod" => 0, | |||||
"guildId" => 1, | |||||
"channelId" => 2, | |||||
"webhookId" => 3, | |||||
_ => null, | |||||
}; | |||||
} | } | ||||
} | } | ||||
@@ -22,33 +22,23 @@ namespace Discord.Rest | |||||
/// <exception cref="InvalidOperationException">Unexpected channel type.</exception> | /// <exception cref="InvalidOperationException">Unexpected channel type.</exception> | ||||
internal static RestChannel Create(BaseDiscordClient discord, Model model) | internal static RestChannel Create(BaseDiscordClient discord, Model model) | ||||
{ | { | ||||
switch (model.Type) | |||||
return model.Type switch | |||||
{ | { | ||||
case ChannelType.News: | |||||
case ChannelType.Text: | |||||
case ChannelType.Voice: | |||||
return RestGuildChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model); | |||||
case ChannelType.DM: | |||||
case ChannelType.Group: | |||||
return CreatePrivate(discord, model) as RestChannel; | |||||
case ChannelType.Category: | |||||
return RestCategoryChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model); | |||||
default: | |||||
return new RestChannel(discord, model.Id); | |||||
} | |||||
ChannelType.News or ChannelType.Text or ChannelType.Voice => RestGuildChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model), | |||||
ChannelType.DM or ChannelType.Group => CreatePrivate(discord, model) as RestChannel, | |||||
ChannelType.Category => RestCategoryChannel.Create(discord, new RestGuild(discord, model.GuildId.Value), model), | |||||
_ => new RestChannel(discord, model.Id), | |||||
}; | |||||
} | } | ||||
/// <exception cref="InvalidOperationException">Unexpected channel type.</exception> | /// <exception cref="InvalidOperationException">Unexpected channel type.</exception> | ||||
internal static IRestPrivateChannel CreatePrivate(BaseDiscordClient discord, Model model) | internal static IRestPrivateChannel CreatePrivate(BaseDiscordClient discord, Model model) | ||||
{ | { | ||||
switch (model.Type) | |||||
return model.Type switch | |||||
{ | { | ||||
case ChannelType.DM: | |||||
return RestDMChannel.Create(discord, model); | |||||
case ChannelType.Group: | |||||
return RestGroupChannel.Create(discord, model); | |||||
default: | |||||
throw new InvalidOperationException($"Unexpected channel type: {model.Type}"); | |||||
} | |||||
ChannelType.DM => RestDMChannel.Create(discord, model), | |||||
ChannelType.Group => RestGroupChannel.Create(discord, model), | |||||
_ => throw new InvalidOperationException($"Unexpected channel type: {model.Type}"), | |||||
}; | |||||
} | } | ||||
internal virtual void Update(Model model) { } | internal virtual void Update(Model model) { } | ||||
@@ -33,23 +33,16 @@ namespace Discord.Rest | |||||
} | } | ||||
internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild, Model model) | internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild, Model model) | ||||
{ | { | ||||
switch (model.Type) | |||||
return model.Type switch | |||||
{ | { | ||||
case ChannelType.News: | |||||
return RestNewsChannel.Create(discord, guild, model); | |||||
case ChannelType.Text: | |||||
return RestTextChannel.Create(discord, guild, model); | |||||
case ChannelType.Voice: | |||||
return RestVoiceChannel.Create(discord, guild, model); | |||||
case ChannelType.Stage: | |||||
return RestStageChannel.Create(discord, guild, model); | |||||
case ChannelType.Category: | |||||
return RestCategoryChannel.Create(discord, guild, model); | |||||
case ChannelType.PublicThread or ChannelType.PrivateThread or ChannelType.NewsThread: | |||||
return RestThreadChannel.Create(discord, guild, model); | |||||
default: | |||||
return new RestGuildChannel(discord, guild, model.Id); | |||||
} | |||||
ChannelType.News => RestNewsChannel.Create(discord, guild, model), | |||||
ChannelType.Text => RestTextChannel.Create(discord, guild, model), | |||||
ChannelType.Voice => RestVoiceChannel.Create(discord, guild, model), | |||||
ChannelType.Stage => RestStageChannel.Create(discord, guild, model), | |||||
ChannelType.Category => RestCategoryChannel.Create(discord, guild, model), | |||||
ChannelType.PublicThread or ChannelType.PrivateThread or ChannelType.NewsThread => RestThreadChannel.Create(discord, guild, model), | |||||
_ => new RestGuildChannel(discord, guild, model.Id), | |||||
}; | |||||
} | } | ||||
internal override void Update(Model model) | internal override void Update(Model model) | ||||
{ | { | ||||
@@ -43,7 +43,7 @@ namespace Discord.Rest | |||||
RestFollowupMessage entity = RestFollowupMessage.Create(client, model, token, channel); | RestFollowupMessage entity = RestFollowupMessage.Create(client, model, token, channel); | ||||
return entity; | return entity; | ||||
} | } | ||||
#endregion | |||||
#endregion | |||||
#region Global commands | #region Global commands | ||||
public static async Task<RestGlobalCommand> GetGlobalCommandAsync(BaseDiscordClient client, ulong id, | public static async Task<RestGlobalCommand> GetGlobalCommandAsync(BaseDiscordClient client, ulong id, | ||||
@@ -13,28 +13,19 @@ namespace Discord.Net.Converters | |||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||||
{ | { | ||||
switch ((string)reader.Value) | |||||
return (string)reader.Value switch | |||||
{ | { | ||||
case "rich": | |||||
return EmbedType.Rich; | |||||
case "link": | |||||
return EmbedType.Link; | |||||
case "video": | |||||
return EmbedType.Video; | |||||
case "image": | |||||
return EmbedType.Image; | |||||
case "gifv": | |||||
return EmbedType.Gifv; | |||||
case "article": | |||||
return EmbedType.Article; | |||||
case "tweet": | |||||
return EmbedType.Tweet; | |||||
case "html": | |||||
return EmbedType.Html; | |||||
case "application_news": // TODO 2.2 EmbedType.News | |||||
default: | |||||
return EmbedType.Unknown; | |||||
} | |||||
"rich" => EmbedType.Rich, | |||||
"link" => EmbedType.Link, | |||||
"video" => EmbedType.Video, | |||||
"image" => EmbedType.Image, | |||||
"gifv" => EmbedType.Gifv, | |||||
"article" => EmbedType.Article, | |||||
"tweet" => EmbedType.Tweet, | |||||
"html" => EmbedType.Html, | |||||
// TODO 2.2 EmbedType.News | |||||
_ => EmbedType.Unknown, | |||||
}; | |||||
} | } | ||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||||
@@ -1,4 +1,4 @@ | |||||
using Newtonsoft.Json; | |||||
using Newtonsoft.Json; | |||||
using System; | using System; | ||||
namespace Discord.Net.Converters | namespace Discord.Net.Converters | ||||
@@ -13,21 +13,15 @@ namespace Discord.Net.Converters | |||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||||
{ | { | ||||
switch ((string)reader.Value) | |||||
return (string)reader.Value switch | |||||
{ | { | ||||
case "online": | |||||
return UserStatus.Online; | |||||
case "idle": | |||||
return UserStatus.Idle; | |||||
case "dnd": | |||||
return UserStatus.DoNotDisturb; | |||||
case "invisible": | |||||
return UserStatus.Invisible; //Should never happen | |||||
case "offline": | |||||
return UserStatus.Offline; | |||||
default: | |||||
throw new JsonSerializationException("Unknown user status"); | |||||
} | |||||
"online" => UserStatus.Online, | |||||
"idle" => UserStatus.Idle, | |||||
"dnd" => UserStatus.DoNotDisturb, | |||||
"invisible" => UserStatus.Invisible,//Should never happen | |||||
"offline" => UserStatus.Offline, | |||||
_ => throw new JsonSerializationException("Unknown user status"), | |||||
}; | |||||
} | } | ||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||||
@@ -157,15 +157,15 @@ namespace Discord.Net.Rest | |||||
private static readonly HttpMethod Patch = new HttpMethod("PATCH"); | private static readonly HttpMethod Patch = new HttpMethod("PATCH"); | ||||
private HttpMethod GetMethod(string method) | private HttpMethod GetMethod(string method) | ||||
{ | { | ||||
switch (method) | |||||
return method switch | |||||
{ | { | ||||
case "DELETE": return HttpMethod.Delete; | |||||
case "GET": return HttpMethod.Get; | |||||
case "PATCH": return Patch; | |||||
case "POST": return HttpMethod.Post; | |||||
case "PUT": return HttpMethod.Put; | |||||
default: throw new ArgumentOutOfRangeException(nameof(method), $"Unknown HttpMethod: {method}"); | |||||
} | |||||
"DELETE" => HttpMethod.Delete, | |||||
"GET" => HttpMethod.Get, | |||||
"PATCH" => Patch, | |||||
"POST" => HttpMethod.Post, | |||||
"PUT" => HttpMethod.Put, | |||||
_ => throw new ArgumentOutOfRangeException(nameof(method), $"Unknown HttpMethod: {method}"), | |||||
}; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -31,15 +31,12 @@ namespace Discord.WebSocket | |||||
/// <exception cref="InvalidOperationException">Unexpected channel type is created.</exception> | /// <exception cref="InvalidOperationException">Unexpected channel type is created.</exception> | ||||
internal static ISocketPrivateChannel CreatePrivate(DiscordSocketClient discord, ClientState state, Model model) | internal static ISocketPrivateChannel CreatePrivate(DiscordSocketClient discord, ClientState state, Model model) | ||||
{ | { | ||||
switch (model.Type) | |||||
return model.Type switch | |||||
{ | { | ||||
case ChannelType.DM: | |||||
return SocketDMChannel.Create(discord, state, model); | |||||
case ChannelType.Group: | |||||
return SocketGroupChannel.Create(discord, state, model); | |||||
default: | |||||
throw new InvalidOperationException($"Unexpected channel type: {model.Type}"); | |||||
} | |||||
ChannelType.DM => SocketDMChannel.Create(discord, state, model), | |||||
ChannelType.Group => SocketGroupChannel.Create(discord, state, model), | |||||
_ => throw new InvalidOperationException($"Unexpected channel type: {model.Type}"), | |||||
}; | |||||
} | } | ||||
internal abstract void Update(ClientState state, Model model); | internal abstract void Update(ClientState state, Model model); | ||||
#endregion | #endregion | ||||
@@ -79,13 +79,13 @@ namespace Discord.WebSocket | |||||
public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | public static SocketMessage RemoveMessage(ISocketMessageChannel channel, DiscordSocketClient discord, | ||||
ulong id) | ulong id) | ||||
{ | { | ||||
switch (channel) | |||||
return channel switch | |||||
{ | { | ||||
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 {nameof(ISocketMessageChannel)} type."); | |||||
} | |||||
SocketDMChannel dmChannel => dmChannel.RemoveMessage(id), | |||||
SocketGroupChannel groupChannel => groupChannel.RemoveMessage(id), | |||||
SocketTextChannel textChannel => textChannel.RemoveMessage(id), | |||||
_ => throw new NotSupportedException($"Unexpected {nameof(ISocketMessageChannel)} type."), | |||||
}; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -47,23 +47,16 @@ namespace Discord.WebSocket | |||||
} | } | ||||
internal static SocketGuildChannel Create(SocketGuild guild, ClientState state, Model model) | internal static SocketGuildChannel Create(SocketGuild guild, ClientState state, Model model) | ||||
{ | { | ||||
switch (model.Type) | |||||
return model.Type switch | |||||
{ | { | ||||
case ChannelType.News: | |||||
return SocketNewsChannel.Create(guild, state, model); | |||||
case ChannelType.Text: | |||||
return SocketTextChannel.Create(guild, state, model); | |||||
case ChannelType.Voice: | |||||
return SocketVoiceChannel.Create(guild, state, model); | |||||
case ChannelType.Category: | |||||
return SocketCategoryChannel.Create(guild, state, model); | |||||
case ChannelType.PrivateThread or ChannelType.PublicThread or ChannelType.NewsThread: | |||||
return SocketThreadChannel.Create(guild, state, model); | |||||
case ChannelType.Stage: | |||||
return SocketStageChannel.Create(guild, state, model); | |||||
default: | |||||
return new SocketGuildChannel(guild.Discord, model.Id, guild); | |||||
} | |||||
ChannelType.News => SocketNewsChannel.Create(guild, state, model), | |||||
ChannelType.Text => SocketTextChannel.Create(guild, state, model), | |||||
ChannelType.Voice => SocketVoiceChannel.Create(guild, state, model), | |||||
ChannelType.Category => SocketCategoryChannel.Create(guild, state, model), | |||||
ChannelType.PrivateThread or ChannelType.PublicThread or ChannelType.NewsThread => SocketThreadChannel.Create(guild, state, model), | |||||
ChannelType.Stage => SocketStageChannel.Create(guild, state, model), | |||||
_ => new SocketGuildChannel(guild.Discord, model.Id, guild), | |||||
}; | |||||
} | } | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
internal override void Update(ClientState state, Model model) | internal override void Update(ClientState state, Model model) | ||||
@@ -72,16 +72,13 @@ namespace Discord.WebSocket | |||||
if (dataModel == null) | if (dataModel == null) | ||||
return null; | return null; | ||||
switch (dataModel.Type) | |||||
return dataModel.Type switch | |||||
{ | { | ||||
case ApplicationCommandType.Slash: | |||||
return SocketSlashCommand.Create(client, model, channel); | |||||
case ApplicationCommandType.Message: | |||||
return SocketMessageCommand.Create(client, model, channel); | |||||
case ApplicationCommandType.User: | |||||
return SocketUserCommand.Create(client, model, channel); | |||||
default: return null; | |||||
} | |||||
ApplicationCommandType.Slash => SocketSlashCommand.Create(client, model, channel), | |||||
ApplicationCommandType.Message => SocketMessageCommand.Create(client, model, channel), | |||||
ApplicationCommandType.User => SocketUserCommand.Create(client, model, channel), | |||||
_ => null, | |||||
}; | |||||
} | } | ||||
else if (model.Type == InteractionType.MessageComponent) | else if (model.Type == InteractionType.MessageComponent) | ||||
return SocketMessageComponent.Create(client, model, channel); | return SocketMessageComponent.Create(client, model, channel); | ||||
@@ -31,16 +31,16 @@ namespace Discord.WebSocket | |||||
{ | { | ||||
get | get | ||||
{ | { | ||||
switch (Channel) | |||||
return Channel switch | |||||
{ | { | ||||
case IVoiceChannel voiceChannel: return ChannelType.Voice; | |||||
case ICategoryChannel categoryChannel: return ChannelType.Category; | |||||
case IDMChannel dmChannel: return ChannelType.DM; | |||||
case IGroupChannel groupChannel: return ChannelType.Group; | |||||
case INewsChannel newsChannel: return ChannelType.News; | |||||
case ITextChannel textChannel: return ChannelType.Text; | |||||
default: throw new InvalidOperationException("Invalid channel type."); | |||||
} | |||||
IVoiceChannel voiceChannel => ChannelType.Voice, | |||||
ICategoryChannel categoryChannel => ChannelType.Category, | |||||
IDMChannel dmChannel => ChannelType.DM, | |||||
IGroupChannel groupChannel => ChannelType.Group, | |||||
INewsChannel newsChannel => ChannelType.News, | |||||
ITextChannel textChannel => ChannelType.Text, | |||||
_ => throw new InvalidOperationException("Invalid channel type."), | |||||
}; | |||||
} | } | ||||
} | } | ||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||