From 4caf4d0906483baa5d71b7ac439416b1bede68c1 Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 6 Oct 2015 23:39:47 -0300 Subject: [PATCH] Loosened type restrictions on EditMember's roles --- src/Discord.Net/API/Requests.cs | 7 ++++--- src/Discord.Net/DiscordAPIClient.cs | 3 ++- src/Discord.Net/DiscordClient.API.cs | 31 +++++++++++++++++++--------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Discord.Net/API/Requests.cs b/src/Discord.Net/API/Requests.cs index d7edb0c2f..c1808fbd5 100644 --- a/src/Discord.Net/API/Requests.cs +++ b/src/Discord.Net/API/Requests.cs @@ -3,6 +3,7 @@ #pragma warning disable CS0169 using Newtonsoft.Json; +using System.Collections.Generic; namespace Discord.API { @@ -64,7 +65,7 @@ namespace Discord.API [JsonProperty(PropertyName = "deaf", NullValueHandling = NullValueHandling.Ignore)] public bool? Deaf; [JsonProperty(PropertyName = "roles", NullValueHandling = NullValueHandling.Ignore)] - public string[] Roles; + public IEnumerable Roles; } //Messages @@ -73,7 +74,7 @@ namespace Discord.API [JsonProperty("content")] public string Content; [JsonProperty("mentions")] - public string[] Mentions; + public IEnumerable Mentions; [JsonProperty("nonce", NullValueHandling = NullValueHandling.Ignore)] public string Nonce; [JsonProperty("tts", NullValueHandling = NullValueHandling.Ignore)] @@ -84,7 +85,7 @@ namespace Discord.API [JsonProperty("content", NullValueHandling = NullValueHandling.Ignore)] public string Content; [JsonProperty("mentions", NullValueHandling = NullValueHandling.Ignore)] - public string[] Mentions; + public IEnumerable Mentions; } //Permissions diff --git a/src/Discord.Net/DiscordAPIClient.cs b/src/Discord.Net/DiscordAPIClient.cs index f8c87126b..2e6b35d85 100644 --- a/src/Discord.Net/DiscordAPIClient.cs +++ b/src/Discord.Net/DiscordAPIClient.cs @@ -1,5 +1,6 @@ using Discord.API; using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -133,7 +134,7 @@ namespace Discord } //Members - public Task EditMember(string serverId, string userId, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(string serverId, string userId, bool? mute = null, bool? deaf = null, IEnumerable roles = null) { if (serverId == null) throw new ArgumentNullException(nameof(serverId)); if (userId == null) throw new ArgumentNullException(nameof(userId)); diff --git a/src/Discord.Net/DiscordClient.API.cs b/src/Discord.Net/DiscordClient.API.cs index 04044f3b9..178cb3731 100644 --- a/src/Discord.Net/DiscordClient.API.cs +++ b/src/Discord.Net/DiscordClient.API.cs @@ -222,21 +222,31 @@ namespace Discord } //Members - public Task EditMember(Member member, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(Member member, bool? mute = null, bool? deaf = null, IEnumerable roles = null) => EditMember(member?.ServerId, member?.UserId, mute, deaf, roles); - public Task EditMember(Server server, User user, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(Server server, User user, bool? mute = null, bool? deaf = null, IEnumerable roles = null) => EditMember(server?.Id, user?.Id, mute, deaf, roles); - public Task EditMember(Server server, string userId, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(Server server, string userId, bool? mute = null, bool? deaf = null, IEnumerable roles = null) => EditMember(server?.Id, userId, mute, deaf, roles); - public Task EditMember(string serverId, User user, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(string serverId, User user, bool? mute = null, bool? deaf = null, IEnumerable roles = null) => EditMember(serverId, user?.Id, mute, deaf, roles); - public Task EditMember(string serverId, string userId, bool? mute = null, bool? deaf = null, string[] roles = null) + public Task EditMember(string serverId, string userId, bool? mute = null, bool? deaf = null, IEnumerable roles = null) { CheckReady(); if (serverId == null) throw new NullReferenceException(nameof(serverId)); if (userId == null) throw new NullReferenceException(nameof(userId)); - return _api.EditMember(serverId, userId, mute, deaf, roles); + IEnumerable newRoles = roles?.Select(x => + { + if (x is string) + return x as string; + else if (x is Role) + return (x as Role).Id; + else + throw new ArgumentException("Roles must be a collection of strings or roles.", nameof(roles)); + }); + + return _api.EditMember(serverId, userId, mute, deaf, newRoles); } //Messages @@ -263,7 +273,7 @@ namespace Discord CheckReady(); if (channel == null) throw new ArgumentNullException(nameof(channel)); if (text == null) throw new ArgumentNullException(nameof(text)); - if (mentions == null) throw new ArgumentNullException(nameof(mentions)); + mentions = mentions ?? new string[0]; int blockCount = (int)Math.Ceiling(text.Length / (double)MaxMessageSize); Message[] result = new Message[blockCount]; @@ -330,19 +340,20 @@ namespace Discord /// Edits the provided message, changing only non-null attributes. /// While not required, it is recommended to include a mention reference in the text (see Mention.User). - public Task EditMessage(Message message, string text = null, string[] mentions = null) + public Task EditMessage(Message message, string text = null, params string[] mentions) => EditMessage(message?.ChannelId, message?.Id, text, mentions); /// Edits the provided message, changing only non-null attributes. /// While not required, it is recommended to include a mention reference in the text (see Mention.User). - public Task EditMessage(Channel channel, string messageId, string text = null, string[] mentions = null) + public Task EditMessage(Channel channel, string messageId, string text = null, params string[] mentions) => EditMessage(channel?.Id, messageId, text, mentions); /// Edits the provided message, changing only non-null attributes. /// While not required, it is recommended to include a mention reference in the text (see Mention.User). - public async Task EditMessage(string channelId, string messageId, string text = null, string[] mentions = null) + public async Task EditMessage(string channelId, string messageId, string text = null, params string[] mentions) { CheckReady(); if (channelId == null) throw new ArgumentNullException(nameof(channelId)); if (messageId == null) throw new ArgumentNullException(nameof(messageId)); + mentions = mentions ?? new string[0]; if (text != null && text.Length > MaxMessageSize) text = text.Substring(0, MaxMessageSize);