(ifcbrk) feature: Add ModifyMessageAsync to IMessageChannel (#1830)
This commit is contained in:
@@ -257,6 +257,21 @@ namespace Discord
|
||||
/// </returns>
|
||||
Task DeleteMessageAsync(IMessage message, RequestOptions options = null);
|
||||
|
||||
/// <summary>
|
||||
/// Modifies a message.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method modifies this message with the specified properties. To see an example of this
|
||||
/// method and what properties are available, please refer to <see cref="MessageProperties"/>.
|
||||
/// </remarks>
|
||||
/// <param name="messageId">The snowflake identifier of the message that would be changed.</param>
|
||||
/// <param name="func">A delegate containing the properties to modify the message with.</param>
|
||||
/// <param name="options">The options to be used when sending the request.</param>
|
||||
/// <returns>
|
||||
/// A task that represents the asynchronous modification operation.
|
||||
/// </returns>
|
||||
Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null);
|
||||
|
||||
/// <summary>
|
||||
/// Broadcasts the "user is typing" message to all users in this channel, lasting 10 seconds.
|
||||
/// </summary>
|
||||
|
||||
@@ -286,6 +286,13 @@ namespace Discord.Rest
|
||||
return RestUserMessage.Create(client, channel, client.CurrentUser, model);
|
||||
}
|
||||
|
||||
public static async Task<RestUserMessage> ModifyMessageAsync(IMessageChannel channel, ulong messageId, Action<MessageProperties> func,
|
||||
BaseDiscordClient client, RequestOptions options)
|
||||
{
|
||||
var msgModel = await MessageHelper.ModifyAsync(channel.Id, messageId, client, func, options).ConfigureAwait(false);
|
||||
return RestUserMessage.Create(client, channel, msgModel.Author.IsSpecified ? RestUser.Create(client, msgModel.Author.Value) : client.CurrentUser, msgModel);
|
||||
}
|
||||
|
||||
public static Task DeleteMessageAsync(IMessageChannel channel, ulong messageId, BaseDiscordClient client,
|
||||
RequestOptions options)
|
||||
=> MessageHelper.DeleteAsync(channel.Id, messageId, client, options);
|
||||
|
||||
@@ -135,6 +135,10 @@ namespace Discord.Rest
|
||||
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task TriggerTypingAsync(RequestOptions options = null)
|
||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options);
|
||||
|
||||
@@ -93,6 +93,10 @@ namespace Discord.Rest
|
||||
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
|
||||
public Task<RestUserMessage> SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null)
|
||||
|
||||
@@ -152,6 +152,10 @@ namespace Discord.Rest
|
||||
public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task TriggerTypingAsync(RequestOptions options = null)
|
||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options);
|
||||
|
||||
@@ -71,6 +71,48 @@ namespace Discord.Rest
|
||||
return await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, apiArgs, options).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static async Task<Model> ModifyAsync(ulong channelId, ulong msgId, BaseDiscordClient client, Action<MessageProperties> func,
|
||||
RequestOptions options)
|
||||
{
|
||||
var args = new MessageProperties();
|
||||
func(args);
|
||||
|
||||
if ((args.Content.IsSpecified && string.IsNullOrEmpty(args.Content.Value)) && (args.Embed.IsSpecified && args.Embed.Value == null))
|
||||
Preconditions.NotNullOrEmpty(args.Content.IsSpecified ? args.Content.Value : string.Empty, nameof(args.Content));
|
||||
|
||||
if (args.AllowedMentions.IsSpecified)
|
||||
{
|
||||
AllowedMentions allowedMentions = args.AllowedMentions.Value;
|
||||
Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed.");
|
||||
Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed.");
|
||||
|
||||
// check that user flag and user Id list are exclusive, same with role flag and role Id list
|
||||
if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue)
|
||||
{
|
||||
if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Users) &&
|
||||
allowedMentions.UserIds != null && allowedMentions.UserIds.Count > 0)
|
||||
{
|
||||
throw new ArgumentException("The Users flag is mutually exclusive with the list of User Ids.", nameof(allowedMentions));
|
||||
}
|
||||
|
||||
if (allowedMentions.AllowedTypes.Value.HasFlag(AllowedMentionTypes.Roles) &&
|
||||
allowedMentions.RoleIds != null && allowedMentions.RoleIds.Count > 0)
|
||||
{
|
||||
throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var apiArgs = new API.Rest.ModifyMessageParams
|
||||
{
|
||||
Content = args.Content,
|
||||
Embed = args.Embed.IsSpecified ? args.Embed.Value.ToModel() : Optional.Create<API.Embed>(),
|
||||
Flags = args.Flags.IsSpecified ? args.Flags.Value : Optional.Create<MessageFlags?>(),
|
||||
AllowedMentions = args.AllowedMentions.IsSpecified ? args.AllowedMentions.Value.ToModel() : Optional.Create<API.AllowedMentions>(),
|
||||
};
|
||||
return await client.ApiClient.ModifyMessageAsync(channelId, msgId, apiArgs, options).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static Task DeleteAsync(IMessage msg, BaseDiscordClient client, RequestOptions options)
|
||||
=> DeleteAsync(msg.Channel.Id, msg.Id, client, options);
|
||||
|
||||
|
||||
@@ -152,6 +152,10 @@ namespace Discord.WebSocket
|
||||
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task TriggerTypingAsync(RequestOptions options = null)
|
||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options);
|
||||
|
||||
@@ -180,6 +180,10 @@ namespace Discord.WebSocket
|
||||
public Task DeleteMessageAsync(IMessage message, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessageAsync(this, message.Id, Discord, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task TriggerTypingAsync(RequestOptions options = null)
|
||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options);
|
||||
|
||||
@@ -180,6 +180,10 @@ namespace Discord.WebSocket
|
||||
public Task DeleteMessagesAsync(IEnumerable<ulong> messageIds, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messageIds, options);
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
=> await ChannelHelper.ModifyMessageAsync(this, messageId, func, Discord, options).ConfigureAwait(false);
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null)
|
||||
=> ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options);
|
||||
|
||||
@@ -33,6 +33,11 @@ namespace Discord
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IDisposable EnterTypingState(RequestOptions options = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -31,6 +31,11 @@ namespace Discord
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task DisconnectAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
@@ -77,6 +77,11 @@ namespace Discord
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IUserMessage> ModifyMessageAsync(ulong messageId, Action<MessageProperties> func, RequestOptions options = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IDisposable EnterTypingState(RequestOptions options = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
|
||||
Reference in New Issue
Block a user