mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-02 03:18:23 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
76 lines
1.9 KiB
C#
76 lines
1.9 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using static Facepunch.Constants;
|
|
|
|
namespace Facepunch.Steps;
|
|
|
|
internal class DiscordPostStep( string name, string message, string author ) : Step( name )
|
|
{
|
|
public string Message { get; } = message;
|
|
public string Author { get; } = author;
|
|
|
|
protected override ExitCode RunInternal()
|
|
{
|
|
try
|
|
{
|
|
// Run the async method synchronously
|
|
return PostToDiscordAsync().GetAwaiter().GetResult();
|
|
}
|
|
catch ( Exception ex )
|
|
{
|
|
Log.Error( $"Discord post failed with error: {ex}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
}
|
|
|
|
private async Task<ExitCode> PostToDiscordAsync()
|
|
{
|
|
// Validate inputs
|
|
if ( string.IsNullOrWhiteSpace( Message ) )
|
|
{
|
|
Log.Warning( "Discord message was empty, skipping post" );
|
|
return ExitCode.Success; // Not treating this as a failure
|
|
}
|
|
|
|
var hook = Environment.GetEnvironmentVariable( "DISCORD_WEBHOOK" );
|
|
if ( string.IsNullOrEmpty( hook ) )
|
|
{
|
|
Log.Error( "DISCORD_WEBHOOK environment variable was empty" );
|
|
return ExitCode.Failure;
|
|
}
|
|
|
|
Log.Info( $"Posting to Discord webhook as {Author}" );
|
|
|
|
try
|
|
{
|
|
using ( var client = new HttpClient() )
|
|
{
|
|
var payload = new
|
|
{
|
|
content = Message.Substring( 0, Math.Min( Message.Length, 2000 ) ), // Discord message limit is 2000 characters
|
|
username = Author
|
|
};
|
|
|
|
// Serialize and post
|
|
var content = new StringContent( JsonSerializer.Serialize( payload ), Encoding.UTF8, "application/json" );
|
|
var response = await client.PostAsync( hook, content );
|
|
|
|
if ( !response.IsSuccessStatusCode )
|
|
{
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
Log.Error( $"Discord returned {response.StatusCode}: {responseString}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
|
|
Log.Info( "Successfully posted to Discord" );
|
|
return ExitCode.Success;
|
|
}
|
|
}
|
|
catch ( Exception ex )
|
|
{
|
|
Log.Error( $"Discord post error: {ex}" );
|
|
return ExitCode.Failure;
|
|
}
|
|
}
|
|
}
|