Skip to content

Commit a313599

Browse files
committed
Implement channel patches
#!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.
1 parent 9bc014a commit a313599

File tree

6 files changed

+16
-18
lines changed

6 files changed

+16
-18
lines changed

services/grid-bot/lib/commands/Modules/ExecuteScript.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,9 @@ private async Task AlertForSystem(string script, string originalScript, string s
494494

495495
var userInfo = Context.User.ToString();
496496
var guildInfo = Context.Guild?.ToString() ?? "DMs";
497-
var channelInfo = Context.Channel.ToString();
497+
498+
/* Temporary until mfdlabs/grid-bot#335 is resolved */
499+
var channelInfo = Context.Channel?.ToString() ?? Context.Interaction.ChannelId?.ToString() ?? "Thread";
498500

499501
// Script & original script in attachments
500502
var scriptAttachment = new FileAttachment(new MemoryStream(Encoding.ASCII.GetBytes(script)), "script.lua");

services/grid-bot/lib/events/Events/OnMessage.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public async Task Invoke(SocketMessage rawMessage)
173173

174174
_totalUsersBypassedMaintenance.WithLabels(
175175
message.Author.Id.ToString(),
176-
message.Channel.Id.ToString(),
176+
message.Channel?.Id.ToString() ?? message.Thread?.Id.ToString(),
177177
GetGuildId(message)
178178
).Inc();
179179
}
@@ -182,7 +182,7 @@ public async Task Invoke(SocketMessage rawMessage)
182182
{
183183
_totalBlacklistedUserAttemptedMessages.WithLabels(
184184
message.Author.Id.ToString(),
185-
message.Channel.Id.ToString(),
185+
message.Channel?.Id.ToString() ?? message.Thread?.Id.ToString(),
186186
GetGuildId(message)
187187
).Inc();
188188

services/grid-bot/lib/events/Events/OnSlashCommand.cs

+5-11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ ILoggerFactory loggerFactory
8989

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

@@ -123,15 +124,6 @@ public async Task Invoke(SocketInteraction interaction)
123124
interaction.Type.ToString()
124125
).Inc();
125126

126-
var guildName = string.Empty;
127-
var guildId = 0UL;
128-
129-
if (interaction.Channel is SocketGuildChannel guildChannel)
130-
{
131-
guildName = guildChannel.Guild.Name;
132-
guildId = guildChannel.Guild.Id;
133-
}
134-
135127
logger.Warning("Maintenance enabled user tried to use the bot.");
136128

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

158150
_totalUsersBypassedMaintenance.WithLabels(
159151
interaction.User.Id.ToString(),
160-
interaction.Channel.Id.ToString(),
152+
/* Temporary until mfdlabs/grid-bot#335 is resolved */
153+
interaction.Channel?.Id.ToString() ?? interaction.ChannelId?.ToString() ?? "Thread",
161154
GetGuildId(interaction)
162155
).Inc();
163156
}
@@ -166,7 +159,8 @@ public async Task Invoke(SocketInteraction interaction)
166159
{
167160
_totalBlacklistedUserAttemptedInteractions.WithLabels(
168161
interaction.User.Id.ToString(),
169-
interaction.Channel.Id.ToString(),
162+
/* Temporary until mfdlabs/grid-bot#335 is resolved */
163+
interaction.Channel?.Id.ToString() ?? interaction.ChannelId?.ToString() ?? "Thread",
170164
GetGuildId(interaction)
171165
).Inc();
172166

services/grid-bot/lib/events/Events/OnSlashCommandExecuted.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ public async Task Invoke(ICommandInfo command, IInteractionContext context, IRes
7474
interaction.Type.ToString(),
7575
interaction.Id.ToString(),
7676
interaction.User.Id.ToString(),
77-
interaction.ChannelId.ToString(),
77+
/* Temporary until mfdlabs/grid-bot#335 is resolved */
78+
interaction.ChannelId?.ToString() ?? "Thread",
7879
GetGuildId(context)
7980
).Inc();
8081

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public ILogger CreateLogger(SocketInteraction interaction)
2121
logToFileSystem: false
2222
);
2323

24-
logger.CustomLogPrefixes.Add(() => interaction.ChannelId.ToString());
24+
logger.CustomLogPrefixes.Add(() => interaction.ChannelId?.ToString() ?? "Thread");
2525
logger.CustomLogPrefixes.Add(() => interaction.User.Id.ToString());
2626

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

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public async Task LogScriptAsync(string script, ShardedInteractionContext contex
8484
var username = $"{Environment.MachineName} ({_localIpAddressProvider.AddressV4} / {_localIpAddressProvider.AddressV6})";
8585
var userInfo = context.User.ToString();
8686
var guildInfo = context.Guild?.ToString() ?? "DMs";
87-
var channelInfo = context.Channel?.ToString() ?? "Forum Channel";
88-
8987

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

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

0 commit comments

Comments
 (0)