diff --git a/src/Discord.Net/DiscordAPIClient.cs b/src/Discord.Net/DiscordAPIClient.cs index 438f4edf7..ea75e0459 100644 --- a/src/Discord.Net/DiscordAPIClient.cs +++ b/src/Discord.Net/DiscordAPIClient.cs @@ -173,7 +173,7 @@ namespace Discord } //Messages - public Task SendMessage(string channelId, string message, string[] mentionedUserIds = null, string nonce = null, bool isTTS = false) + public Task SendMessage(string channelId, string message, IEnumerable mentionedUserIds = null, string nonce = null, bool isTTS = false) { if (channelId == null) throw new ArgumentNullException(nameof(channelId)); if (message == null) throw new ArgumentNullException(nameof(message)); @@ -195,12 +195,12 @@ namespace Discord return _rest.Delete(Endpoints.ChannelMessage(channelId, messageId)); } - public Task EditMessage(string messageId, string channelId, string message = null, string[] mentions = null) + public Task EditMessage(string messageId, string channelId, string message = null, IEnumerable mentionedUserIds = null) { if (messageId == null) throw new ArgumentNullException(nameof(messageId)); if (channelId == null) throw new ArgumentNullException(nameof(channelId)); - var request = new EditMessageRequest { Content = message, Mentions = mentions }; + var request = new EditMessageRequest { Content = message, Mentions = mentionedUserIds }; return _rest.Patch(Endpoints.ChannelMessage(channelId, messageId), request); } public Task AckMessage(string messageId, string channelId) diff --git a/src/Discord.Net/DiscordClient.API.cs b/src/Discord.Net/DiscordClient.API.cs index 89b2543e9..b90f23d52 100644 --- a/src/Discord.Net/DiscordClient.API.cs +++ b/src/Discord.Net/DiscordClient.API.cs @@ -151,16 +151,7 @@ namespace Discord if (channels == null) throw new ArgumentNullException(nameof(channels)); if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer."); - var channelIds = channels.Select(x => - { - if (x is string) - return x as string; - else if (x is Channel) - return (x as Channel).Id; - else - throw new ArgumentException("Channels must be a collection of string or Channel.", nameof(channels)); - }); - + var channelIds = CollectionHelper.FlattenChannels(channels); return _api.ReorderChannels(serverId, channelIds, startPos); } @@ -280,44 +271,35 @@ namespace Discord if (serverId == null) throw new NullReferenceException(nameof(serverId)); if (userId == null) throw new NullReferenceException(nameof(userId)); - 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 string or Role.", nameof(roles)); - }); - + var newRoles = CollectionHelper.FlattenRoles(roles); return _api.EditMember(serverId, userId, mute: mute, deaf: deaf, roles: newRoles); } //Messages /// Sends a message to the provided channel, optionally mentioning certain users. /// While not required, it is recommended to include a mention reference in the text (see User.Mention). - public Task SendMessage(Channel channel, string text, params string[] mentionedUserIds) - => SendMessage(channel, text, mentionedUserIds, false); + public Task SendMessage(Channel channel, string text, IEnumerable mentionedUsers = null) + => SendMessage(channel, text, mentionedUsers, false); /// Sends a message to the provided channel, optionally mentioning certain users. /// While not required, it is recommended to include a mention reference in the text (see User.Mention). - public Task SendMessage(string channelId, string text, params string[] mentionedUserIds) - => SendMessage(_channels[channelId], text, mentionedUserIds, false); + public Task SendMessage(string channelId, string text, IEnumerable mentionedUsers = null) + => SendMessage(_channels[channelId], text, mentionedUsers, false); /// Sends a message to the provided channel, optionally mentioning certain users. /// While not required, it is recommended to include a mention reference in the text (see User.Mention). - public Task SendTTSMessage(Channel channel, string text, params string[] mentionedUserIds) - => SendMessage(channel, text, mentionedUserIds, true); + public Task SendTTSMessage(Channel channel, string text, IEnumerable mentionedUsers = null) + => SendMessage(channel, text, mentionedUsers, true); /// Sends a message to the provided channel, optionally mentioning certain users. /// While not required, it is recommended to include a mention reference in the text (see User.Mention). - public Task SendTTSMessage(string channelId, string text, params string[] mentionedUserIds) - => SendMessage(_channels[channelId], text, mentionedUserIds, true); + public Task SendTTSMessage(string channelId, string text, IEnumerable mentionedUsers = null) + => SendMessage(_channels[channelId], text, mentionedUsers, true); /// Sends a message to the provided channel, optionally mentioning certain users. /// While not required, it is recommended to include a mention reference in the text (see User.Mention). - private async Task SendMessage(Channel channel, string text, string[] mentionedUserIds, bool isTextToSpeech) + private async Task SendMessage(Channel channel, string text, IEnumerable mentionedUsers = null, bool isTextToSpeech = false) { CheckReady(); if (channel == null) throw new ArgumentNullException(nameof(channel)); if (text == null) throw new ArgumentNullException(nameof(text)); - mentionedUserIds = mentionedUserIds ?? new string[0]; + var mentionedUserIds = CollectionHelper.FlattenUsers(mentionedUsers); int blockCount = (int)Math.Ceiling(text.Length / (double)MaxMessageSize); Message[] result = new Message[blockCount]; @@ -381,28 +363,28 @@ namespace Discord return _api.SendFile(channelId, filePath); } - + /// 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, params string[] mentions) - => EditMessage(message?.ChannelId, message?.Id, text, mentions); + public Task EditMessage(Message message, string text = null, IEnumerable mentionedUsers = null) + => EditMessage(message?.ChannelId, message?.Id, text, mentionedUsers); /// 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, params string[] mentions) - => EditMessage(channel?.Id, messageId, text, mentions); + public Task EditMessage(Channel channel, string messageId, string text = null, IEnumerable mentionedUsers = null) + => EditMessage(channel?.Id, messageId, text, mentionedUsers); /// 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, params string[] mentions) + public async Task EditMessage(string channelId, string messageId, string text = null, IEnumerable mentionedUsers = null) { CheckReady(); if (channelId == null) throw new ArgumentNullException(nameof(channelId)); if (messageId == null) throw new ArgumentNullException(nameof(messageId)); - mentions = mentions ?? new string[0]; + var mentionedUserIds = CollectionHelper.FlattenUsers(mentionedUsers); if (text != null && text.Length > MaxMessageSize) text = text.Substring(0, MaxMessageSize); - var model = await _api.EditMessage(messageId, channelId, text, mentions).ConfigureAwait(false); + var model = await _api.EditMessage(messageId, channelId, text, mentionedUserIds).ConfigureAwait(false); var msg = _messages[messageId]; if (msg != null) msg.Update(model); diff --git a/src/Discord.Net/Helpers/CollectionHelper.cs b/src/Discord.Net/Helpers/CollectionHelper.cs new file mode 100644 index 000000000..127ea1da2 --- /dev/null +++ b/src/Discord.Net/Helpers/CollectionHelper.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Discord +{ + internal static class CollectionHelper + { + public static IEnumerable FlattenChannels(IEnumerable channels) + { + if (channels == null) + return new string[0]; + + return channels.Select(x => + { + if (x is string) + return x as string; + else if (x is Channel) + return (x as Channel).Id; + else + throw new ArgumentException("Collection may only contain string or Channel.", nameof(channels)); + }); + } + public static IEnumerable FlattenUsers(IEnumerable users) + { + if (users == null) + return new string[0]; + + return users.Select(x => + { + if (x is string) + return x as string; + else if (x is User) + return (x as User).Id; + else + throw new ArgumentException("Collection may only contain string or User.", nameof(users)); + }); + } + public static IEnumerable FlattenRoles(IEnumerable roles) + { + if (roles == null) + return new string[0]; + + return 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("Collection may only contain string or Role.", nameof(roles)); + }); + } + } +}