message := webhook.Message{ Embeds: []webhook.Embed{ { Title: "Новое подключение…
- User
message := webhook.Message{ Embeds: []webhook.Embed{ { Title: "Новое подключение нарушителя", Footer: &webhook.Footer{ Text: "Version 0.3.0 | © BlockDB", }, Fields: []webhook.Field{ { Name: "👑 Владелец проекта", Value: fmt.Sprintf("<@%d>", discordID), Inline: true, }, { Name: "🔗 Steam Профиль", Value: fmt.Sprintf("%s", html.EscapeString(playerSummary[0].PersonaName), html.EscapeString(steamid64)), Inline: true, }, { Name: "Список предыдущих нарушений:", Value: bans, Inline: false, }, }, }, }, }
convert to c# like this
public async Task Send() { await _webhookClient.SendMessageAsync(embeds: new [] { new EmbedBuilder() // etc. }); }
- Assistant
Sure, here is how you can convert the provided Go code for sending a webhook message with an embed to C# using Discord.Net library:
First, ensure you have the Discord.Net nuget package installed:
dotnet add package Discord.NetThen you can structure your code like this:
using Discord; using Discord.Webhook; using System; using System.Threading.Tasks; public class WebhookService { private readonly DiscordWebhookClient _webhookClient; private readonly ulong _discordID; private readonly string _playerName; private readonly string _steamId64; private readonly string _bans; public WebhookService(string webhookUrl, ulong discordID, string playerName, string steamId64, string bans) { _webhookClient = new DiscordWebhookClient(webhookUrl); _discordID = discordID; _playerName = playerName; _steamId64 = steamId64; _bans = bans; } public async Task Send() { var embed = new EmbedBuilder() .WithTitle("Новое подключение нарушителя") .WithFooter(footer => footer.Text = "Version 0.3.0 | © BlockDB") .AddField("👑 Владелец проекта", $"<@{_discordID}>", true) .AddField("🔗 Steam Профиль", $"[{EscapeString(_playerName)}](https://steamcommunity.com/profiles/{EscapeString(_steamId64)}/)", true) .AddField("Список предыдущих нарушений:", _bans, false) .Build(); await _webhookClient.SendMessageAsync(embeds: new[] { embed }); } private string EscapeString(string value) { // Replace this method with appropriate string escaping if necessary. // In C# you might not need to escape the string but you can add necessary logic here if required. return value; } }Explanation:
- DiscordWebhookClient Initialization: We initialize the
DiscordWebhookClientwith the webhook URL. - Embed Construction: We create an
EmbedBuilderand configure it with title, footer, and fields. - String Escaping: The
EscapeStringmethod is a placeholder in case you need to escape the string for any reason. - SendMessageAsync: We send the embed using
_webhookClient.SendMessageAsync.
You can then use the
WebhookServiceclass in your application like this:// Example usage: public class Program { public static async Task Main(string[] args) { string webhookUrl = "your_webhook_url_here"; ulong discordID = 123456789012345678; string playerName = "PlayerName"; string steamId64 = "12345678901234567"; string bans = "List of previous violations."; var webhookService = new WebhookService(webhookUrl, discordID, playerName, steamId64, bans); await webhookService.Send(); } }Ensure that you replace the placeholders (
webhookUrl,discordID,playerName,steamId64, andbans) with actual values as required. - DiscordWebhookClient Initialization: We initialize the