Пример #1
0
        public async Task SetBg(
            [Summary("Direct link to image. If you leave this blank you must provide an attachment!")]
            string url = null)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                if (Context.Message.Attachments.Count != 1)
                {
                    await ReplyFailureEmbed("If you do not provide an image link you MUST provide 1 Attached image");

                    return;
                }
                url = Context.Message.Attachments.First().Url;
            }
            // Check if URL is valid
            if (Helper.LinkIsNoImage(url))
            {
                await ReplyFailureEmbedExtended("The provided link or attachment is not an image!",
                                                "Make sure the link ends with any of these extensions: `.jpg, .png, .gif, .jpeg`");

                return;
            }
            // Check cooldown
            var cd = _cacheService.Get <DateTime>(CacheId.BgCooldownId(Context.User.Id));

            if (cd.HasValue)
            {
                var secondsRemaining = cd.Some().Subtract(DateTime.UtcNow.TimeOfDay).Second;
                await ReplyFailureEmbed(
                    $"Dont break me >.< Please wait another {secondsRemaining.ToString()} seconds!");

                return;
            }
            // No cooldown so we do the actual stuff.
            try
            {
                // Download and resize image
                string imageTemp = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_BG,
                                                $"{Context.User.Id.ToString()}_temp.png");

                await _hch.DownloadAndSaveFile(new Uri(url), imageTemp).ConfigureAwait(false);

                _imgGen.ResizeAndSaveImage(
                    imageTemp,
                    Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_BG, $"{Context.User.Id.ToString()}.png"),
                    new Size(470, 265));

                // Remove temporary one
                File.Delete(imageTemp);
            }
            catch (Exception e)
            {
                await ReplyFailureEmbed("Failed downloading the picture! Try another link.");

                _log.LogWarning(e, $"Failed to download background image for {Context.User.Id.ToString()}");
                return;
            }
            // Set BG on user
            await _profileRepo.SetUserHasBgBoolean(Context.User.Id, true);

            // Add cooldown
            _cacheService.Set(CacheId.BgCooldownId(Context.User.Id), DateTime.UtcNow.AddSeconds(_SET_BG_COOLDOWN_S), TimeSpan.FromSeconds(_SET_BG_COOLDOWN_S));

            await ReplySuccessEmbed("Successfully updated your profile card background :>");
        }