A Discord API Library for Gleam! 💫
0

Configure Feed

Select the types of activity you want to include in your feed.

bugfix!: incorrect type signature in current_user.get_guilds

+67 -13
+66 -12
src/grom/user/current_user.gleam
··· 1 1 import gleam/bool 2 + import gleam/dynamic/decode 2 3 import gleam/http 3 4 import gleam/http/request 4 5 import gleam/int ··· 8 9 import gleam/result 9 10 import gleam/string 10 11 import grom 12 + import grom/guild 11 13 import grom/guild_member.{type GuildMember} 12 14 import grom/image 13 15 import grom/internal/rest 16 + import grom/permission.{type Permission} 14 17 import grom/user.{type User} 15 18 16 19 // TYPES ---------------------------------------------------------------------- ··· 24 27 } 25 28 26 29 pub type GetGuildsQuery { 27 - BeforeId(String) 28 - AfterId(String) 29 - Limit(Int) 30 - WithCounts(Bool) 30 + GetGuildsBeforeId(String) 31 + GetGuildsAfterId(String) 32 + GetGuildsLimit(Int) 33 + /// Refers to the approximate_member and approximate_presence counts 34 + GetGuildsWithCounts(Bool) 35 + } 36 + 37 + pub type PartialGuild { 38 + PartialGuild( 39 + id: String, 40 + name: String, 41 + icon_hash: Option(String), 42 + banner_hash: Option(String), 43 + is_current_user_owner: Bool, 44 + current_user_permissions: List(Permission), 45 + features: List(guild.Feature), 46 + approximate_member_count: Option(Int), 47 + approximate_presence_count: Option(Int), 48 + ) 49 + } 50 + 51 + // DECODERS ------------------------------------------------------------------- 52 + 53 + fn partial_guild_decoder() -> decode.Decoder(PartialGuild) { 54 + use id <- decode.field("id", decode.string) 55 + use name <- decode.field("name", decode.string) 56 + use icon_hash <- decode.field("icon", decode.optional(decode.string)) 57 + use banner_hash <- decode.field("banner", decode.optional(decode.string)) 58 + use is_current_user_owner <- decode.field("owner", decode.bool) 59 + use current_user_permissions <- decode.field( 60 + "permissions", 61 + permission.decoder(), 62 + ) 63 + use features <- decode.field("features", decode.list(guild.feature_decoder())) 64 + use approximate_member_count <- decode.optional_field( 65 + "approximate_member_count", 66 + None, 67 + decode.optional(decode.int), 68 + ) 69 + use approximate_presence_count <- decode.optional_field( 70 + "approximate_presence_count", 71 + None, 72 + decode.optional(decode.int), 73 + ) 74 + decode.success(PartialGuild( 75 + id:, 76 + name:, 77 + icon_hash:, 78 + banner_hash:, 79 + is_current_user_owner:, 80 + current_user_permissions:, 81 + features:, 82 + approximate_member_count:, 83 + approximate_presence_count:, 84 + )) 31 85 } 32 86 33 87 // ENCODERS -------------------------------------------------------------------- 34 88 35 89 @internal 36 - pub fn modify_encode(modify: Modify) -> Json { 90 + pub fn modify_to_json(modify: Modify) -> Json { 37 91 let Modify(username:, avatar:, banner:) = modify 38 92 let username = case username { 39 93 Some(name) -> [#("username", json.string(name))] ··· 73 127 client: grom.Client, 74 128 with data: Modify, 75 129 ) -> Result(User, grom.Error) { 76 - let json = data |> modify_encode 130 + let json = data |> modify_to_json 77 131 use response <- result.try( 78 132 client 79 133 |> rest.new_request(http.Patch, "/users/@me") ··· 93 147 pub fn get_guilds( 94 148 client: grom.Client, 95 149 with query: List(GetGuildsQuery), 96 - ) -> Result(User, grom.Error) { 150 + ) -> Result(List(PartialGuild), grom.Error) { 97 151 let query = 98 152 list.map(query, fn(parameter) { 99 153 case parameter { 100 - BeforeId(id) -> #("before", id) 101 - AfterId(id) -> #("after", id) 102 - Limit(limit) -> #("limit", limit |> int.to_string) 103 - WithCounts(with_counts) -> #( 154 + GetGuildsBeforeId(id) -> #("before", id) 155 + GetGuildsAfterId(id) -> #("after", id) 156 + GetGuildsLimit(limit) -> #("limit", limit |> int.to_string) 157 + GetGuildsWithCounts(with_counts) -> #( 104 158 "with_counts", 105 159 with_counts 106 160 |> bool.to_string ··· 117 171 ) 118 172 119 173 response.body 120 - |> json.parse(using: user.decoder()) 174 + |> json.parse(using: decode.list(of: partial_guild_decoder())) 121 175 |> result.map_error(grom.CouldNotDecode) 122 176 } 123 177
+1 -1
test/grom_test.gleam
··· 17 17 18 18 pub fn encode_modify_current_test() { 19 19 current_user.Modify(..current_user.new_modify(), username: Some("fo1o")) 20 - |> current_user.modify_encode 20 + |> current_user.modify_to_json 21 21 |> json.to_string 22 22 |> should.equal("{\"username\":\"fo1o\"}") 23 23 }