Skip to content

Commit

Permalink
Implement channel patches
Browse files Browse the repository at this point in the history
#!components: grid-bot

discord-net/Discord.Net#2997 and #335

In the case of interactions, fallback to ChannelId.
In the case of messages, fallback to Thread ID.
  • Loading branch information
jf-06 committed Sep 3, 2024
1 parent 9bc014a commit a313599
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
4 changes: 3 additions & 1 deletion services/grid-bot/lib/commands/Modules/ExecuteScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ private async Task AlertForSystem(string script, string originalScript, string s

var userInfo = Context.User.ToString();
var guildInfo = Context.Guild?.ToString() ?? "DMs";
var channelInfo = Context.Channel.ToString();

/* Temporary until mfdlabs/grid-bot#335 is resolved */
var channelInfo = Context.Channel?.ToString() ?? Context.Interaction.ChannelId?.ToString() ?? "Thread";

// Script & original script in attachments
var scriptAttachment = new FileAttachment(new MemoryStream(Encoding.ASCII.GetBytes(script)), "script.lua");
Expand Down
4 changes: 2 additions & 2 deletions services/grid-bot/lib/events/Events/OnMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public async Task Invoke(SocketMessage rawMessage)

_totalUsersBypassedMaintenance.WithLabels(
message.Author.Id.ToString(),
message.Channel.Id.ToString(),
message.Channel?.Id.ToString() ?? message.Thread?.Id.ToString(),
GetGuildId(message)
).Inc();
}
Expand All @@ -182,7 +182,7 @@ public async Task Invoke(SocketMessage rawMessage)
{
_totalBlacklistedUserAttemptedMessages.WithLabels(
message.Author.Id.ToString(),
message.Channel.Id.ToString(),
message.Channel?.Id.ToString() ?? message.Thread?.Id.ToString(),
GetGuildId(message)
).Inc();

Expand Down
16 changes: 5 additions & 11 deletions services/grid-bot/lib/events/Events/OnSlashCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ILoggerFactory loggerFactory

private static string GetGuildId(SocketInteraction interaction)
{
/* Always false in private thread channels, please look into discord-net/Discord.Net#2997 and mfdlabs/grid-bot#335 */
if (interaction.Channel is SocketGuildChannel guildChannel)
return guildChannel.Guild.Id.ToString();

Expand Down Expand Up @@ -123,15 +124,6 @@ public async Task Invoke(SocketInteraction interaction)
interaction.Type.ToString()
).Inc();

var guildName = string.Empty;
var guildId = 0UL;

if (interaction.Channel is SocketGuildChannel guildChannel)
{
guildName = guildChannel.Guild.Name;
guildId = guildChannel.Guild.Id;
}

logger.Warning("Maintenance enabled user tried to use the bot.");

var failureMessage = _maintenanceSettings.MaintenanceStatus;
Expand All @@ -157,7 +149,8 @@ public async Task Invoke(SocketInteraction interaction)

_totalUsersBypassedMaintenance.WithLabels(
interaction.User.Id.ToString(),
interaction.Channel.Id.ToString(),
/* Temporary until mfdlabs/grid-bot#335 is resolved */
interaction.Channel?.Id.ToString() ?? interaction.ChannelId?.ToString() ?? "Thread",
GetGuildId(interaction)
).Inc();
}
Expand All @@ -166,7 +159,8 @@ public async Task Invoke(SocketInteraction interaction)
{
_totalBlacklistedUserAttemptedInteractions.WithLabels(
interaction.User.Id.ToString(),
interaction.Channel.Id.ToString(),
/* Temporary until mfdlabs/grid-bot#335 is resolved */
interaction.Channel?.Id.ToString() ?? interaction.ChannelId?.ToString() ?? "Thread",
GetGuildId(interaction)
).Inc();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public async Task Invoke(ICommandInfo command, IInteractionContext context, IRes
interaction.Type.ToString(),
interaction.Id.ToString(),
interaction.User.Id.ToString(),
interaction.ChannelId.ToString(),
/* Temporary until mfdlabs/grid-bot#335 is resolved */
interaction.ChannelId?.ToString() ?? "Thread",
GetGuildId(context)
).Inc();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public ILogger CreateLogger(SocketInteraction interaction)
logToFileSystem: false
);

logger.CustomLogPrefixes.Add(() => interaction.ChannelId.ToString());
logger.CustomLogPrefixes.Add(() => interaction.ChannelId?.ToString() ?? "Thread");
logger.CustomLogPrefixes.Add(() => interaction.User.Id.ToString());

// Add guild id if the interaction is from a guild.
Expand All @@ -45,6 +45,7 @@ public ILogger CreateLogger(SocketMessage message)
logger.CustomLogPrefixes.Add(() => message.Author.Id.ToString());

// Add guild id if the message is from a guild.
/* Always false in private thread channels, please look into discord-net/Discord.Net#2997 and mfdlabs/grid-bot#335 */
if (message.Channel is SocketGuildChannel guildChannel)
logger.CustomLogPrefixes.Add(() => guildChannel.Guild.Id.ToString());

Expand Down
4 changes: 2 additions & 2 deletions services/grid-bot/lib/utility/Implementation/ScriptLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex
var username = $"{Environment.MachineName} ({_localIpAddressProvider.AddressV4} / {_localIpAddressProvider.AddressV6})";
var userInfo = context.User.ToString();
var guildInfo = context.Guild?.ToString() ?? "DMs";
var channelInfo = context.Channel?.ToString() ?? "Forum Channel";


/* Temporary until mfdlabs/grid-bot#335 is resolved */
var channelInfo = context.Channel?.ToString() ?? context.Interaction.ChannelId?.ToString() ?? "Thread";

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

0 comments on commit a313599

Please sign in to comment.