@@ -1,5 +1,5 @@ | |||
steps: | |||
- script: dotnet restore -v minimal Discord.Net.sln | |||
- script: dotnet restore --no-cache Discord.Net.sln | |||
displayName: Restore packages | |||
- script: dotnet build "Discord.Net.sln" --no-restore -v minimal -c $(buildConfiguration) /p:BuildNumber=$(buildNumber) /p:IsTagBuild=$(buildTag) | |||
@@ -135,7 +135,8 @@ namespace Discord.Commands | |||
if (builder.Name == null) | |||
builder.Name = typeInfo.Name; | |||
var validCommands = typeInfo.DeclaredMethods.Where(IsValidCommandDefinition); | |||
// Get all methods (including from inherited members), that are valid commands | |||
var validCommands = typeInfo.GetMethods().Where(IsValidCommandDefinition); | |||
foreach (var method in validCommands) | |||
{ | |||
@@ -59,11 +59,15 @@ namespace Discord | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich" /> <see cref="Embed" /> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Sends a file to this message channel with an optional caption. | |||
/// </summary> | |||
@@ -88,11 +92,15 @@ namespace Discord | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Gets a message from this message channel. | |||
@@ -683,6 +683,9 @@ namespace Discord | |||
/// <summary> | |||
/// Downloads all users for this guild if the current list is incomplete. | |||
/// </summary> | |||
/// <remarks> | |||
/// This method downloads all users found within this guild throught the Gateway and caches them. | |||
/// </remarks> | |||
/// <returns> | |||
/// A task that represents the asynchronous download operation. | |||
/// </returns> | |||
@@ -707,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. | |||
@@ -8,17 +8,27 @@ namespace Discord | |||
[Flags] | |||
public enum AllowedMentionTypes | |||
{ | |||
/// <summary> | |||
/// No flag is set. | |||
/// </summary> | |||
/// <remarks> | |||
/// This flag is not used to control mentions. | |||
/// <note type="warning"> | |||
/// It will always be present and does not mean mentions will not be allowed. | |||
/// </note> | |||
/// </remarks> | |||
None = 0, | |||
/// <summary> | |||
/// Controls role mentions. | |||
/// </summary> | |||
Roles, | |||
Roles = 1, | |||
/// <summary> | |||
/// Controls user mentions. | |||
/// </summary> | |||
Users, | |||
Users = 2, | |||
/// <summary> | |||
/// Controls <code>@everyone</code> and <code>@here</code> mentions. | |||
/// </summary> | |||
Everyone, | |||
Everyone = 4, | |||
} | |||
} |
@@ -39,7 +39,7 @@ namespace Discord | |||
/// flag of the <see cref="AllowedTypes"/> property. If the flag is set, the value of this property | |||
/// must be <c>null</c> or empty. | |||
/// </summary> | |||
public List<ulong> RoleIds { get; set; } | |||
public List<ulong> RoleIds { get; set; } = new List<ulong>(); | |||
/// <summary> | |||
/// Gets or sets the list of all user ids that will be mentioned. | |||
@@ -47,7 +47,7 @@ namespace Discord | |||
/// flag of the <see cref="AllowedTypes"/> property. If the flag is set, the value of this property | |||
/// must be <c>null</c> or empty. | |||
/// </summary> | |||
public List<ulong> UserIds { get; set; } | |||
public List<ulong> UserIds { get; set; } = new List<ulong>(); | |||
/// <summary> | |||
/// Initializes a new instance of the <see cref="AllowedMentions"/> class. | |||
@@ -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. | |||
@@ -73,7 +73,7 @@ namespace Discord | |||
/// </summary> | |||
/// <example> | |||
/// <para>The following example checks if the current user has the ability to send a message with attachment in | |||
/// this channel; if so, uploads a file via <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool)"/>.</para> | |||
/// this channel; if so, uploads a file via <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool, AllowedMentions)"/>.</para> | |||
/// <code language="cs"> | |||
/// if (currentUser?.GetPermissions(targetChannel)?.AttachFiles) | |||
/// await targetChannel.SendFileAsync("fortnite.png"); | |||
@@ -0,0 +1,41 @@ | |||
using System; | |||
namespace Discord | |||
{ | |||
[Flags] | |||
public enum GatewayIntents | |||
{ | |||
/// <summary> This intent includes no events </summary> | |||
None = 0, | |||
/// <summary> This intent includes GUILD_CREATE, GUILD_UPDATE, GUILD_DELETE, GUILD_ROLE_CREATE, GUILD_ROLE_UPDATE, GUILD_ROLE_DELETE, CHANNEL_CREATE, CHANNEL_UPDATE, CHANNEL_DELETE, CHANNEL_PINS_UPDATE </summary> | |||
Guilds = 1 << 0, | |||
/// <summary> This intent includes GUILD_MEMBER_ADD, GUILD_MEMBER_UPDATE, GUILD_MEMBER_REMOVE </summary> | |||
GuildMembers = 1 << 1, | |||
/// <summary> This intent includes GUILD_BAN_ADD, GUILD_BAN_REMOVE </summary> | |||
GuildBans = 1 << 2, | |||
/// <summary> This intent includes GUILD_EMOJIS_UPDATE </summary> | |||
GuildEmojis = 1 << 3, | |||
/// <summary> This intent includes GUILD_INTEGRATIONS_UPDATE </summary> | |||
GuildIntegrations = 1 << 4, | |||
/// <summary> This intent includes WEBHOOKS_UPDATE </summary> | |||
GuildWebhooks = 1 << 5, | |||
/// <summary> This intent includes INVITE_CREATE, INVITE_DELETE </summary> | |||
GuildInvites = 1 << 6, | |||
/// <summary> This intent includes VOICE_STATE_UPDATE </summary> | |||
GuildVoiceStates = 1 << 7, | |||
/// <summary> This intent includes PRESENCE_UPDATE </summary> | |||
GuildPresences = 1 << 8, | |||
/// <summary> This intent includes MESSAGE_CREATE, MESSAGE_UPDATE, MESSAGE_DELETE, MESSAGE_DELETE_BULK </summary> | |||
GuildMessages = 1 << 9, | |||
/// <summary> This intent includes MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE, MESSAGE_REACTION_REMOVE_ALL, MESSAGE_REACTION_REMOVE_EMOJI </summary> | |||
GuildMessageReactions = 1 << 10, | |||
/// <summary> This intent includes TYPING_START </summary> | |||
GuildMessageTyping = 1 << 11, | |||
/// <summary> This intent includes CHANNEL_CREATE, MESSAGE_CREATE, MESSAGE_UPDATE, MESSAGE_DELETE, CHANNEL_PINS_UPDATE </summary> | |||
DirectMessages = 1 << 12, | |||
/// <summary> This intent includes MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE, MESSAGE_REACTION_REMOVE_ALL, MESSAGE_REACTION_REMOVE_EMOJI </summary> | |||
DirectMessageReactions = 1 << 13, | |||
/// <summary> This intent includes TYPING_START </summary> | |||
DirectMessageTyping = 1 << 14, | |||
} | |||
} |
@@ -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; } | |||
} | |||
} |
@@ -19,6 +19,7 @@ namespace Discord.API.Rest | |||
public Optional<string> Nonce { get; set; } | |||
public Optional<bool> IsTTS { get; set; } | |||
public Optional<Embed> Embed { get; set; } | |||
public Optional<AllowedMentions> AllowedMentions { get; set; } | |||
public bool IsSpoiler { get; set; } = false; | |||
public UploadFileParams(Stream file) | |||
@@ -43,6 +44,8 @@ namespace Discord.API.Rest | |||
payload["nonce"] = Nonce.Value; | |||
if (Embed.IsSpecified) | |||
payload["embed"] = Embed.Value; | |||
if (AllowedMentions.IsSpecified) | |||
payload["allowed_mentions"] = AllowedMentions.Value; | |||
if (IsSpoiler) | |||
payload["hasSpoiler"] = IsSpoiler.ToString(); | |||
@@ -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) | |||
{ | |||
@@ -109,12 +109,19 @@ namespace Discord.Rest | |||
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client, | |||
ulong? fromMessageId, Direction dir, int limit, RequestOptions options) | |||
{ | |||
if (dir == Direction.Around) | |||
throw new NotImplementedException(); //TODO: Impl | |||
var guildId = (channel as IGuildChannel)?.GuildId; | |||
var guild = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null; | |||
if (dir == Direction.Around && limit > DiscordConfig.MaxMessagesPerBatch) | |||
{ | |||
int around = limit / 2; | |||
if (fromMessageId.HasValue) | |||
return GetMessagesAsync(channel, client, fromMessageId.Value + 1, Direction.Before, around + 1, options) //Need to include the message itself | |||
.Concat(GetMessagesAsync(channel, client, fromMessageId, Direction.After, around, options)); | |||
else //Shouldn't happen since there's no public overload for ulong? and Direction | |||
return GetMessagesAsync(channel, client, null, Direction.Before, around + 1, options); | |||
} | |||
return new PagedAsyncEnumerable<RestMessage>( | |||
DiscordConfig.MaxMessagesPerBatch, | |||
async (info, ct) => | |||
@@ -218,18 +225,37 @@ namespace Discord.Rest | |||
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | |||
string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
string filePath, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler) | |||
{ | |||
string filename = Path.GetFileName(filePath); | |||
using (var file = File.OpenRead(filePath)) | |||
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
return await SendFileAsync(channel, client, file, filename, text, isTTS, embed, allowedMentions, options, isSpoiler).ConfigureAwait(false); | |||
} | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public static async Task<RestUserMessage> SendFileAsync(IMessageChannel channel, BaseDiscordClient client, | |||
Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
Stream stream, string filename, string text, bool isTTS, Embed embed, AllowedMentions allowedMentions, RequestOptions options, bool isSpoiler) | |||
{ | |||
var args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed != null ? embed.ToModel() : Optional<API.Embed>.Unspecified, IsSpoiler = isSpoiler }; | |||
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 args = new UploadFileParams(stream) { Filename = filename, Content = text, IsTTS = isTTS, Embed = embed?.ToModel() ?? Optional<API.Embed>.Unspecified, AllowedMentions = allowedMentions?.ToModel() ?? Optional<API.AllowedMentions>.Unspecified, IsSpoiler = isSpoiler }; | |||
var model = await client.ApiClient.UploadFileAsync(channel.Id, args, options).ConfigureAwait(false); | |||
return RestUserMessage.Create(client, channel, client.CurrentUser, model); | |||
} | |||
@@ -34,7 +34,7 @@ namespace Discord.Rest | |||
/// </summary> | |||
/// <remarks> | |||
/// This method follows the same behavior as described in | |||
/// <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool)"/>. Please visit | |||
/// <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool, AllowedMentions)"/>. Please visit | |||
/// its documentation for more details on this method. | |||
/// </remarks> | |||
/// <param name="filePath">The file path of the file.</param> | |||
@@ -42,16 +42,21 @@ namespace Discord.Rest | |||
/// <param name="isTTS">Whether the message should be read aloud by Discord or not.</param> | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich" /> <see cref="Embed" /> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Sends a file to this message channel with an optional caption. | |||
/// </summary> | |||
/// <remarks> | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(Stream, string, string, bool, Embed, RequestOptions, bool)"/>. | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(Stream, string, string, bool, Embed, RequestOptions, bool, AllowedMentions)"/>. | |||
/// Please visit its documentation for more details on this method. | |||
/// </remarks> | |||
/// <param name="stream">The <see cref="Stream" /> of the file to be sent.</param> | |||
@@ -60,11 +65,16 @@ namespace Discord.Rest | |||
/// <param name="isTTS">Whether the message should be read aloud by Discord or not.</param> | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
new Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
new Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Gets a message from this message channel. | |||
@@ -121,12 +121,12 @@ namespace Discord.Rest | |||
/// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception> | |||
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) | |||
@@ -200,11 +200,11 @@ namespace Discord.Rest | |||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -123,12 +123,12 @@ namespace Discord.Rest | |||
/// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception> | |||
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task TriggerTypingAsync(RequestOptions options = null) | |||
@@ -178,11 +178,11 @@ namespace Discord.Rest | |||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -129,13 +129,13 @@ namespace Discord.Rest | |||
/// <exception cref="NotSupportedException"><paramref name="filePath" /> is in an invalid format.</exception> | |||
/// <exception cref="IOException">An I/O error occurred while opening the file.</exception> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) | |||
@@ -266,12 +266,12 @@ namespace Discord.Rest | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -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); | |||
} | |||
@@ -1,4 +1,4 @@ | |||
#pragma warning disable CS1591 | |||
#pragma warning disable CS1591 | |||
using Newtonsoft.Json; | |||
using System.Collections.Generic; | |||
@@ -17,5 +17,7 @@ namespace Discord.API.Gateway | |||
public Optional<int[]> ShardingParams { get; set; } | |||
[JsonProperty("guild_subscriptions")] | |||
public Optional<bool> GuildSubscriptions { get; set; } | |||
[JsonProperty("intents")] | |||
public Optional<int> Intents { get; set; } | |||
} | |||
} |
@@ -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); | |||
@@ -209,7 +209,7 @@ namespace Discord.API | |||
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false); | |||
} | |||
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, RequestOptions options = null) | |||
public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, bool guildSubscriptions = true, GatewayIntents? gatewayIntents = null, RequestOptions options = null) | |||
{ | |||
options = RequestOptions.CreateOrClone(options); | |||
var props = new Dictionary<string, string> | |||
@@ -220,12 +220,16 @@ namespace Discord.API | |||
{ | |||
Token = AuthToken, | |||
Properties = props, | |||
LargeThreshold = largeThreshold, | |||
GuildSubscriptions = guildSubscriptions | |||
LargeThreshold = largeThreshold | |||
}; | |||
if (totalShards > 1) | |||
msg.ShardingParams = new int[] { shardID, totalShards }; | |||
if (gatewayIntents.HasValue) | |||
msg.Intents = (int)gatewayIntents.Value; | |||
else | |||
msg.GuildSubscriptions = guildSubscriptions; | |||
await SendGatewayAsync(GatewayOpCode.Identify, msg, options: options).ConfigureAwait(false); | |||
} | |||
public async Task SendResumeAsync(string sessionId, int lastSeq, RequestOptions options = null) | |||
@@ -44,6 +44,7 @@ namespace Discord.WebSocket | |||
private RestApplication _applicationInfo; | |||
private bool _isDisposed; | |||
private bool _guildSubscriptions; | |||
private GatewayIntents? _gatewayIntents; | |||
/// <summary> | |||
/// Provides access to a REST-only client with a shared state from this client. | |||
@@ -137,6 +138,7 @@ namespace Discord.WebSocket | |||
Rest = new DiscordSocketRestClient(config, ApiClient); | |||
_heartbeatTimes = new ConcurrentQueue<long>(); | |||
_guildSubscriptions = config.GuildSubscriptions; | |||
_gatewayIntents = config.GatewayIntents; | |||
_stateLock = new SemaphoreSlim(1, 1); | |||
_gatewayLogger = LogManager.CreateLogger(ShardId == 0 && TotalShards == 1 ? "Gateway" : $"Shard #{ShardId}"); | |||
@@ -242,7 +244,7 @@ namespace Discord.WebSocket | |||
else | |||
{ | |||
await _gatewayLogger.DebugAsync("Identifying").ConfigureAwait(false); | |||
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions).ConfigureAwait(false); | |||
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions, gatewayIntents: _gatewayIntents).ConfigureAwait(false); | |||
} | |||
//Wait for READY | |||
@@ -517,7 +519,7 @@ namespace Discord.WebSocket | |||
_sessionId = null; | |||
_lastSeq = 0; | |||
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards).ConfigureAwait(false); | |||
await ApiClient.SendIdentifyAsync(shardID: ShardId, totalShards: TotalShards, guildSubscriptions: _guildSubscriptions, gatewayIntents: _gatewayIntents).ConfigureAwait(false); | |||
} | |||
break; | |||
case GatewayOpCode.Reconnect: | |||
@@ -1394,6 +1396,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); | |||
@@ -121,6 +121,7 @@ namespace Discord.WebSocket | |||
/// <summary> | |||
/// Gets or sets enabling dispatching of guild subscription events e.g. presence and typing events. | |||
/// This is not used if <see cref="GatewayIntents"/> are provided. | |||
/// </summary> | |||
public bool GuildSubscriptions { get; set; } = true; | |||
@@ -149,6 +150,15 @@ namespace Discord.WebSocket | |||
} | |||
} | |||
private int _maxWaitForGuildAvailable = 10000; | |||
/// Gets or sets gateway intents to limit what events are sent from Discord. Allows for more granular control than the <see cref="GuildSubscriptions"/> property. | |||
/// </summary> | |||
/// <remarks> | |||
/// For more information, please see | |||
/// <see href="https://discord.com/developers/docs/topics/gateway#gateway-intents">GatewayIntents</see> | |||
/// on the official Discord API documentation. | |||
/// </remarks> | |||
public GatewayIntents? GatewayIntents { get; set; } | |||
/// <summary> | |||
/// Initializes a default configuration. | |||
@@ -42,7 +42,7 @@ namespace Discord.WebSocket | |||
/// Sends a file to this message channel with an optional caption. | |||
/// </summary> | |||
/// <remarks> | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool)"/>. | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(string, string, bool, Embed, RequestOptions, bool, AllowedMentions)"/>. | |||
/// Please visit its documentation for more details on this method. | |||
/// </remarks> | |||
/// <param name="filePath">The file path of the file.</param> | |||
@@ -51,16 +51,20 @@ namespace Discord.WebSocket | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich" /> <see cref="Embed" /> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
new Task<RestUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Sends a file to this message channel with an optional caption. | |||
/// </summary> | |||
/// <remarks> | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(Stream, string, string, bool, Embed, RequestOptions, bool)"/>. | |||
/// This method follows the same behavior as described in <see cref="IMessageChannel.SendFileAsync(Stream, string, string, bool, Embed, RequestOptions, bool, AllowedMentions)"/>. | |||
/// Please visit its documentation for more details on this method. | |||
/// </remarks> | |||
/// <param name="stream">The <see cref="Stream" /> of the file to be sent.</param> | |||
@@ -70,11 +74,15 @@ namespace Discord.WebSocket | |||
/// <param name="embed">The <see cref="Discord.EmbedType.Rich"/> <see cref="Embed"/> to be sent.</param> | |||
/// <param name="options">The options to be used when sending the request.</param> | |||
/// <param name="isSpoiler">Whether the message attachment should be hidden as a spoiler.</param> | |||
/// <param name="allowedMentions"> | |||
/// Specifies if notifications are sent for mentioned users and roles in the message <paramref name="text"/>. | |||
/// If <c>null</c>, all mentioned roles and users will be notified. | |||
/// </param> | |||
/// <returns> | |||
/// A task that represents an asynchronous send operation for delivering the message. The task result | |||
/// contains the sent message. | |||
/// </returns> | |||
new Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false); | |||
new Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null); | |||
/// <summary> | |||
/// Gets a cached message from this channel. | |||
@@ -11,23 +11,11 @@ namespace Discord.WebSocket | |||
public static IAsyncEnumerable<IReadOnlyCollection<IMessage>> GetMessagesAsync(ISocketMessageChannel channel, DiscordSocketClient discord, MessageCache messages, | |||
ulong? fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options) | |||
{ | |||
if (dir == Direction.Around) | |||
throw new NotImplementedException(); //TODO: Impl | |||
IReadOnlyCollection<SocketMessage> cachedMessages = null; | |||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> result = null; | |||
if (dir == Direction.After && fromMessageId == null) | |||
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | |||
if (dir == Direction.Before || mode == CacheMode.CacheOnly) | |||
{ | |||
if (messages != null) //Cache enabled | |||
cachedMessages = messages.GetMany(fromMessageId, dir, limit); | |||
else | |||
cachedMessages = ImmutableArray.Create<SocketMessage>(); | |||
result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>(); | |||
} | |||
var cachedMessages = GetCachedMessages(channel, discord, messages, fromMessageId, dir, limit); | |||
var result = ImmutableArray.Create(cachedMessages).ToAsyncEnumerable<IReadOnlyCollection<IMessage>>(); | |||
if (dir == Direction.Before) | |||
{ | |||
@@ -38,18 +26,35 @@ namespace Discord.WebSocket | |||
//Download remaining messages | |||
ulong? minId = cachedMessages.Count > 0 ? cachedMessages.Min(x => x.Id) : fromMessageId; | |||
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, minId, dir, limit, options); | |||
return result.Concat(downloadedMessages); | |||
if (cachedMessages.Count != 0) | |||
return result.Concat(downloadedMessages); | |||
else | |||
return downloadedMessages; | |||
} | |||
else | |||
else if (dir == Direction.After) | |||
{ | |||
limit -= cachedMessages.Count; | |||
if (mode == CacheMode.CacheOnly || limit <= 0) | |||
return result; | |||
//Download remaining messages | |||
ulong maxId = cachedMessages.Count > 0 ? cachedMessages.Max(x => x.Id) : fromMessageId.Value; | |||
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, maxId, dir, limit, options); | |||
if (cachedMessages.Count != 0) | |||
return result.Concat(downloadedMessages); | |||
else | |||
return downloadedMessages; | |||
} | |||
else //Direction.Around | |||
{ | |||
if (mode == CacheMode.CacheOnly) | |||
if (mode == CacheMode.CacheOnly || limit <= cachedMessages.Count) | |||
return result; | |||
//Dont use cache in this case | |||
//Cache isn't useful here since Discord will send them anyways | |||
return ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit, options); | |||
} | |||
} | |||
public static IReadOnlyCollection<SocketMessage> GetCachedMessages(SocketChannel channel, DiscordSocketClient discord, MessageCache messages, | |||
public static IReadOnlyCollection<SocketMessage> GetCachedMessages(ISocketMessageChannel channel, DiscordSocketClient discord, MessageCache messages, | |||
ulong? fromMessageId, Direction dir, int limit) | |||
{ | |||
if (messages != null) //Cache enabled | |||
@@ -139,12 +139,12 @@ namespace Discord.WebSocket | |||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, allowedMentions, options); | |||
/// <inheritdoc /> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) | |||
=> ChannelHelper.DeleteMessageAsync(this, messageId, Discord, options); | |||
@@ -229,11 +229,11 @@ namespace Discord.WebSocket | |||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -167,11 +167,11 @@ namespace Discord.WebSocket | |||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, allowedMentions, options); | |||
/// <inheritdoc /> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) | |||
@@ -293,11 +293,11 @@ namespace Discord.WebSocket | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -165,13 +165,13 @@ namespace Discord.WebSocket | |||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, embed, allowedMentions, options); | |||
/// <inheritdoc /> | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception> | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, options, isSpoiler); | |||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, embed, allowedMentions, options, isSpoiler); | |||
/// <inheritdoc /> | |||
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null) | |||
@@ -302,11 +302,11 @@ namespace Discord.WebSocket | |||
=> await GetPinnedMessagesAsync(options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler).ConfigureAwait(false); | |||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, Embed embed, RequestOptions options, bool isSpoiler, AllowedMentions allowedMentions) | |||
=> await SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, Embed embed, RequestOptions options, AllowedMentions allowedMentions) | |||
=> await SendMessageAsync(text, isTTS, embed, options, allowedMentions).ConfigureAwait(false); | |||
@@ -821,6 +821,25 @@ namespace Discord.WebSocket | |||
} | |||
} | |||
/// <summary> | |||
/// Gets a collection of all users in this guild. | |||
/// </summary> | |||
/// <remarks> | |||
/// <para>This method retrieves all users found within this guild throught REST.</para> | |||
/// <para>Users returned by this method are not cached.</para> | |||
/// </remarks> | |||
/// <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 found within this guild. | |||
/// </returns> | |||
public IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> GetUsersAsync(RequestOptions options = null) | |||
{ | |||
if (HasAllMembers) | |||
return ImmutableArray.Create(Users).ToAsyncEnumerable<IReadOnlyCollection<IGuildUser>>(); | |||
return GuildHelper.GetUsersAsync(this, Discord, null, null, options); | |||
} | |||
/// <inheritdoc /> | |||
public async Task DownloadUsersAsync() | |||
{ | |||
@@ -831,6 +850,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. | |||
@@ -1185,8 +1221,13 @@ namespace Discord.WebSocket | |||
=> await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false); | |||
/// <inheritdoc /> | |||
Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) | |||
=> Task.FromResult<IReadOnlyCollection<IGuildUser>>(Users); | |||
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) | |||
{ | |||
if (mode == CacheMode.AllowDownload && !HasAllMembers) | |||
return (await GetUsersAsync(options).FlattenAsync().ConfigureAwait(false)).ToImmutableArray(); | |||
else | |||
return Users; | |||
} | |||
/// <inheritdoc /> | |||
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options) | |||
@@ -1200,6 +1241,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, | |||
@@ -56,11 +56,23 @@ namespace Discord.WebSocket | |||
cachedMessageIds = _orderedMessages; | |||
else if (dir == Direction.Before) | |||
cachedMessageIds = _orderedMessages.Where(x => x < fromMessageId.Value); | |||
else | |||
else if (dir == Direction.After) | |||
cachedMessageIds = _orderedMessages.Where(x => x > fromMessageId.Value); | |||
else //Direction.Around | |||
{ | |||
if (!_messages.TryGetValue(fromMessageId.Value, out SocketMessage msg)) | |||
return ImmutableArray<SocketMessage>.Empty; | |||
int around = limit / 2; | |||
var before = GetMany(fromMessageId, Direction.Before, around); | |||
var after = GetMany(fromMessageId, Direction.After, around).Reverse(); | |||
return after.Concat(new SocketMessage[] { msg }).Concat(before).ToImmutableArray(); | |||
} | |||
if (dir == Direction.Before) | |||
cachedMessageIds = cachedMessageIds.Reverse(); | |||
if (dir == Direction.Around) //Only happens if fromMessageId is null, should only get "around" and itself (+1) | |||
limit = limit / 2 + 1; | |||
return cachedMessageIds | |||
.Select(x => | |||
@@ -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); | |||
} | |||
@@ -44,8 +44,11 @@ namespace Discord.WebSocket | |||
/// <summary> | |||
/// Gets mutual guilds shared with this user. | |||
/// </summary> | |||
/// <remarks> | |||
/// This property will only include guilds in the same <see cref="DiscordSocketClient"/>. | |||
/// </remarks> | |||
public IReadOnlyCollection<SocketGuild> MutualGuilds | |||
=> Discord.Guilds.Where(g => g.Users.Any(u => u.Id == Id)).ToImmutableArray(); | |||
=> Discord.Guilds.Where(g => g.GetUser(Id) != null).ToImmutableArray(); | |||
internal SocketUser(DiscordSocketClient discord, ulong id) | |||
: base(discord, id) | |||
@@ -33,7 +33,7 @@ namespace Discord.Webhook | |||
: this(webhookUrl, new DiscordRestConfig()) { } | |||
// regex pattern to match webhook urls | |||
private static Regex WebhookUrlRegex = new Regex(@"^.*discordapp\.com\/api\/webhooks\/([\d]+)\/([a-z0-9_-]+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | |||
private static Regex WebhookUrlRegex = new Regex(@"^.*(discord|discordapp)\.com\/api\/webhooks\/([\d]+)\/([a-z0-9_-]+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase); | |||
/// <summary> Creates a new Webhook Discord client. </summary> | |||
public DiscordWebhookClient(ulong webhookId, string webhookToken, DiscordRestConfig config) | |||
@@ -132,13 +132,13 @@ namespace Discord.Webhook | |||
if (match != null) | |||
{ | |||
// ensure that the first group is a ulong, set the _webhookId | |||
// 0th group is always the entire match, so start at index 1 | |||
if (!(match.Groups[1].Success && ulong.TryParse(match.Groups[1].Value, NumberStyles.None, CultureInfo.InvariantCulture, out webhookId))) | |||
// 0th group is always the entire match, and 1 is the domain; so start at index 2 | |||
if (!(match.Groups[2].Success && ulong.TryParse(match.Groups[2].Value, NumberStyles.None, CultureInfo.InvariantCulture, out webhookId))) | |||
throw ex("The webhook Id could not be parsed."); | |||
if (!match.Groups[2].Success) | |||
if (!match.Groups[3].Success) | |||
throw ex("The webhook token could not be parsed."); | |||
webhookToken = match.Groups[2].Value; | |||
webhookToken = match.Groups[3].Value; | |||
} | |||
else | |||
throw ex(); | |||
@@ -73,12 +73,12 @@ namespace Discord | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
@@ -81,12 +81,12 @@ namespace Discord | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
@@ -167,12 +167,12 @@ namespace Discord | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(string filePath, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false) | |||
public Task<IUserMessage> SendFileAsync(Stream stream, string filename, string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null) | |||
{ | |||
throw new NotImplementedException(); | |||
} | |||