Skip to content

Commit 9bc014a

Browse files
committed
Update ScriptLogger.cs
~ Take into account that forum channels exist. ~ Allow logging of already existing script hashes (just log the hash instead of the whole content)
1 parent 7ce11b0 commit 9bc014a

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

services/grid-bot/lib/utility/Implementation/ScriptLogger.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex
8080

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

83-
// Get a SHA256 hash of the script (hex)
84-
var scriptHash = string.Join("", SHA256.HashData(Encoding.UTF8.GetBytes(script)).Select(b => b.ToString("x2")));
85-
if (_scriptHashes.Contains(scriptHash)) return;
86-
8783
// username based off machine info
8884
var username = $"{Environment.MachineName} ({_localIpAddressProvider.AddressV4} / {_localIpAddressProvider.AddressV6})";
8985
var userInfo = context.User.ToString();
9086
var guildInfo = context.Guild?.ToString() ?? "DMs";
91-
var channelInfo = context.Channel.ToString();
87+
var channelInfo = context.Channel?.ToString() ?? "Forum Channel";
88+
89+
9290

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

100100
using var client = _httpClientFactory.CreateClient();
101101
var url = _scriptsSettings.ScriptLoggingDiscordWebhookUrl;
102+
103+
104+
if (_scriptHashes.Contains(scriptHash))
105+
{
106+
// Just log the hash
107+
content += "\n\n**Script already logged**";
108+
109+
var existsPayload = new
110+
{
111+
username,
112+
content
113+
};
114+
115+
var existsJson = JsonConvert.SerializeObject(existsPayload);
116+
117+
await client.PostAsync(url, new StringContent(existsJson, Encoding.UTF8, "application/json"));
118+
119+
return;
120+
}
121+
102122
var payload = new
103123
{
104124
username,

0 commit comments

Comments
 (0)