From 97d17cfdda22a04173b63247c646acc7cd1122a7 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Tue, 11 Sep 2018 18:21:22 -0400 Subject: [PATCH 1/2] api: add slow mode This adds the following property to ITextChannel - SlowMode (int) The SlowMode field defines the time in seconds users must wait between sending messages; if zero, slow-mode is disabled. Bots are not affected by slow-mode, and as such, no changes need to be made to the REST client's internal ratelimiting. This is strictly a read/write cosmetic property for bots. --- .../Entities/Channels/ITextChannel.cs | 3 +++ .../Entities/Channels/TextChannelProperties.cs | 14 +++++++++++++- src/Discord.Net.Rest/API/Common/Channel.cs | 4 +++- .../API/Rest/ModifyTextChannelParams.cs | 4 +++- src/Discord.Net.Rest/DiscordRestApiClient.cs | 2 ++ .../Entities/Channels/ChannelHelper.cs | 3 ++- .../Entities/Channels/RestTextChannel.cs | 2 ++ .../Entities/Channels/SocketTextChannel.cs | 2 ++ 8 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs index 2aa070b03..e9350601d 100644 --- a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs +++ b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs @@ -13,6 +13,9 @@ namespace Discord /// Gets the current topic for this text channel. string Topic { get; } + /// Gets the current slow-mode delay for this channel. 0 if disabled. + int SlowMode { get; } + /// Bulk deletes multiple messages. Task DeleteMessagesAsync(IEnumerable messages, RequestOptions options = null); /// Bulk deletes multiple messages. diff --git a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs index b7b568133..e3c0557dd 100644 --- a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs +++ b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs @@ -1,4 +1,6 @@ -namespace Discord +using System; + +namespace Discord { /// public class TextChannelProperties : GuildChannelProperties @@ -11,5 +13,15 @@ /// Should this channel be flagged as NSFW? /// public Optional IsNsfw { get; set; } + /// + /// What the slow-mode ratelimit for this channel should be set to; 0 will disable slow-mode. + /// + /// + /// This value must fall within [0, 120] + /// + /// Users with will be exempt from slow-mode. + /// + /// Throws ArgummentOutOfRange if the value does not fall within [0, 120] + public Optional SlowMode { get; set; } } } diff --git a/src/Discord.Net.Rest/API/Common/Channel.cs b/src/Discord.Net.Rest/API/Common/Channel.cs index 97c35a57b..57a5ce9ab 100644 --- a/src/Discord.Net.Rest/API/Common/Channel.cs +++ b/src/Discord.Net.Rest/API/Common/Channel.cs @@ -1,4 +1,4 @@ -#pragma warning disable CS1591 +#pragma warning disable CS1591 using Newtonsoft.Json; using System; @@ -33,6 +33,8 @@ namespace Discord.API public Optional LastPinTimestamp { get; set; } [JsonProperty("nsfw")] public Optional Nsfw { get; set; } + [JsonProperty("rate_limit_per_user")] + public Optional SlowMode { get; set; } //VoiceChannel [JsonProperty("bitrate")] diff --git a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs index 9cabc67c1..7cb1405c9 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs +++ b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs @@ -1,4 +1,4 @@ -#pragma warning disable CS1591 +#pragma warning disable CS1591 using Newtonsoft.Json; namespace Discord.API.Rest @@ -10,5 +10,7 @@ namespace Discord.API.Rest public Optional Topic { get; set; } [JsonProperty("nsfw")] public Optional IsNsfw { get; set; } + [JsonProperty("rate_limit_per_user")] + public Optional SlowMode { get; set; } } } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index 02529310f..f7e86a8f3 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -356,6 +356,8 @@ namespace Discord.API Preconditions.NotNull(args, nameof(args)); Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); + Preconditions.AtLeast(args.SlowMode, 0, nameof(args.SlowMode)); + Preconditions.AtMost(args.SlowMode, 120, nameof(args.SlowMode)); options = RequestOptions.CreateOrClone(options); var ids = new BucketIds(channelId: channelId); diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index 50cb352a7..47f82f612 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -45,7 +45,8 @@ namespace Discord.Rest Position = args.Position, CategoryId = args.CategoryId, Topic = args.Topic, - IsNsfw = args.IsNsfw + IsNsfw = args.IsNsfw, + SlowMode = args.SlowMode, }; return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs index 7f57c96ff..8fe17ba2f 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs @@ -12,6 +12,7 @@ namespace Discord.Rest public class RestTextChannel : RestGuildChannel, IRestMessageChannel, ITextChannel { public string Topic { get; private set; } + public int SlowMode { get; private set; } public ulong? CategoryId { get; private set; } public string Mention => MentionUtils.MentionChannel(Id); @@ -34,6 +35,7 @@ namespace Discord.Rest base.Update(model); CategoryId = model.CategoryId; Topic = model.Topic.Value; + SlowMode = model.SlowMode.Value; _nsfw = model.Nsfw.GetValueOrDefault(); } diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs index e41c2ba94..d6982bf6a 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs @@ -16,6 +16,7 @@ namespace Discord.WebSocket private readonly MessageCache _messages; public string Topic { get; private set; } + public int SlowMode { get; private set; } public ulong? CategoryId { get; private set; } public ICategoryChannel Category => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null; @@ -47,6 +48,7 @@ namespace Discord.WebSocket base.Update(state, model); CategoryId = model.CategoryId; Topic = model.Topic.Value; + SlowMode = model.SlowMode.GetValueOrDefault(); // some guilds haven't been patched to include this yet? _nsfw = model.Nsfw.GetValueOrDefault(); } From 232f525b59e801332ec9dca755366944c79afa1d Mon Sep 17 00:00:00 2001 From: Christopher F Date: Tue, 11 Sep 2018 18:28:31 -0400 Subject: [PATCH 2/2] lint: refactor SlowMode -> SlowModeInterval this was the name change i always wanted --- src/Discord.Net.Core/Entities/Channels/ITextChannel.cs | 2 +- .../Entities/Channels/TextChannelProperties.cs | 2 +- src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs | 2 +- src/Discord.Net.Rest/DiscordRestApiClient.cs | 4 ++-- src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs | 2 +- src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs | 4 ++-- src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs | 2 +- .../Entities/Channels/SocketTextChannel.cs | 4 ++-- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs index e9350601d..89e10e65e 100644 --- a/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs +++ b/src/Discord.Net.Core/Entities/Channels/ITextChannel.cs @@ -14,7 +14,7 @@ namespace Discord string Topic { get; } /// Gets the current slow-mode delay for this channel. 0 if disabled. - int SlowMode { get; } + int SlowModeInterval { get; } /// Bulk deletes multiple messages. Task DeleteMessagesAsync(IEnumerable messages, RequestOptions options = null); diff --git a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs index e3c0557dd..87adccb85 100644 --- a/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs +++ b/src/Discord.Net.Core/Entities/Channels/TextChannelProperties.cs @@ -22,6 +22,6 @@ namespace Discord /// Users with will be exempt from slow-mode. /// /// Throws ArgummentOutOfRange if the value does not fall within [0, 120] - public Optional SlowMode { get; set; } + public Optional SlowModeInterval { get; set; } } } diff --git a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs index 7cb1405c9..94f149fc1 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs +++ b/src/Discord.Net.Rest/API/Rest/ModifyTextChannelParams.cs @@ -11,6 +11,6 @@ namespace Discord.API.Rest [JsonProperty("nsfw")] public Optional IsNsfw { get; set; } [JsonProperty("rate_limit_per_user")] - public Optional SlowMode { get; set; } + public Optional SlowModeInterval { get; set; } } } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index f7e86a8f3..35fa0e989 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -356,8 +356,8 @@ namespace Discord.API Preconditions.NotNull(args, nameof(args)); Preconditions.AtLeast(args.Position, 0, nameof(args.Position)); Preconditions.NotNullOrEmpty(args.Name, nameof(args.Name)); - Preconditions.AtLeast(args.SlowMode, 0, nameof(args.SlowMode)); - Preconditions.AtMost(args.SlowMode, 120, nameof(args.SlowMode)); + Preconditions.AtLeast(args.SlowModeInterval, 0, nameof(args.SlowModeInterval)); + Preconditions.AtMost(args.SlowModeInterval, 120, nameof(args.SlowModeInterval)); options = RequestOptions.CreateOrClone(options); var ids = new BucketIds(channelId: channelId); diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index 47f82f612..74ce7870f 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -46,7 +46,7 @@ namespace Discord.Rest CategoryId = args.CategoryId, Topic = args.Topic, IsNsfw = args.IsNsfw, - SlowMode = args.SlowMode, + SlowModeInterval = args.SlowModeInterval, }; return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs index 8fe17ba2f..12437c969 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs @@ -12,7 +12,7 @@ namespace Discord.Rest public class RestTextChannel : RestGuildChannel, IRestMessageChannel, ITextChannel { public string Topic { get; private set; } - public int SlowMode { get; private set; } + public int SlowModeInterval { get; private set; } public ulong? CategoryId { get; private set; } public string Mention => MentionUtils.MentionChannel(Id); @@ -35,7 +35,7 @@ namespace Discord.Rest base.Update(model); CategoryId = model.CategoryId; Topic = model.Topic.Value; - SlowMode = model.SlowMode.Value; + SlowModeInterval = model.SlowMode.Value; _nsfw = model.Nsfw.GetValueOrDefault(); } diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 3b2935149..bde36922a 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -156,7 +156,7 @@ namespace Discord.Rest { CategoryId = props.CategoryId, Topic = props.Topic, - IsNsfw = props.IsNsfw + IsNsfw = props.IsNsfw, }; var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false); return RestTextChannel.Create(client, guild, model); diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs index d6982bf6a..2e9cd90be 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs @@ -16,7 +16,7 @@ namespace Discord.WebSocket private readonly MessageCache _messages; public string Topic { get; private set; } - public int SlowMode { get; private set; } + public int SlowModeInterval { get; private set; } public ulong? CategoryId { get; private set; } public ICategoryChannel Category => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null; @@ -48,7 +48,7 @@ namespace Discord.WebSocket base.Update(state, model); CategoryId = model.CategoryId; Topic = model.Topic.Value; - SlowMode = model.SlowMode.GetValueOrDefault(); // some guilds haven't been patched to include this yet? + SlowModeInterval = model.SlowMode.GetValueOrDefault(); // some guilds haven't been patched to include this yet? _nsfw = model.Nsfw.GetValueOrDefault(); }