/// <inheritdoc /> public virtual async Task <Result <IUser> > ModifyCurrentUserAsync ( Optional <string> username, Optional <Stream?> avatar = default, CancellationToken ct = default ) { var packAvatar = await ImagePacker.PackImageAsync(avatar, ct); if (!packAvatar.IsSuccess) { return(Result <IUser> .FromError(packAvatar)); } var avatarData = packAvatar.Entity; return(await this.DiscordHttpClient.PatchAsync <IUser> ( "users/@me", b => b.WithJson ( json => { json.Write("username", username, this.JsonOptions); json.Write("avatar", avatarData, this.JsonOptions); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IWebhook> > ModifyWebhookWithTokenAsync ( Snowflake webhookID, string token, Optional <string> name = default, Optional <Stream?> avatar = default, CancellationToken ct = default ) { var packAvatar = await ImagePacker.PackImageAsync(avatar, ct); if (!packAvatar.IsSuccess) { return(Result <IWebhook> .FromError(new GenericError("Failed to pack avatar."), packAvatar)); } var avatarData = packAvatar.Entity; return(await _discordHttpClient.PatchAsync <IWebhook> ( $"webhooks/{webhookID}/{token}", b => b.WithJson ( json => { json.Write("name", name, _jsonOptions); json.Write("avatar", avatarData, _jsonOptions); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IGuild> > CreateGuildAsync ( string name, Optional <Stream> icon = default, Optional <VerificationLevel> verificationLevel = default, Optional <MessageNotificationLevel> defaultMessageNotifications = default, Optional <ExplicitContentFilterLevel> explicitContentFilter = default, Optional <IReadOnlyList <IRole> > roles = default, Optional <IReadOnlyList <IPartialChannel> > channels = default, Optional <Snowflake> afkChannelID = default, Optional <TimeSpan> afkTimeout = default, Optional <Snowflake> systemChannelID = default, Optional <SystemChannelFlags> systemChannelFlags = default, CancellationToken ct = default ) { if (name.Length < 2 || name.Length > 100) { return(new GenericError("The name must be between 2 and 100 characters.")); } await using var memoryStream = new MemoryStream(); var packIcon = await ImagePacker.PackImageAsync(new Optional <Stream?>(icon.Value), ct); if (!packIcon.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), packIcon)); } var iconData = packIcon.Entity; return(await _discordHttpClient.PostAsync <IGuild> ( "guilds", b => b.WithJson ( json => { json.WriteString("name", name); json.Write("icon", iconData, _jsonOptions); json.Write("verification_level", verificationLevel, _jsonOptions); json.Write("default_message_notifications", defaultMessageNotifications, _jsonOptions); json.Write("explicit_content_filter", explicitContentFilter, _jsonOptions); json.Write("roles", roles, _jsonOptions); json.Write("channels", channels, _jsonOptions); json.Write("afk_channel_id", afkChannelID, _jsonOptions); if (afkTimeout.HasValue) { json.WriteNumber("afk_timeout", (ulong)afkTimeout.Value.TotalSeconds); } json.Write("system_channel_id", systemChannelID, _jsonOptions); json.Write("system_channel_flags", systemChannelFlags, _jsonOptions); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IGuild> > CreateGuildFromTemplateAsync ( string templateCode, string name, Optional <Stream> icon = default, CancellationToken ct = default ) { var packIcon = await ImagePacker.PackImageAsync(new Optional <Stream?>(icon.Value), ct); if (!packIcon.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), packIcon)); } var iconData = packIcon.Entity; return(await _discordHttpClient.PostAsync <IGuild> ( $"guilds/templates/{templateCode}", b => b.WithJson ( j => { j.WriteString("name", name); j.Write("icon", iconData); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IWebhook> > ModifyWebhookAsync ( Snowflake webhookID, Optional <string> name = default, Optional <Stream?> avatar = default, Optional <Snowflake> channelID = default, CancellationToken ct = default ) { var packAvatar = await ImagePacker.PackImageAsync(avatar, ct); if (!packAvatar.IsSuccess) { return(Result <IWebhook> .FromError(packAvatar)); } var avatarData = packAvatar.Entity; return(await this.DiscordHttpClient.PatchAsync <IWebhook> ( $"webhooks/{webhookID}", b => b.WithJson ( json => { json.Write("name", name, this.JsonOptions); json.Write("avatar", avatarData, this.JsonOptions); json.Write("channel_id", channelID, this.JsonOptions); } ), ct : ct )); }
public async Task CanPackGIFAsync() { // Create a dummy GIF image await using var image = new MemoryStream(); await using var binaryWriter = new BinaryWriter(image); binaryWriter.Write(0x00464947); image.Position = 0; var result = await ImagePacker.PackImageAsync(image); ResultAssert.Successful(result); }
public async Task CanPackPNGAsync() { // Create a dummy PNG image await using var image = new MemoryStream(); await using var binaryWriter = new BinaryWriter(image); binaryWriter.Write(9894494448401390090); image.Position = 0; var result = await ImagePacker.PackImageAsync(image); ResultAssert.Successful(result); }
public async Task ReturnsErrorIfFormatIsUnknown() { // Create a dummy unknown image await using var image = new MemoryStream(); await using var binaryWriter = new BinaryWriter(image); binaryWriter.Write(0x00000000); image.Position = 0; var result = await ImagePacker.PackImageAsync(image); ResultAssert.Unsuccessful(result); }
/// <inheritdoc /> public virtual async Task <Result <IWebhook> > CreateWebhookAsync ( Snowflake channelID, string name, Optional <Stream?> avatar, CancellationToken ct = default ) { if (name.Length < 1 || name.Length > 80) { return(new GenericError("Names must be between 1 and 80 characters")); } if (name.Equals("clyde", StringComparison.InvariantCultureIgnoreCase)) { return(new GenericError("Names cannot be \"clyde\".")); } var packAvatar = await ImagePacker.PackImageAsync(avatar, ct); if (!packAvatar.IsSuccess) { return(Result <IWebhook> .FromError(new GenericError("Failed to pack avatar."), packAvatar)); } var avatarData = packAvatar.Entity; return(await _discordHttpClient.PostAsync <IWebhook> ( $"channels/{channelID}/webhooks", b => b.WithJson ( json => { json.WriteString("name", name); json.WriteString("avatar", avatarData.Value); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IEmoji> > CreateGuildEmojiAsync ( Snowflake guildID, string name, Stream image, IReadOnlyList <Snowflake> roles, CancellationToken ct = default ) { if (image.Length > 256000) { return(new NotSupportedError("Image too large (max 256k).")); } var packImage = await ImagePacker.PackImageAsync(image, ct); if (!packImage.IsSuccess) { return(Result <IEmoji> .FromError(packImage)); } var emojiData = packImage.Entity; return(await this.DiscordHttpClient.PostAsync <IEmoji> ( $"guilds/{guildID}/emojis", b => b.WithJson ( json => { json.WriteString("name", name); json.WriteString("image", emojiData); json.WritePropertyName("roles"); JsonSerializer.Serialize(json, roles, this.JsonOptions); } ), ct : ct )); }
/// <inheritdoc /> public virtual async Task <Result <IGuild> > ModifyGuildAsync ( Snowflake guildID, Optional <string> name = default, Optional <string?> region = default, Optional <VerificationLevel?> verificationLevel = default, Optional <MessageNotificationLevel?> defaultMessageNotifications = default, Optional <ExplicitContentFilterLevel?> explicitContentFilter = default, Optional <Snowflake?> afkChannelID = default, Optional <TimeSpan> afkTimeout = default, Optional <Stream?> icon = default, Optional <Snowflake> ownerID = default, Optional <Stream?> splash = default, Optional <Stream?> discoverySplash = default, Optional <Stream?> banner = default, Optional <Snowflake?> systemChannelID = default, Optional <SystemChannelFlags> systemChannelFlags = default, Optional <Snowflake?> rulesChannelID = default, Optional <Snowflake?> publicUpdatesChannelID = default, Optional <string?> preferredLocale = default, Optional <IReadOnlyList <GuildFeature> > features = default, Optional <string?> description = default, CancellationToken ct = default ) { await using var memoryStream = new MemoryStream(); Optional <string?> iconData = default; if (icon.HasValue) { if (icon.Value is null) { iconData = new Optional <string?>(null); } else { var packImage = await ImagePacker.PackImageAsync(icon.Value, ct); if (!packImage.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack icon."), packImage)); } iconData = packImage.Entity; } } Optional <string?> splashData = default; if (splash.HasValue) { if (splash.Value is null) { splashData = new Optional <string?>(null); } else { var packImage = await ImagePacker.PackImageAsync(splash.Value, ct); if (!packImage.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack splash."), packImage)); } splashData = packImage.Entity; } } Optional <string?> discoverySplashData = default; if (discoverySplash.HasValue) { if (discoverySplash.Value is null) { discoverySplashData = new Optional <string?>(null); } else { var packImage = await ImagePacker.PackImageAsync(discoverySplash.Value, ct); if (!packImage.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack discovery splash."), packImage)); } discoverySplashData = packImage.Entity; } } var packBanner = await ImagePacker.PackImageAsync(banner, ct); if (!packBanner.IsSuccess) { return(Result <IGuild> .FromError(new GenericError("Failed to pack banner."), packBanner)); } var bannerData = packBanner.Entity; return(await _discordHttpClient.PatchAsync <IGuild> ( $"guilds/{guildID}", b => b.WithJson ( json => { json.Write("name", name, _jsonOptions); json.Write("region", region, _jsonOptions); json.WriteEnum("verification_level", verificationLevel, jsonOptions: _jsonOptions); json.WriteEnum ( "default_message_notifications", defaultMessageNotifications, jsonOptions: _jsonOptions ); json.WriteEnum("explicit_content_filter", explicitContentFilter, jsonOptions: _jsonOptions); json.Write("afk_channel_id", afkChannelID, _jsonOptions); if (afkTimeout.HasValue) { json.WriteNumber("afk_timeout", (ulong)afkTimeout.Value.TotalSeconds); } json.Write("icon", iconData, _jsonOptions); json.Write("owner_id", ownerID, _jsonOptions); json.Write("splash", splashData, _jsonOptions); json.Write("discovery_splash", discoverySplashData, _jsonOptions); json.Write("banner", bannerData, _jsonOptions); json.Write("system_channel_id", systemChannelID, _jsonOptions); json.Write("system_channel_flags", systemChannelFlags, _jsonOptions); json.Write("rules_channel_id", rulesChannelID, _jsonOptions); json.Write("public_updates_channel_id", publicUpdatesChannelID, _jsonOptions); json.Write("preferred_locale", preferredLocale, _jsonOptions); json.Write("features", features, _jsonOptions); json.Write("description", description, _jsonOptions); } ), ct : ct )); }