Added support for user objects being passed to Send/EditMessage.
This commit is contained in:
@@ -173,7 +173,7 @@ namespace Discord
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Messages
|
//Messages
|
||||||
public Task<SendMessageResponse> SendMessage(string channelId, string message, string[] mentionedUserIds = null, string nonce = null, bool isTTS = false)
|
public Task<SendMessageResponse> SendMessage(string channelId, string message, IEnumerable<string> mentionedUserIds = null, string nonce = null, bool isTTS = false)
|
||||||
{
|
{
|
||||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
||||||
if (message == null) throw new ArgumentNullException(nameof(message));
|
if (message == null) throw new ArgumentNullException(nameof(message));
|
||||||
@@ -195,12 +195,12 @@ namespace Discord
|
|||||||
|
|
||||||
return _rest.Delete(Endpoints.ChannelMessage(channelId, messageId));
|
return _rest.Delete(Endpoints.ChannelMessage(channelId, messageId));
|
||||||
}
|
}
|
||||||
public Task<EditMessageResponse> EditMessage(string messageId, string channelId, string message = null, string[] mentions = null)
|
public Task<EditMessageResponse> EditMessage(string messageId, string channelId, string message = null, IEnumerable<string> mentionedUserIds = null)
|
||||||
{
|
{
|
||||||
if (messageId == null) throw new ArgumentNullException(nameof(messageId));
|
if (messageId == null) throw new ArgumentNullException(nameof(messageId));
|
||||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
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<EditMessageResponse>(Endpoints.ChannelMessage(channelId, messageId), request);
|
return _rest.Patch<EditMessageResponse>(Endpoints.ChannelMessage(channelId, messageId), request);
|
||||||
}
|
}
|
||||||
public Task AckMessage(string messageId, string channelId)
|
public Task AckMessage(string messageId, string channelId)
|
||||||
|
|||||||
@@ -151,16 +151,7 @@ namespace Discord
|
|||||||
if (channels == null) throw new ArgumentNullException(nameof(channels));
|
if (channels == null) throw new ArgumentNullException(nameof(channels));
|
||||||
if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer.");
|
if (startPos < 0) throw new ArgumentOutOfRangeException(nameof(startPos), "startPos must be a positive integer.");
|
||||||
|
|
||||||
var channelIds = channels.Select(x =>
|
var channelIds = CollectionHelper.FlattenChannels(channels);
|
||||||
{
|
|
||||||
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));
|
|
||||||
});
|
|
||||||
|
|
||||||
return _api.ReorderChannels(serverId, channelIds, startPos);
|
return _api.ReorderChannels(serverId, channelIds, startPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,44 +271,35 @@ namespace Discord
|
|||||||
if (serverId == null) throw new NullReferenceException(nameof(serverId));
|
if (serverId == null) throw new NullReferenceException(nameof(serverId));
|
||||||
if (userId == null) throw new NullReferenceException(nameof(userId));
|
if (userId == null) throw new NullReferenceException(nameof(userId));
|
||||||
|
|
||||||
IEnumerable<string> newRoles = roles?.Select(x =>
|
var newRoles = CollectionHelper.FlattenRoles(roles);
|
||||||
{
|
|
||||||
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));
|
|
||||||
});
|
|
||||||
|
|
||||||
return _api.EditMember(serverId, userId, mute: mute, deaf: deaf, roles: newRoles);
|
return _api.EditMember(serverId, userId, mute: mute, deaf: deaf, roles: newRoles);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Messages
|
//Messages
|
||||||
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
||||||
public Task<Message[]> SendMessage(Channel channel, string text, params string[] mentionedUserIds)
|
public Task<Message[]> SendMessage(Channel channel, string text, IEnumerable<object> mentionedUsers = null)
|
||||||
=> SendMessage(channel, text, mentionedUserIds, false);
|
=> SendMessage(channel, text, mentionedUsers, false);
|
||||||
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
||||||
public Task<Message[]> SendMessage(string channelId, string text, params string[] mentionedUserIds)
|
public Task<Message[]> SendMessage(string channelId, string text, IEnumerable<object> mentionedUsers = null)
|
||||||
=> SendMessage(_channels[channelId], text, mentionedUserIds, false);
|
=> SendMessage(_channels[channelId], text, mentionedUsers, false);
|
||||||
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
||||||
public Task<Message[]> SendTTSMessage(Channel channel, string text, params string[] mentionedUserIds)
|
public Task<Message[]> SendTTSMessage(Channel channel, string text, IEnumerable<object> mentionedUsers = null)
|
||||||
=> SendMessage(channel, text, mentionedUserIds, true);
|
=> SendMessage(channel, text, mentionedUsers, true);
|
||||||
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
||||||
public Task<Message[]> SendTTSMessage(string channelId, string text, params string[] mentionedUserIds)
|
public Task<Message[]> SendTTSMessage(string channelId, string text, IEnumerable<object> mentionedUsers = null)
|
||||||
=> SendMessage(_channels[channelId], text, mentionedUserIds, true);
|
=> SendMessage(_channels[channelId], text, mentionedUsers, true);
|
||||||
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
/// <summary> Sends a message to the provided channel, optionally mentioning certain users. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
|
||||||
private async Task<Message[]> SendMessage(Channel channel, string text, string[] mentionedUserIds, bool isTextToSpeech)
|
private async Task<Message[]> SendMessage(Channel channel, string text, IEnumerable<object> mentionedUsers = null, bool isTextToSpeech = false)
|
||||||
{
|
{
|
||||||
CheckReady();
|
CheckReady();
|
||||||
if (channel == null) throw new ArgumentNullException(nameof(channel));
|
if (channel == null) throw new ArgumentNullException(nameof(channel));
|
||||||
if (text == null) throw new ArgumentNullException(nameof(text));
|
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);
|
int blockCount = (int)Math.Ceiling(text.Length / (double)MaxMessageSize);
|
||||||
Message[] result = new Message[blockCount];
|
Message[] result = new Message[blockCount];
|
||||||
@@ -381,28 +363,28 @@ namespace Discord
|
|||||||
|
|
||||||
return _api.SendFile(channelId, filePath);
|
return _api.SendFile(channelId, filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
||||||
public Task EditMessage(Message message, string text = null, params string[] mentions)
|
public Task EditMessage(Message message, string text = null, IEnumerable<object> mentionedUsers = null)
|
||||||
=> EditMessage(message?.ChannelId, message?.Id, text, mentions);
|
=> EditMessage(message?.ChannelId, message?.Id, text, mentionedUsers);
|
||||||
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
||||||
public Task EditMessage(Channel channel, string messageId, string text = null, params string[] mentions)
|
public Task EditMessage(Channel channel, string messageId, string text = null, IEnumerable<object> mentionedUsers = null)
|
||||||
=> EditMessage(channel?.Id, messageId, text, mentions);
|
=> EditMessage(channel?.Id, messageId, text, mentionedUsers);
|
||||||
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
/// <summary> Edits the provided message, changing only non-null attributes. </summary>
|
||||||
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
/// <remarks> While not required, it is recommended to include a mention reference in the text (see Mention.User). </remarks>
|
||||||
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<object> mentionedUsers = null)
|
||||||
{
|
{
|
||||||
CheckReady();
|
CheckReady();
|
||||||
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
if (channelId == null) throw new ArgumentNullException(nameof(channelId));
|
||||||
if (messageId == null) throw new ArgumentNullException(nameof(messageId));
|
if (messageId == null) throw new ArgumentNullException(nameof(messageId));
|
||||||
mentions = mentions ?? new string[0];
|
var mentionedUserIds = CollectionHelper.FlattenUsers(mentionedUsers);
|
||||||
|
|
||||||
if (text != null && text.Length > MaxMessageSize)
|
if (text != null && text.Length > MaxMessageSize)
|
||||||
text = text.Substring(0, 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];
|
var msg = _messages[messageId];
|
||||||
if (msg != null)
|
if (msg != null)
|
||||||
msg.Update(model);
|
msg.Update(model);
|
||||||
|
|||||||
55
src/Discord.Net/Helpers/CollectionHelper.cs
Normal file
55
src/Discord.Net/Helpers/CollectionHelper.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Discord
|
||||||
|
{
|
||||||
|
internal static class CollectionHelper
|
||||||
|
{
|
||||||
|
public static IEnumerable<string> FlattenChannels(IEnumerable<object> 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<string> FlattenUsers(IEnumerable<object> 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<string> FlattenRoles(IEnumerable<object> 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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user