// 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();
// 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();