Relay custom votes to a webhook (#18561)
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com> Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Content.Server.Discord;
|
||||
@@ -66,7 +68,7 @@ public sealed class DiscordWebhook : IPostInjectInit
|
||||
public async Task<HttpResponseMessage> CreateMessage(WebhookIdentifier identifier, WebhookPayload payload)
|
||||
{
|
||||
var url = $"{GetUrl(identifier)}?wait=true";
|
||||
return await _http.PostAsJsonAsync(url, payload);
|
||||
return await _http.PostAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,7 +93,7 @@ public sealed class DiscordWebhook : IPostInjectInit
|
||||
public async Task<HttpResponseMessage> EditMessage(WebhookIdentifier identifier, ulong messageId, WebhookPayload payload)
|
||||
{
|
||||
var url = $"{GetUrl(identifier)}/messages/{messageId}";
|
||||
return await _http.PatchAsJsonAsync(url, payload);
|
||||
return await _http.PatchAsJsonAsync(url, payload, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
|
||||
}
|
||||
|
||||
void IPostInjectInit.PostInject()
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Content.Server.Discord;
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-structure
|
||||
public struct WebhookEmbed
|
||||
{
|
||||
[JsonPropertyName("title")]
|
||||
public string Title { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; } = "";
|
||||
|
||||
@@ -14,6 +17,10 @@ public struct WebhookEmbed
|
||||
[JsonPropertyName("footer")]
|
||||
public WebhookEmbedFooter? Footer { get; set; } = null;
|
||||
|
||||
|
||||
[JsonPropertyName("fields")]
|
||||
public List<WebhookEmbedField> Fields { get; set; } = default!;
|
||||
|
||||
public WebhookEmbed()
|
||||
{
|
||||
}
|
||||
|
||||
20
Content.Server/Discord/WebhookEmbedField.cs
Normal file
20
Content.Server/Discord/WebhookEmbedField.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Content.Server.Discord;
|
||||
|
||||
// https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
|
||||
public struct WebhookEmbedField
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("value")]
|
||||
public string Value { get; set; } = "";
|
||||
|
||||
[JsonPropertyName("inline")]
|
||||
public bool Inline { get; set; } = true;
|
||||
|
||||
public WebhookEmbedField()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Content.Server.Discord;
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Content.Server.Discord;
|
||||
|
||||
@@ -9,13 +9,13 @@ public struct WebhookPayload
|
||||
/// The message to send in the webhook. Maximum of 2000 characters.
|
||||
/// </summary>
|
||||
[JsonPropertyName("content")]
|
||||
public string Content { get; set; } = "";
|
||||
public string? Content { get; set; }
|
||||
|
||||
[JsonPropertyName("username")]
|
||||
public string? Username { get; set; }
|
||||
|
||||
[JsonPropertyName("avatar_url")]
|
||||
public string? AvatarUrl { get; set; } = "";
|
||||
public string? AvatarUrl { get; set; }
|
||||
|
||||
[JsonPropertyName("embeds")]
|
||||
public List<WebhookEmbed>? Embeds { get; set; } = null;
|
||||
|
||||
Reference in New Issue
Block a user