Skip to content

Commit

Permalink
Update ScriptLogger.cs
Browse files Browse the repository at this point in the history
~ Take into account that forum channels exist.
~ Allow logging of already existing script hashes (just log the hash instead of the whole content)
  • Loading branch information
jf-06 committed Sep 3, 2024
1 parent 7ce11b0 commit 9bc014a
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions services/grid-bot/lib/utility/Implementation/ScriptLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex

if (!_percentageInvoker.CanInvoke(_scriptsSettings.ScriptLoggingPercentage)) return;

// Get a SHA256 hash of the script (hex)
var scriptHash = string.Join("", SHA256.HashData(Encoding.UTF8.GetBytes(script)).Select(b => b.ToString("x2")));
if (_scriptHashes.Contains(scriptHash)) return;

// username based off machine info
var username = $"{Environment.MachineName} ({_localIpAddressProvider.AddressV4} / {_localIpAddressProvider.AddressV6})";
var userInfo = context.User.ToString();
var guildInfo = context.Guild?.ToString() ?? "DMs";
var channelInfo = context.Channel.ToString();
var channelInfo = context.Channel?.ToString() ?? "Forum Channel";



// Get a SHA256 hash of the script (hex)
var scriptHash = string.Join("", SHA256.HashData(Encoding.UTF8.GetBytes(script)).Select(b => b.ToString("x2")));
var content = $"""
**User:** {userInfo}
**Guild:** {guildInfo}
Expand All @@ -99,6 +99,26 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex

using var client = _httpClientFactory.CreateClient();
var url = _scriptsSettings.ScriptLoggingDiscordWebhookUrl;


if (_scriptHashes.Contains(scriptHash))
{
// Just log the hash
content += "\n\n**Script already logged**";

var existsPayload = new
{
username,
content
};

var existsJson = JsonConvert.SerializeObject(existsPayload);

await client.PostAsync(url, new StringContent(existsJson, Encoding.UTF8, "application/json"));

return;
}

var payload = new
{
username,
Expand Down

0 comments on commit 9bc014a

Please sign in to comment.