Ejemplo n.º 1
0
            public async Task Slowmode(IUserMessage umsg, int msg, int perSec)
            {
                await Slowmode(umsg).ConfigureAwait(false); // disable if exists

                var channel = (ITextChannel)umsg.Channel;

                if (msg < 1 || perSec < 1 || msg > 100 || perSec > 3600)
                {
                    await channel.SendMessageAsync("`Invalid parameters.`");

                    return;
                }
                var toAdd = new Ratelimiter()
                {
                    ChannelId   = channel.Id,
                    MaxMessages = msg,
                    PerSeconds  = perSec,
                };

                if (RatelimitingChannels.TryAdd(channel.Id, toAdd))
                {
                    await channel.SendMessageAsync("`Slow mode initiated.` " +
                                                   $"Users can't send more than {toAdd.MaxMessages} message(s) every {toAdd.PerSeconds} second(s).")
                    .ConfigureAwait(false);
                }
            }
Ejemplo n.º 2
0
        public async void GetImgurLimitInfo_calls_LoadLimits_and_returns_them()
        {
            var rateMock = new Mock <ImgurRatelimiter>(null);

            var limitTuple = new Tuple <int, int, int, int>(200, 150, 100, 50);

            rateMock.Setup(m => m.GetLimiterValues()).Returns(limitTuple);

            var ratelimiter = new Ratelimiter(rateMock.Object);
            var result      = await ratelimiter.GetImgurLimitInfo();

            Assert.Equal(limitTuple.Item1, result.Item1);
            Assert.Equal(limitTuple.Item2, result.Item2);
            Assert.Equal(limitTuple.Item3, result.Item3);
            Assert.Equal(limitTuple.Item4, result.Item4);
            rateMock.Verify(i => i.LoadLimits(), Times.Once);
        }
Ejemplo n.º 3
0
            public async Task Slowmode(int msg, int perSec)
            {
                await Slowmode().ConfigureAwait(false); // disable if exists

                if (msg < 1 || perSec < 1 || msg > 100 || perSec > 3600)
                {
                    await Context.Channel.SendErrorAsync("⚠️ Invalid parameters.");

                    return;
                }
                var toAdd = new Ratelimiter()
                {
                    ChannelId   = Context.Channel.Id,
                    MaxMessages = msg,
                    PerSeconds  = perSec,
                };

                if (RatelimitingChannels.TryAdd(Context.Channel.Id, toAdd))
                {
                    await Context.Channel.SendConfirmAsync("Slow mode initiated",
                                                           $"Users can't send more than `{toAdd.MaxMessages} message(s)` every `{toAdd.PerSeconds} second(s)`.")
                    .ConfigureAwait(false);
                }
            }
            public async Task Slowmode(int msg, int perSec)
            {
                await Slowmode().ConfigureAwait(false); // disable if exists

                if (msg < 1 || perSec < 1 || msg > 100 || perSec > 3600)
                {
                    await ReplyErrorLocalized("invalid_params").ConfigureAwait(false);

                    return;
                }
                var toAdd = new Ratelimiter(Service)
                {
                    ChannelId   = Context.Channel.Id,
                    MaxMessages = msg,
                    PerSeconds  = perSec,
                };

                if (Service.RatelimitingChannels.TryAdd(Context.Channel.Id, toAdd))
                {
                    await Context.Channel.SendConfirmAsync(GetText("slowmode_init"),
                                                           GetText("slowmode_desc", Format.Bold(toAdd.MaxMessages.ToString()), Format.Bold(toAdd.PerSeconds.ToString())))
                    .ConfigureAwait(false);
                }
            }
Ejemplo n.º 5
0
 public DiscordRateLimitPolicy(Ratelimiter ratelimiter, PolicyBuilder <HttpResponseMessage>?policyBuilder = null)
     : base(policyBuilder)
 {
     _ratelimiter = ratelimiter;
 }
Ejemplo n.º 6
0
 /// <summary>
 ///     Queues a Message to be send to the WebSocket Server.
 /// </summary>
 /// <param name="opCode">The OPCode of this Message.</param>
 /// <param name="data">The Data of this Message.</param>
 /// <returns>Task</returns>
 public Task SendAsync(OpCode opCode, object data)
 => Ratelimiter.Enqueue(() => _sendAsync(opCode, data));
        public SettingsControlViewModel(Ratelimiter limitHandler, UpdateChecker updateChecker)
        {
            CheckForUpdate(updateChecker);

            RefreshLimits = new RelayCommand(async o =>
            {
                try
                {
                    var info             = await limitHandler.GetImgurLimitInfo();
                    MaxClientLimit       = info.Item1;
                    MaxUserLimit         = info.Item2;
                    RemainingClientLimit = info.Item3;
                    RemainingUserLimit   = info.Item4;
                }
                catch (InvalidClientIDException)
                {
                    MessageBox.Show("The provided Imgur ID is invalid. All subsequent API calls to the Imgur will fail.\nPlease change the Imgur ID and restart the program.", "ImageDownloader - Invalid Imgur ID");
                    MaxClientLimit       = -1;
                    MaxUserLimit         = -1;
                    RemainingClientLimit = -1;
                    RemainingUserLimit   = -1;
                }
            });

            RestoreDefaults = new RelayCommand(o =>
            {
                Settings.ResetDefaults();
                LoadSettings.Execute(null);
            });

            SaveSettings = new RelayCommand(o =>
            {
                Settings.SetImgurClientID(string.IsNullOrWhiteSpace(ImgurSetting) ? string.Empty : ImgurSetting);

                Settings.SetFallbackHeight(FallbackImageDimensionsForNonsupportedImageTypes_Height);
                Settings.SetFallbackWidth(FallbackImageDimensionsForNonsupportedImageTypes_Width);

                var stringcollection = new StringCollection();
                stringcollection.AddRange(SupportedExtensions.ToLower().Split(','));
                Settings.SetSupportedExtensions(stringcollection);

                Settings.Save();
                LoadSettings.Execute(null);
            });

            LoadSettings = new RelayCommand(o =>
            {
                ImgurSetting = Settings.GetImgurClientID();
                FallbackImageDimensionsForNonsupportedImageTypes_Height = Settings.GetFallbackHeight();
                FallbackImageDimensionsForNonsupportedImageTypes_Width  = Settings.GetFallbackWidth();

                var builder = new StringBuilder();
                foreach (var ext in Settings.GetSupportedExtensions())
                {
                    builder.Append(ext);
                    builder.Append(",");
                }

                SupportedExtensions = builder.ToString().TrimEnd(',');
            });
        }
Ejemplo n.º 8
0
 /// <summary>
 ///     Queues a Message to be send to the WebSocket Server.
 /// </summary>
 /// <param name="opCode">The OPCode of this Message.</param>
 /// <param name="data">The Data of this Message.</param>
 /// <returns>Task</returns>
 public Task SendAsync(OpCode opCode, object data)
 => Ratelimiter.Perform(() => _sendAsync(opCode, data));
Ejemplo n.º 9
0
            public async Task Slowmode(IUserMessage umsg, int msg, int perSec)
            {
                await Slowmode(umsg).ConfigureAwait(false); // disable if exists
                var channel = (ITextChannel)umsg.Channel;

                if (msg < 1 || perSec < 1 || msg > 100 || perSec > 3600)
                {
                    await channel.SendMessageAsync("⚠️ `Invalid parameters.`");
                    return;
                }
                var toAdd = new Ratelimiter()
                {
                    ChannelId = channel.Id,
                    MaxMessages = msg,
                    PerSeconds = perSec,
                };
                if(RatelimitingChannels.TryAdd(channel.Id, toAdd))
                {
                    await channel.SendMessageAsync("✅ **Slow mode initiated: " +
                                                $"Users can't send more than `{toAdd.MaxMessages} message(s)` every `{toAdd.PerSeconds} second(s)`.**")
                                                .ConfigureAwait(false);
                }
            }