#UserPrescence.User Cycle

1 messages · Page 1 of 1 (latest)

glossy rose
#

Receiving following error:

[2023-04-17 18:26:21 -04:00] [104 /EventHandler] [Error] Event handler exception for event MESSAGE_CREATED thrown from System.Threading.Tasks.Task <Main>b__3_0(DSharpPlus.DiscordClient, DSharpPlus.EventArgs.MessageCreateEventArgs) (defined in AuctionHouseDiscord.Program+<>c)
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.User.Presence.User.Presence.User.Presence.User.Presence.User.Presence.User.Presence.User.Presence.User.Presence...(you get the idea)
   at System.Text.Json.ThrowHelper.ThrowJsonException_SerializerCycleDetected(Int32 maxDepth)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)

happened as i was deserlising a json of the following class:

    public class Player
    {
        public int Age { get; set; }
        public DiscordUser User { get; set; }
        public List<ItemQuantity> Inventory { get; set; } = new();
    }
    public class ItemQuantity
    {
        public int Quantity { get; set; }
        public int ItemID { get; set; }
    }
#

not even quite sure how this happened

last prism
#

Hey I wouldn't really recommend serializing entities, however if it's required then I suggest using the DiscordJson class instead

#

How you're going to intertwine DiscordJson and STJ I'm not really sure

glossy rose
#

i see

#

if i use the userid instead

last prism
#

But if you're just saving users to the database, I recommend storing the user id instead

glossy rose
#

would that avoid this error?

last prism
#

Yeah

glossy rose
#

alr thanks

last prism
#

That and you save a lot of database space

glossy rose
#

was what i orignally was doing anyways

last prism
#

And you don't have outdated information too

glossy rose
#

but thought that would save time

last prism
#

I don't think it would. Whenever a user object is received from the gateway, it's cached into the DiscordClient

#

So doing await client.GetUserAsync usually grabs the user from cache instead of making a rest request (which in turn saves ratelimits)

glossy rose
#

i see

#

it might be worth setting a warning for that although i understand that it might not be a priority

last prism
#

A warning in the docs? Maybe

#

In the code? Oh goodness that'd spam up logs so fast

#

A lot of our methods rely on the information from the cache

#

So you could only imagine how often a user is grabbed from the cache

#

Right now it's a tradeoff between highish memory usage for convenience