Add json information to command error handler

This commit is contained in:
Runebaas 2017-11-14 23:08:36 +01:00
parent b45370cf9e
commit 6cef559601
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -1,6 +1,7 @@
using System;
using Discord.Commands;
using Serilog;
using Utf8Json;
namespace Geekbot.net.Lib
{
@ -15,14 +16,72 @@ namespace Geekbot.net.Lib
public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:")
{
var errorMsg =
$"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\" in \"{Context.Guild.Name}/{Context.Channel.Name}\"";
_logger.Error(e, errorMsg);
if (!string.IsNullOrEmpty(errorMessage))
try
{
Context.Channel.SendMessageAsync(errorMessage);
var errorObj = new ErrorObject()
{
Message = new ErrorMessage()
{
Content = Context.Message.Content,
Id = Context.Message.Id.ToString(),
Attachments = Context.Message.Attachments.Count,
ChannelMentions = Context.Message.MentionedChannelIds.Count,
UserMentions = Context.Message.MentionedUserIds.Count,
RoleMentions = Context.Message.MentionedRoleIds.Count
},
User = new IdAndName()
{
Id = Context.User.Id.ToString(),
Name = $"{Context.User.Username}#{Context.User.Discriminator}"
},
Guild = new IdAndName()
{
Id = Context.Guild.Id.ToString(),
Name = Context.Guild.Name
},
Channel = new IdAndName()
{
Id = Context.Channel.Id.ToString(),
Name = Context.Channel.Name
},
TimeStamp = DateTime.Now.ToString()
};
var errorJson = JsonSerializer.ToJsonString(errorObj);
_logger.Error(e, errorJson);
if (!string.IsNullOrEmpty(errorMessage))
{
Context.Channel.SendMessageAsync(errorMessage);
}
}
catch (Exception ex)
{
_logger.Error(ex, "Errorception");
}
}
public class ErrorObject
{
public ErrorMessage Message { get; set; }
public IdAndName User { get; set; }
public IdAndName Guild { get; set; }
public IdAndName Channel { get; set; }
public string TimeStamp { get; set; }
}
public class ErrorMessage
{
public string Content { get; set; }
public string Id { get; set; }
public int Attachments { get; set; }
public int ChannelMentions { get; set; }
public int UserMentions { get; set; }
public int RoleMentions { get; set; }
}
public class IdAndName
{
public string Id { get; set; }
public string Name { get; set; }
}
}