@@ -710,6 +710,22 @@ namespace Discord | |||
/// be or has been removed from this guild. | |||
/// </returns> | |||
Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null); | |||
/// <summary> | |||
/// Gets a collection of users in this guild that the name or nickname starts with the | |||
/// provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </summary> | |||
/// <remarks> | |||
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>. | |||
/// </remarks> | |||
/// <param name="query">The partial name or nickname to search.</param> | |||
/// <param name="limit">The maximum number of users to be gotten.</param> | |||
/// <param name="mode">The <see cref="CacheMode" /> that determines whether the object should be fetched from cache.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <returns> | |||
/// A task that represents the asynchronous get operation. The task result contains a collection of guild | |||
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </returns> | |||
Task<IReadOnlyCollection<IGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); | |||
/// <summary> | |||
/// Gets the specified number of audit log entries for this guild. | |||
@@ -215,6 +215,15 @@ namespace Discord | |||
/// A task that represents the asynchronous removal operation. | |||
/// </returns> | |||
Task RemoveAllReactionsAsync(RequestOptions options = null); | |||
/// <summary> | |||
/// Removes all reactions with a specific emoji from this message. | |||
/// </summary> | |||
/// <param name="emote">The emoji used to react to this message.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <returns> | |||
/// A task that represents the asynchronous removal operation. | |||
/// </returns> | |||
Task RemoveAllReactionsForEmoteAsync(IEmote emote, RequestOptions options = null); | |||
/// <summary> | |||
/// Gets all users that reacted to a message with a given emote. | |||
@@ -41,16 +41,13 @@ namespace Discord | |||
{ | |||
public override bool Equals(TEntity x, TEntity y) | |||
{ | |||
bool xNull = x == null; | |||
bool yNull = y == null; | |||
if (xNull && yNull) | |||
return true; | |||
if (xNull ^ yNull) | |||
return false; | |||
return x.Id.Equals(y.Id); | |||
return (x, y) switch | |||
{ | |||
(null, null) => true, | |||
(null, _) => false, | |||
(_, null) => false, | |||
var (l, r) => l.Id.Equals(r.Id) | |||
}; | |||
} | |||
public override int GetHashCode(TEntity obj) | |||
@@ -0,0 +1,9 @@ | |||
#pragma warning disable CS1591 | |||
namespace Discord.API.Rest | |||
{ | |||
internal class SearchGuildMembersParams | |||
{ | |||
public string Query { get; set; } | |||
public Optional<int> Limit { get; set; } | |||
} | |||
} |
@@ -660,6 +660,18 @@ namespace Discord.API | |||
await SendAsync("DELETE", () => $"channels/{channelId}/messages/{messageId}/reactions", ids, options: options).ConfigureAwait(false); | |||
} | |||
public async Task RemoveAllReactionsForEmoteAsync(ulong channelId, ulong messageId, string emoji, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(channelId, 0, nameof(channelId)); | |||
Preconditions.NotEqual(messageId, 0, nameof(messageId)); | |||
Preconditions.NotNullOrWhitespace(emoji, nameof(emoji)); | |||
options = RequestOptions.CreateOrClone(options); | |||
var ids = new BucketIds(channelId: channelId); | |||
await SendAsync("DELETE", () => $"channels/{channelId}/messages/{messageId}/reactions/{emoji}", ids, options: options).ConfigureAwait(false); | |||
} | |||
public async Task<IReadOnlyCollection<User>> GetReactionUsersAsync(ulong channelId, ulong messageId, string emoji, GetReactionUsersParams args, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(channelId, 0, nameof(channelId)); | |||
@@ -1136,6 +1148,22 @@ namespace Discord.API | |||
await SendJsonAsync("PATCH", () => $"guilds/{guildId}/members/{userId}", args, ids, options: options).ConfigureAwait(false); | |||
} | |||
} | |||
public async Task<IReadOnlyCollection<GuildMember>> SearchGuildMembersAsync(ulong guildId, SearchGuildMembersParams args, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(guildId, 0, nameof(guildId)); | |||
Preconditions.NotNull(args, nameof(args)); | |||
Preconditions.GreaterThan(args.Limit, 0, nameof(args.Limit)); | |||
Preconditions.AtMost(args.Limit, DiscordConfig.MaxUsersPerBatch, nameof(args.Limit)); | |||
Preconditions.NotNullOrEmpty(args.Query, nameof(args.Query)); | |||
options = RequestOptions.CreateOrClone(options); | |||
int limit = args.Limit.GetValueOrDefault(DiscordConfig.MaxUsersPerBatch); | |||
string query = args.Query; | |||
var ids = new BucketIds(guildId: guildId); | |||
Expression<Func<string>> endpoint = () => $"guilds/{guildId}/members/search?limit={limit}&query={query}"; | |||
return await SendAsync<IReadOnlyCollection<GuildMember>>("GET", endpoint, ids, options: options).ConfigureAwait(false); | |||
} | |||
//Guild Roles | |||
public async Task<IReadOnlyCollection<Role>> GetGuildRolesAsync(ulong guildId, RequestOptions options = null) | |||
@@ -1146,13 +1174,13 @@ namespace Discord.API | |||
var ids = new BucketIds(guildId: guildId); | |||
return await SendAsync<IReadOnlyCollection<Role>>("GET", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false); | |||
} | |||
public async Task<Role> CreateGuildRoleAsync(ulong guildId, RequestOptions options = null) | |||
public async Task<Role> CreateGuildRoleAsync(ulong guildId, Rest.ModifyGuildRoleParams args, RequestOptions options = null) | |||
{ | |||
Preconditions.NotEqual(guildId, 0, nameof(guildId)); | |||
options = RequestOptions.CreateOrClone(options); | |||
var ids = new BucketIds(guildId: guildId); | |||
return await SendAsync<Role>("POST", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false); | |||
return await SendJsonAsync<Role>("POST", () => $"guilds/{guildId}/roles", args, ids, options: options).ConfigureAwait(false); | |||
} | |||
public async Task DeleteGuildRoleAsync(ulong guildId, ulong roleId, RequestOptions options = null) | |||
{ | |||
@@ -264,19 +264,18 @@ namespace Discord.Rest | |||
{ | |||
if (name == null) throw new ArgumentNullException(paramName: nameof(name)); | |||
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false); | |||
var role = RestRole.Create(client, guild, model); | |||
await role.ModifyAsync(x => | |||
var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams | |||
{ | |||
x.Name = name; | |||
x.Permissions = (permissions ?? role.Permissions); | |||
x.Color = (color ?? Color.Default); | |||
x.Hoist = isHoisted; | |||
x.Mentionable = isMentionable; | |||
}, options).ConfigureAwait(false); | |||
Color = color?.RawValue ?? Optional.Create<uint>(), | |||
Hoist = isHoisted, | |||
Mentionable = isMentionable, | |||
Name = name, | |||
Permissions = permissions?.RawValue ?? Optional.Create<ulong>() | |||
}; | |||
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, createGuildRoleParams, options).ConfigureAwait(false); | |||
return role; | |||
return RestRole.Create(client, guild, model); | |||
} | |||
//Users | |||
@@ -387,6 +386,17 @@ namespace Discord.Rest | |||
model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false); | |||
return model.Pruned; | |||
} | |||
public static async Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(IGuild guild, BaseDiscordClient client, | |||
string query, int? limit, RequestOptions options) | |||
{ | |||
var apiArgs = new SearchGuildMembersParams | |||
{ | |||
Query = query, | |||
Limit = limit ?? Optional.Create<int>() | |||
}; | |||
var models = await client.ApiClient.SearchGuildMembersAsync(guild.Id, apiArgs, options).ConfigureAwait(false); | |||
return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray(); | |||
} | |||
// Audit logs | |||
public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client, | |||
@@ -634,6 +634,23 @@ namespace Discord.Rest | |||
public Task<int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) | |||
=> GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options); | |||
/// <summary> | |||
/// Gets a collection of users in this guild that the name or nickname starts with the | |||
/// provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </summary> | |||
/// <remarks> | |||
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>. | |||
/// </remarks> | |||
/// <param name="query">The partial name or nickname to search.</param> | |||
/// <param name="limit">The maximum number of users to be gotten.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <returns> | |||
/// A task that represents the asynchronous get operation. The task result contains a collection of guild | |||
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </returns> | |||
public Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, RequestOptions options = null) | |||
=> GuildHelper.SearchUsersAsync(this, Discord, query, limit, options); | |||
//Audit logs | |||
/// <summary> | |||
/// Gets the specified number of audit log entries for this guild. | |||
@@ -884,6 +901,14 @@ namespace Discord.Rest | |||
/// <exception cref="NotSupportedException">Downloading users is not supported for a REST-based guild.</exception> | |||
Task IGuild.DownloadUsersAsync() => | |||
throw new NotSupportedException(); | |||
/// <inheritdoc /> | |||
async Task<IReadOnlyCollection<IGuildUser>> IGuild.SearchUsersAsync(string query, int limit, CacheMode mode, RequestOptions options) | |||
{ | |||
if (mode == CacheMode.AllowDownload) | |||
return await SearchUsersAsync(query, limit, options).ConfigureAwait(false); | |||
else | |||
return ImmutableArray.Create<IGuildUser>(); | |||
} | |||
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options, | |||
ulong? beforeId, ulong? userId, ActionType? actionType) | |||
@@ -78,6 +78,11 @@ namespace Discord.Rest | |||
await client.ApiClient.RemoveAllReactionsAsync(msg.Channel.Id, msg.Id, options).ConfigureAwait(false); | |||
} | |||
public static async Task RemoveAllReactionsForEmoteAsync(IMessage msg, IEmote emote, BaseDiscordClient client, RequestOptions options) | |||
{ | |||
await client.ApiClient.RemoveAllReactionsForEmoteAsync(msg.Channel.Id, msg.Id, emote is Emote e ? $"{e.Name}:{e.Id}" : emote.Name, options).ConfigureAwait(false); | |||
} | |||
public static IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IMessage msg, IEmote emote, | |||
int? limit, BaseDiscordClient client, RequestOptions options) | |||
{ | |||
@@ -182,6 +182,9 @@ namespace Discord.Rest | |||
public Task RemoveAllReactionsAsync(RequestOptions options = null) | |||
=> MessageHelper.RemoveAllReactionsAsync(this, Discord, options); | |||
/// <inheritdoc /> | |||
public Task RemoveAllReactionsForEmoteAsync(IEmote emote, RequestOptions options = null) | |||
=> MessageHelper.RemoveAllReactionsForEmoteAsync(this, emote, Discord, options); | |||
/// <inheritdoc /> | |||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit, RequestOptions options = null) | |||
=> MessageHelper.GetReactionUsersAsync(this, emote, limit, Discord, options); | |||
} | |||
@@ -0,0 +1,16 @@ | |||
using Newtonsoft.Json; | |||
namespace Discord.API.Gateway | |||
{ | |||
internal class RemoveAllReactionsForEmoteEvent | |||
{ | |||
[JsonProperty("channel_id")] | |||
public ulong ChannelId { get; set; } | |||
[JsonProperty("guild_id")] | |||
public Optional<ulong> GuildId { get; set; } | |||
[JsonProperty("message_id")] | |||
public ulong MessageId { get; set; } | |||
[JsonProperty("emoji")] | |||
public Emoji Emoji { get; set; } | |||
} | |||
} |
@@ -234,6 +234,28 @@ namespace Discord.WebSocket | |||
remove { _reactionsClearedEvent.Remove(value); } | |||
} | |||
internal readonly AsyncEvent<Func<Cacheable<IUserMessage, ulong>, ISocketMessageChannel, Task>> _reactionsClearedEvent = new AsyncEvent<Func<Cacheable<IUserMessage, ulong>, ISocketMessageChannel, Task>>(); | |||
/// <summary> | |||
/// Fired when all reactions to a message with a specific emote are removed. | |||
/// </summary> | |||
/// <remarks> | |||
/// <para> | |||
/// This event is fired when all reactions to a message with a specific emote are removed. | |||
/// The event handler must return a <see cref="Task"/> and accept a <see cref="ISocketMessageChannel"/> and | |||
/// a <see cref="IEmote"/> as its parameters. | |||
/// </para> | |||
/// <para> | |||
/// The channel where this message was sent will be passed into the <see cref="ISocketMessageChannel"/> parameter. | |||
/// </para> | |||
/// <para> | |||
/// The emoji that all reactions had and were removed will be passed into the <see cref="IEmote"/> parameter. | |||
/// </para> | |||
/// </remarks> | |||
public event Func<Cacheable<IUserMessage, ulong>, ISocketMessageChannel, IEmote, Task> ReactionsRemovedForEmote | |||
{ | |||
add { _reactionsRemovedForEmoteEvent.Add(value); } | |||
remove { _reactionsRemovedForEmoteEvent.Remove(value); } | |||
} | |||
internal readonly AsyncEvent<Func<Cacheable<IUserMessage, ulong>, ISocketMessageChannel, IEmote, Task>> _reactionsRemovedForEmoteEvent = new AsyncEvent<Func<Cacheable<IUserMessage, ulong>, ISocketMessageChannel, IEmote, Task>>(); | |||
//Roles | |||
/// <summary> Fired when a role is created. </summary> | |||
@@ -313,6 +313,7 @@ namespace Discord.WebSocket | |||
client.ReactionAdded += (cache, channel, reaction) => _reactionAddedEvent.InvokeAsync(cache, channel, reaction); | |||
client.ReactionRemoved += (cache, channel, reaction) => _reactionRemovedEvent.InvokeAsync(cache, channel, reaction); | |||
client.ReactionsCleared += (cache, channel) => _reactionsClearedEvent.InvokeAsync(cache, channel); | |||
client.ReactionsRemovedForEmote += (cache, channel, emote) => _reactionsRemovedForEmoteEvent.InvokeAsync(cache, channel, emote); | |||
client.RoleCreated += (role) => _roleCreatedEvent.InvokeAsync(role); | |||
client.RoleDeleted += (role) => _roleDeletedEvent.InvokeAsync(role); | |||
@@ -1391,6 +1391,34 @@ namespace Discord.WebSocket | |||
} | |||
} | |||
break; | |||
case "MESSAGE_REACTION_REMOVE_EMOJI": | |||
{ | |||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_REACTION_REMOVE_EMOJI)").ConfigureAwait(false); | |||
var data = (payload as JToken).ToObject<API.Gateway.RemoveAllReactionsForEmoteEvent>(_serializer); | |||
if (State.GetChannel(data.ChannelId) is ISocketMessageChannel channel) | |||
{ | |||
var cachedMsg = channel.GetCachedMessage(data.MessageId) as SocketUserMessage; | |||
bool isCached = cachedMsg != null; | |||
var optionalMsg = !isCached | |||
? Optional.Create<SocketUserMessage>() | |||
: Optional.Create(cachedMsg); | |||
var cacheable = new Cacheable<IUserMessage, ulong>(cachedMsg, data.MessageId, isCached, async () => await channel.GetMessageAsync(data.MessageId).ConfigureAwait(false) as IUserMessage); | |||
var emote = data.Emoji.ToIEmote(); | |||
cachedMsg?.RemoveAllReactionsForEmoteAsync(emote); | |||
await TimedInvokeAsync(_reactionsRemovedForEmoteEvent, nameof(ReactionsRemovedForEmote), cacheable, channel, emote).ConfigureAwait(false); | |||
} | |||
else | |||
{ | |||
await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false); | |||
return; | |||
} | |||
} | |||
break; | |||
case "MESSAGE_DELETE_BULK": | |||
{ | |||
await _gatewayLogger.DebugAsync("Received Dispatch (MESSAGE_DELETE_BULK)").ConfigureAwait(false); | |||
@@ -830,6 +830,23 @@ namespace Discord.WebSocket | |||
_downloaderPromise.TrySetResultAsync(true); | |||
} | |||
/// <summary> | |||
/// Gets a collection of users in this guild that the name or nickname starts with the | |||
/// provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </summary> | |||
/// <remarks> | |||
/// The <paramref name="limit"/> can not be higher than <see cref="DiscordConfig.MaxUsersPerBatch"/>. | |||
/// </remarks> | |||
/// <param name="query">The partial name or nickname to search.</param> | |||
/// <param name="limit">The maximum number of users to be gotten.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <returns> | |||
/// A task that represents the asynchronous get operation. The task result contains a collection of guild | |||
/// users that the name or nickname starts with the provided <see cref="string"/> at <paramref name="query"/>. | |||
/// </returns> | |||
public Task<IReadOnlyCollection<RestGuildUser>> SearchUsersAsync(string query, int limit = DiscordConfig.MaxUsersPerBatch, RequestOptions options = null) | |||
=> GuildHelper.SearchUsersAsync(this, Discord, query, limit, options); | |||
//Audit logs | |||
/// <summary> | |||
/// Gets the specified number of audit log entries for this guild. | |||
@@ -1199,6 +1216,14 @@ namespace Discord.WebSocket | |||
/// <inheritdoc /> | |||
Task<IGuildUser> IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) | |||
=> Task.FromResult<IGuildUser>(Owner); | |||
/// <inheritdoc /> | |||
async Task<IReadOnlyCollection<IGuildUser>> IGuild.SearchUsersAsync(string query, int limit, CacheMode mode, RequestOptions options) | |||
{ | |||
if (mode == CacheMode.AllowDownload) | |||
return await SearchUsersAsync(query, limit, options).ConfigureAwait(false); | |||
else | |||
return ImmutableArray.Create<IGuildUser>(); | |||
} | |||
/// <inheritdoc /> | |||
async Task<IReadOnlyCollection<IAuditLogEntry>> IGuild.GetAuditLogsAsync(int limit, CacheMode cacheMode, RequestOptions options, | |||
@@ -140,7 +140,7 @@ namespace Discord.WebSocket | |||
Activity = new MessageActivity() | |||
{ | |||
Type = model.Activity.Value.Type.Value, | |||
PartyId = model.Activity.Value.PartyId.Value | |||
PartyId = model.Activity.Value.PartyId.GetValueOrDefault() | |||
}; | |||
} | |||
@@ -200,6 +200,10 @@ namespace Discord.WebSocket | |||
{ | |||
_reactions.Clear(); | |||
} | |||
internal void RemoveReactionsForEmote(IEmote emote) | |||
{ | |||
_reactions.RemoveAll(x => x.Emote.Equals(emote)); | |||
} | |||
/// <inheritdoc /> | |||
public Task AddReactionAsync(IEmote emote, RequestOptions options = null) | |||
@@ -214,6 +218,9 @@ namespace Discord.WebSocket | |||
public Task RemoveAllReactionsAsync(RequestOptions options = null) | |||
=> MessageHelper.RemoveAllReactionsAsync(this, Discord, options); | |||
/// <inheritdoc /> | |||
public Task RemoveAllReactionsForEmoteAsync(IEmote emote, RequestOptions options = null) | |||
=> MessageHelper.RemoveAllReactionsForEmoteAsync(this, emote, Discord, options); | |||
/// <inheritdoc /> | |||
public IAsyncEnumerable<IReadOnlyCollection<IUser>> GetReactionUsersAsync(IEmote emote, int limit, RequestOptions options = null) | |||
=> MessageHelper.GetReactionUsersAsync(this, emote, limit, Discord, options); | |||
} | |||