Skip to content

Commit

Permalink
#336: Script Logger
Browse files Browse the repository at this point in the history
~ Reorganize code to support both interaction and command context
~ Add Discord.Net.Commands dependency to Shared.Utility
  • Loading branch information
jf-06 committed Oct 1, 2024
1 parent ca7507e commit 4180ad3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
38 changes: 32 additions & 6 deletions services/grid-bot/lib/utility/Implementation/ScriptLogger.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Grid.Bot.Utility;

using Discord;
using Discord.WebSocket;
using Discord.Interactions;

using Newtonsoft.Json;

using Random;
using Networking;

using Extensions;
using Discord.Commands;


/// <summary>
Expand Down Expand Up @@ -81,19 +81,43 @@ private void PersistLoggedScriptHashes()
}
}

/// <inheritdoc cref="IScriptLogger.LogScriptAsync(string, ShardedInteractionContext)"/>
public async Task LogScriptAsync(string script, ShardedInteractionContext context)
/// <inheritdoc cref="IScriptLogger.LogScriptAsync(string, IInteractionContext)"/>
public async Task LogScriptAsync(string script, IInteractionContext context)
{
if (string.IsNullOrWhiteSpace(script)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(script));
ArgumentNullException.ThrowIfNull(context, nameof(context));

if (!_percentageInvoker.CanInvoke(_scriptsSettings.ScriptLoggingPercentage)) return;
if (context.Interaction is not SocketInteraction interaction) return;

// username based off machine info
var userInfo = context.User.ToString();
var guildInfo = interaction.GetGuild(context.Client)?.ToString() ?? "DMs";
var channelInfo = interaction.GetChannelAsString();

await DoLogScriptAsync(script, userInfo, guildInfo, channelInfo);
}

/// <inheritdoc cref="IScriptLogger.LogScriptAsync(string, ICommandContext)"/>
public async Task LogScriptAsync(string script, ICommandContext context)
{
if (string.IsNullOrWhiteSpace(script)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(script));
ArgumentNullException.ThrowIfNull(context, nameof(context));

if (!_percentageInvoker.CanInvoke(_scriptsSettings.ScriptLoggingPercentage)) return;
if (context.Message is not SocketUserMessage message) return;

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

await DoLogScriptAsync(script, userInfo, guildInfo, channelInfo);
}

private async Task DoLogScriptAsync(string script, string userInfo, string guildInfo, string channelInfo)
{
var username = $"{Environment.MachineName} ({_localIpAddressProvider.AddressV4} / {_localIpAddressProvider.AddressV6})";

// Get a SHA256 hash of the script (hex)
var scriptHash = string.Join("", SHA256.HashData(Encoding.UTF8.GetBytes(script)).Select(b => b.ToString("x2")));
Expand All @@ -105,6 +129,7 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex
""";

using var client = _httpClientFactory.CreateClient();

var url = _scriptsSettings.ScriptLoggingDiscordWebhookUrl;


Expand Down Expand Up @@ -154,4 +179,5 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex
// Add the hash to the list of logged hashes
_scriptHashes.Add(scriptHash);
}

}
15 changes: 12 additions & 3 deletions services/grid-bot/lib/utility/Interfaces/IScriptLogger.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace Grid.Bot.Utility;

using System.Threading.Tasks;

using Discord.Interactions;
using Discord;
using Discord.Commands;

/// <summary>
/// Handles logging the contents of scripts to a Discord webhook.
Expand All @@ -23,7 +24,15 @@ public interface IScriptLogger
/// Logs the contents of a script to a Discord webhook.
/// </summary>
/// <param name="script">The script to log.</param>
/// <param name="context">The <see cref="ShardedInteractionContext"/> to use.</param>
/// <param name="context">The <see cref="IInteractionContext"/> to use.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task LogScriptAsync(string script, ShardedInteractionContext context);
Task LogScriptAsync(string script, IInteractionContext context);

/// <summary>
/// Logs the contents of a script to a Discord webhook.
/// </summary>
/// <param name="script">The script to log.</param>
/// <param name="context">The <see cref="ICommandContext"/> to use.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task LogScriptAsync(string script, ICommandContext context);
}
1 change: 1 addition & 0 deletions services/grid-bot/lib/utility/Shared.Utility.csproj
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" />
<PackageReference Include="Discord.Net.WebSocket" Version="3.15.3" />
<PackageReference Include="Discord.Net.Commands" Version="3.15.3" />
</ItemGroup>

<!-- Protobuf -->
Expand Down

0 comments on commit 4180ad3

Please sign in to comment.