private async Task AddBackgroundAsync(MagickImage image, ProfileInfo profileInfo) { if (string.IsNullOrEmpty(profileInfo.BackgroundUrl)) { AddBackground(null, image, profileInfo); return; } try { using var response = await _httpClient.GetAsync(profileInfo.BackgroundUrl); if (!response.IsSuccessStatusCode) { AddBackground(null, image, profileInfo); return; } await using var backgroundStream = await response.Content.ReadAsStreamAsync(); if (!(RiasUtilities.IsPng(backgroundStream) || RiasUtilities.IsJpg(backgroundStream))) { AddBackground(null, image, profileInfo); return; } backgroundStream.Position = 0; using var tempBackgroundImage = new MagickImage(backgroundStream); tempBackgroundImage.Resize(new MagickGeometry { Width = 500, Height = 250, IgnoreAspectRatio = false, FillArea = true }); using var backgroundImageLayer = new MagickImage(MagickColors.Black, 500, 250); backgroundImageLayer.Draw(new DrawableComposite(0, 0, CompositeOperator.Over, tempBackgroundImage)); AddBackground(backgroundImageLayer, image, profileInfo); } catch { AddBackground(null, image, profileInfo); } }
public async Task SetAvatarAsync(string url) { if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); return; } using var response = await _httpClient.GetAsync(uri); if (!response.IsSuccessStatusCode) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); return; } await using var stream = await response.Content.ReadAsStreamAsync(); await using var avatarStream = new MemoryStream(); await stream.CopyToAsync(avatarStream); avatarStream.Position = 0; if (!(RiasUtilities.IsPng(stream) || RiasUtilities.IsJpg(stream))) { await ReplyErrorAsync(Localization.UtilityUrlNotPngJpg); return; } avatarStream.Position = 0; try { await RiasBot.CurrentUser.ModifyAsync(x => x.Avatar = avatarStream); await ReplyConfirmationAsync(Localization.BotAvatarSet); } catch { await ReplyErrorAsync(Localization.BotSetAvatarError); } }
public async Task AddEmojiAsync(string url, [Remainder] string name) { if (!Uri.TryCreate(url, UriKind.Absolute, out var emojiUri)) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); return; } if (emojiUri.Scheme != Uri.UriSchemeHttps) { await ReplyErrorAsync(Localization.UtilityUrlNotHttps); return; } using var result = await _httpClient.GetAsync(emojiUri); if (!result.IsSuccessStatusCode) { await ReplyErrorAsync(Localization.UtilityImageOrUrlNotGood); return; } await using var stream = await result.Content.ReadAsStreamAsync(); await using var emojiStream = new MemoryStream(); await stream.CopyToAsync(emojiStream); emojiStream.Position = 0; bool?isAnimated = null; if (RiasUtilities.IsPng(emojiStream) || RiasUtilities.IsJpg(emojiStream)) { isAnimated = false; } if (!isAnimated.HasValue && RiasUtilities.IsGif(emojiStream)) { isAnimated = true; } if (!isAnimated.HasValue) { await ReplyErrorAsync(Localization.UtilityUrlNotPngJpgGif); return; } var emojis = Context.Guild !.Emojis.Values; var emojisSlots = Context.Guild !.GetGuildEmotesSlots(); if (isAnimated.Value) { if (emojis.Count(x => x.IsAnimated) >= emojisSlots) { await ReplyErrorAsync(Localization.AdministrationAnimatedEmojisLimit, emojisSlots); return; } } else { if (emojis.Count(x => !x.IsAnimated) >= emojisSlots) { await ReplyErrorAsync(Localization.AdministrationStaticEmojisLimit, emojisSlots); return; } } if (emojiStream.Length / 1024 > 256) //in KB { await ReplyErrorAsync(Localization.AdministrationEmojiSizeLimit); return; } name = name.Replace(" ", "_"); emojiStream.Position = 0; await Context.Guild.CreateEmojiAsync(emojiStream, name); await ReplyConfirmationAsync(Localization.AdministrationEmojiCreated, name); }
public async Task AddEmojiAsync(string emoji, [Remainder] string name) { var httpClient = HttpClient; await using var emojiStream = new MemoryStream(); bool?isAnimated = null; if (RiasUtilities.TryParseEmoji(emoji, out var emojiId)) { var emojiUrl = string.Format(EmojiCdn, $"{emojiId}.gif"); var result = await httpClient.GetAsync(emojiUrl); isAnimated = true; if (result.StatusCode == HttpStatusCode.UnsupportedMediaType) { emojiUrl = string.Format(EmojiCdn, $"{emojiId}.png"); result = await httpClient.GetAsync(emojiUrl); isAnimated = false; if (!result.IsSuccessStatusCode) { return; } } await using var stream = await result.Content.ReadAsStreamAsync(); await stream.CopyToAsync(emojiStream); emojiStream.Position = 0; } if (!isAnimated.HasValue) { if (!Uri.TryCreate(emoji, UriKind.Absolute, out var emojiUri)) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); return; } if (emojiUri.Scheme != Uri.UriSchemeHttps) { await ReplyErrorAsync(Localization.UtilityUrlNotHttps); return; } using var result = await httpClient.GetAsync(emojiUri); if (!result.IsSuccessStatusCode) { await ReplyErrorAsync(Localization.UtilityImageOrUrlNotGood); return; } await using var stream = await result.Content.ReadAsStreamAsync(); await stream.CopyToAsync(emojiStream); emojiStream.Position = 0; if (RiasUtilities.IsPng(emojiStream) || RiasUtilities.IsJpg(emojiStream)) { isAnimated = false; } if (!isAnimated.HasValue && RiasUtilities.IsGif(emojiStream)) { isAnimated = true; } if (!isAnimated.HasValue) { await ReplyErrorAsync(Localization.UtilityUrlNotPngJpgGif); return; } } var emojis = Context.Guild !.Emojis.Values; var emojisSlots = Context.Guild !.GetGuildEmotesSlots(); if (isAnimated.Value) { if (emojis.Count(x => x.IsAnimated) >= emojisSlots) { await ReplyErrorAsync(Localization.AdministrationAnimatedEmojisLimit, emojisSlots); return; } } else { if (emojis.Count(x => !x.IsAnimated) >= emojisSlots) { await ReplyErrorAsync(Localization.AdministrationStaticEmojisLimit, emojisSlots); return; } } // Check if length is bigger than 256 KB if (emojiStream.Length / 1024 > 256) { await ReplyErrorAsync(Localization.AdministrationEmojiSizeLimit); return; } name = name.Replace(" ", "_"); emojiStream.Position = 0; await Context.Guild.CreateEmojiAsync(name, emojiStream); await ReplyConfirmationAsync(Localization.AdministrationEmojiCreated, name); }
public async Task BackgroundAsync(string url) { var userDb = await DbContext.GetOrAddAsync(x => x.UserId == Context.User.Id, () => new UsersEntity { UserId = Context.User.Id }); if (userDb.Currency < 1000) { await ReplyErrorAsync(Localization.GamblingCurrencyNotEnough, Credentials.Currency); return; } if (!Uri.TryCreate(url, UriKind.Absolute, out var backgroundUri)) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); Context.Command.ResetCooldowns(); return; } if (backgroundUri.Scheme != Uri.UriSchemeHttps) { await ReplyErrorAsync(Localization.UtilityUrlNotHttps); Context.Command.ResetCooldowns(); return; } using var _ = Context.Channel.Typing(); using var result = await _httpClient.GetAsync(backgroundUri); if (!result.IsSuccessStatusCode) { await ReplyErrorAsync(Localization.UtilityImageOrUrlNotGood); Context.Command.ResetCooldowns(); return; } await using var backgroundStream = await result.Content.ReadAsStreamAsync(); if (!(RiasUtilities.IsPng(backgroundStream) || RiasUtilities.IsJpg(backgroundStream))) { await ReplyErrorAsync(Localization.UtilityUrlNotPngJpg); return; } var currentMember = Context.Guild !.CurrentMember; if (!currentMember.Permissions.AttachFiles) { await ReplyErrorAsync(Localization.ProfileBackgroundNoAttachFilesPermission); return; } if (!currentMember.GetPermissionsFor((CachedTextChannel)Context.Channel).AttachFiles) { await ReplyErrorAsync(Localization.ProfileBackgroundNoAttachFilesChannelPermission); return; } backgroundStream.Position = 0; await using var profilePreview = await Service.GenerateProfileBackgroundAsync(Context.User, backgroundStream); await Context.Channel.SendMessageAsync(new[] { new LocalAttachment(profilePreview, $"{Context.User.Id}_profile_preview.png") }, GetText(Localization.ProfileBackgroundPreview)); var messageReceived = await NextMessageAsync(); if (!string.Equals(messageReceived?.Message.Content, GetText(Localization.CommonYes), StringComparison.InvariantCultureIgnoreCase)) { await ReplyErrorAsync(Localization.ProfileBackgroundCanceled); return; } userDb.Currency -= 1000; var profileDb = await DbContext.GetOrAddAsync(x => x.UserId == Context.User.Id, () => new ProfileEntity { UserId = Context.User.Id, BackgroundDim = 50 }); profileDb.BackgroundUrl = url; await DbContext.SaveChangesAsync(); await ReplyConfirmationAsync(Localization.ProfileBackgroundSet); }
public async Task BackgroundAsync(string url) { var userDb = await DbContext.GetOrAddAsync(x => x.UserId == Context.User.Id, () => new UserEntity { UserId = Context.User.Id }); if (userDb.Currency < 1000) { await ReplyErrorAsync(Localization.GamblingCurrencyNotEnough, Configuration.Currency); return; } if (!Uri.TryCreate(url, UriKind.Absolute, out var backgroundUri)) { await ReplyErrorAsync(Localization.UtilityUrlNotValid); Context.Command.ResetCooldowns(); return; } if (backgroundUri.Scheme != Uri.UriSchemeHttps) { await ReplyErrorAsync(Localization.UtilityUrlNotHttps); Context.Command.ResetCooldowns(); return; } await Context.Channel.TriggerTypingAsync(); using var result = await HttpClient.GetAsync(backgroundUri); if (!result.IsSuccessStatusCode) { await ReplyErrorAsync(Localization.UtilityImageOrUrlNotGood); Context.Command.ResetCooldowns(); return; } await using var backgroundStream = await result.Content.ReadAsStreamAsync(); if (!(RiasUtilities.IsPng(backgroundStream) || RiasUtilities.IsJpg(backgroundStream))) { await ReplyErrorAsync(Localization.UtilityUrlNotPngJpg); return; } var serverAttachFilesPerm = Context.Guild !.CurrentMember.GetPermissions().HasPermission(Permissions.AttachFiles); var channelAttachFilesPerm = Context.Guild !.CurrentMember.PermissionsIn(Context.Channel).HasPermission(Permissions.AttachFiles); if (!serverAttachFilesPerm && !channelAttachFilesPerm) { await ReplyErrorAsync(Localization.ProfileBackgroundNoAttachFilesPermission); return; } if (serverAttachFilesPerm && !channelAttachFilesPerm) { await ReplyErrorAsync(Localization.ProfileBackgroundNoAttachFilesChannelPermission); return; } backgroundStream.Position = 0; await using var profilePreview = await Service.GenerateProfileBackgroundAsync(Context.User, backgroundStream); var componentInteractionArgs = await SendConfirmationButtonsAsync(new DiscordMessageBuilder() .WithEmbed(new DiscordEmbedBuilder { Color = RiasUtilities.Yellow, Description = GetText(Localization.ProfileBackgroundPreview), ImageUrl = $"attachment://{Context.User.Id}_profile_preview.png" }) .WithFile($"{Context.User.Id}_profile_preview.png", profilePreview)); if (componentInteractionArgs is null) { return; } userDb.Currency -= 1000; var profileDb = await DbContext.GetOrAddAsync(x => x.UserId == Context.User.Id, () => new ProfileEntity { UserId = Context.User.Id, BackgroundDim = 50 }); profileDb.BackgroundUrl = url; await DbContext.SaveChangesAsync(); await ButtonsActionModifyDescriptionAsync(componentInteractionArgs.Value.Result.Message, Localization.ProfileBackgroundSet); }