C#C
C#17mo ago
13eck

My Discord app is using all my memory. What am I doing wrong?

I'm making what I thought was a simple dice rolling app for Discord. The user tells it how many dice to roll, any static modifiers to the total, and an optional description for the action being taken.

However, when I run the app I can only do a small handful of commands before my computer tells me that VS Code is using too much memory and I need to close it down. Sometimes the console even says something about heartbeat being too slow and a possible thread starvation.

I don't have that much code written so I'm not sure what's going on, but here's what I currently have:

// Program.cs
using D6.Api.Commands;
using D6.Api.Discord;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using System.Text.Json.Nodes;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<DiscordHelper>();

var app = builder.Build();

app.MapPost("/", async (HttpContext ctx,
                [FromHeader(Name = "X-Signature-Ed25519")] string sig,
                [FromHeader(Name = "X-Signature-Timestamp")] string timestamp,
                [FromServices] DiscordHelper DiscordHelper) =>
{

    using StreamReader reader = new(ctx.Request.Body);
    string body = await reader.ReadToEndAsync();
    JsonDocument jsonBody = JsonDocument.Parse(body);
    JsonElement interaction = jsonBody.RootElement.Clone();
    jsonBody.Dispose();

    bool is_verified = DiscordHelper.VerifySignature(body, timestamp, sig);

    if (!is_verified)
    {
        return Results.Unauthorized();
    }

    int interaction_type = interaction.GetProperty("type").GetInt32();

    if (interaction_type == 1)
    {
        return Results.Ok(new JsonObject
        {
            ["type"] = 1
        });
    }

    string CommandName = interaction.GetProperty("data").GetProperty("name").GetString()!;

    JsonObject JsonReply = CommandName switch
    {
        "d6" => Commands.D6(interaction),
        _ => new JsonObject()
    };

    return Results.Ok(JsonReply);
});

app.Run();
Was this page helpful?