Skip to content

Commit

Permalink
Merge pull request #230 from 13xforever/vnext
Browse files Browse the repository at this point in the history
Fix some bugs and update file logging
  • Loading branch information
13xforever authored Feb 24, 2019
2 parents ac41a7a + e2133e9 commit 1813033
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
33 changes: 32 additions & 1 deletion CompatBot/Commands/Sudo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.CommandsNext.Converters;
using DSharpPlus.Entities;

namespace CompatBot.Commands
Expand Down Expand Up @@ -85,5 +86,35 @@ public async Task React(
Config.Log.Debug(e);
}
}

[Command("log"), RequiresDm]
[Description("Uploads current log file as an attachment")]
public async Task Log(CommandContext ctx)
{
try
{
using (var log = File.Open(Config.LogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var result = new MemoryStream((int)Math.Min(Config.AttachmentSizeLimit, log.Length)))
{
using (var gzip = new GZipStream(result, CompressionLevel.Optimal, true))
{
await log.CopyToAsync(gzip, Config.Cts.Token).ConfigureAwait(false);
await gzip.FlushAsync().ConfigureAwait(false);
}
if (result.Length <= Config.AttachmentSizeLimit)
{
result.Seek(0, SeekOrigin.Begin);
await ctx.RespondWithFileAsync(Path.GetFileName(Config.LogPath) + ".gz", result).ConfigureAwait(false);
}
else
await ctx.ReactWithAsync(Config.Reactions.Failure, "Compressed log size is too large, ask Nicba for help :(", true).ConfigureAwait(false);
}
}
catch (Exception e)
{
Config.Log.Warn(e, "Failed to upload current log");
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to send the log", true).ConfigureAwait(false);
}
}
}
}
6 changes: 4 additions & 2 deletions CompatBot/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using DSharpPlus.Entities;
Expand Down Expand Up @@ -32,7 +33,7 @@ internal static class Config
public static readonly TimeSpan BuildTimeDifferenceForOutdatedBuilds = TimeSpan.FromDays(3);

public static readonly string Token;
public static readonly string LogPath = "../../../logs/bot.log"; // paths are relative to the assembly, so this will put it in the project's root
public static readonly string LogPath = "logs/bot.log"; // paths are relative to the assembly, so this will put it in the project's root
public static readonly string IrdCachePath = "./ird/";

internal static readonly ILogger Log;
Expand Down Expand Up @@ -146,13 +147,14 @@ private static ILogger GetLog()
{
var config = new NLog.Config.LoggingConfiguration();
var fileTarget = new FileTarget("logfile") {
FileName = LogPath,
FileName = Path.Combine("../../../", LogPath),
ArchiveEvery = FileArchivePeriod.Day,
ArchiveNumbering = ArchiveNumberingMode.DateAndSequence,
KeepFileOpen = true,
ConcurrentWrites = false,
AutoFlush = false,
OpenFileFlushTimeout = 1,
Layout = "${longdate} ${sequenceid} ${level:uppercase=true} ${message} ${onexception:${newline}${exception:format=tostring}}",
};
var asyncFileTarget = new AsyncTargetWrapper(fileTarget)
{
Expand Down
2 changes: 1 addition & 1 deletion CompatBot/EventHandlers/LogParsingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ await Task.WhenAll(
}
else
botMsg = await botMsg.UpdateOrCreateMessageAsync(channel,
requester == null ? null : $"Reanalyzed log from {client.GetMember(channel.Guild, message.Author).GetUsernameWithNickname()} by request from {requester.Mention}:",
requester == null ? null : $"Analyzed log from {client.GetMember(channel.Guild, message.Author)?.GetUsernameWithNickname()} by request from {requester.Mention}:",
embed: await result.AsEmbedAsync(client, message).ConfigureAwait(false)
).ConfigureAwait(false);
}
Expand Down
2 changes: 1 addition & 1 deletion CompatBot/Utils/DiscordClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static string GetMentionWithNickname(this DiscordMember member)

public static string GetUsernameWithNickname(this DiscordMember member)
{
return string.IsNullOrEmpty(member.Nickname) ? $"`{member.Username.Sanitize()}`" : $"`{member.Username.Sanitize()}` (shown as `{member.Nickname.Sanitize()}`)";
return string.IsNullOrEmpty(member?.Nickname) ? $"`{member?.Username.Sanitize()}`" : $"`{member.Username.Sanitize()}` (shown as `{member.Nickname.Sanitize()}`)";
}

public static DiscordEmoji GetEmoji(this DiscordClient client, string emojiName, DiscordEmoji fallbackEmoji = null)
Expand Down

0 comments on commit 1813033

Please sign in to comment.