Add More logging Statements
This commit is contained in:
parent
79e52774b8
commit
2a12d30d8f
6 changed files with 106 additions and 34 deletions
|
@ -19,7 +19,7 @@ namespace Geekbot.net.Lib
|
||||||
checkEmImageArray = rawCheckEmPics.Split("\n");
|
checkEmImageArray = rawCheckEmPics.Split("\n");
|
||||||
totalCheckEmImages = checkEmImageArray.Length;
|
totalCheckEmImages = checkEmImageArray.Length;
|
||||||
this.rnd = rnd;
|
this.rnd = rnd;
|
||||||
logger.Information($"-- Loaded {totalCheckEmImages} CheckEm Images");
|
logger.Information($"[Geekbot] [CheckEm] Loaded {totalCheckEmImages} CheckEm Images");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
32
Geekbot.net/Lib/ErrorHandler.cs
Normal file
32
Geekbot.net/Lib/ErrorHandler.cs
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
using System;
|
||||||
|
using Discord.Commands;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
|
namespace Geekbot.net.Lib
|
||||||
|
{
|
||||||
|
public class ErrorHandler : IErrorHandler
|
||||||
|
{
|
||||||
|
private readonly ILogger logger;
|
||||||
|
// private readonly IDMChannel botOwnerDmChannel;
|
||||||
|
|
||||||
|
public ErrorHandler(ILogger logger /*, IDMChannel botOwnerDmChannel*/)
|
||||||
|
{
|
||||||
|
this.logger = logger;
|
||||||
|
// this.botOwnerDmChannel = botOwnerDmChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async void HandleCommandException(Exception e, ICommandContext Context)
|
||||||
|
{
|
||||||
|
var errorMsg =
|
||||||
|
$"Error Occured while executing \"{Context.Message.Content}\", executed by \"{Context.User.Username}\"";
|
||||||
|
logger.Error(e, errorMsg);
|
||||||
|
// await botOwnerDmChannel.SendMessageAsync($"{errorMsg}```{e.StackTrace}```");
|
||||||
|
// await Context.Channel.SendMessageAsync("Something went wrong...");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IErrorHandler
|
||||||
|
{
|
||||||
|
void HandleCommandException(Exception e, ICommandContext Context);
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,7 +19,7 @@ namespace Geekbot.net.Lib
|
||||||
fortuneArray = rawFortunes.Split("%");
|
fortuneArray = rawFortunes.Split("%");
|
||||||
totalFortunes = fortuneArray.Length;
|
totalFortunes = fortuneArray.Length;
|
||||||
this.rnd = rnd;
|
this.rnd = rnd;
|
||||||
logger.Information($"-- Loaded {totalFortunes} Fortunes");
|
logger.Information($"[Geekbot] [Fortunes] Loaded {totalFortunes} Fortunes");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace Geekbot.net.Lib
|
||||||
PandaArray = rawFortunes.Split("\n");
|
PandaArray = rawFortunes.Split("\n");
|
||||||
totalPandas = PandaArray.Length;
|
totalPandas = PandaArray.Length;
|
||||||
this.rnd = rnd;
|
this.rnd = rnd;
|
||||||
logger.Information($"-- Loaded {totalPandas} Panda Images");
|
logger.Information($"[Geekbot] [Pandas] Loaded {totalPandas} Panda Images");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Geekbot.net.Modules
|
namespace Geekbot.net.Modules
|
||||||
{
|
{
|
||||||
|
@ -11,11 +12,15 @@ namespace Geekbot.net.Modules
|
||||||
{
|
{
|
||||||
private readonly ICheckEmImageProvider checkEmImages;
|
private readonly ICheckEmImageProvider checkEmImages;
|
||||||
private readonly Random rnd;
|
private readonly Random rnd;
|
||||||
|
private readonly ILogger logger;
|
||||||
|
private readonly IErrorHandler errorHandler;
|
||||||
|
|
||||||
public CheckEm(Random RandomClient, ICheckEmImageProvider checkEmImages)
|
public CheckEm(Random RandomClient, ICheckEmImageProvider checkEmImages, ILogger logger, IErrorHandler errorHandler)
|
||||||
{
|
{
|
||||||
rnd = RandomClient;
|
this.rnd = RandomClient;
|
||||||
this.checkEmImages = checkEmImages;
|
this.checkEmImages = checkEmImages;
|
||||||
|
this.logger = logger;
|
||||||
|
this.errorHandler = errorHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command("checkem", RunMode = RunMode.Async)]
|
[Command("checkem", RunMode = RunMode.Async)]
|
||||||
|
@ -50,7 +55,7 @@ namespace Geekbot.net.Modules
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e.Message);
|
errorHandler.HandleCommandException(e, Context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ using Discord.WebSocket;
|
||||||
using Geekbot.net.Lib;
|
using Geekbot.net.Lib;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Sinks.SystemConsole.Themes;
|
|
||||||
using StackExchange.Redis;
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Geekbot.net
|
namespace Geekbot.net
|
||||||
|
@ -22,16 +21,17 @@ namespace Geekbot.net
|
||||||
private IServiceProvider servicesProvider;
|
private IServiceProvider servicesProvider;
|
||||||
private RedisValue token;
|
private RedisValue token;
|
||||||
private ILogger logger;
|
private ILogger logger;
|
||||||
|
private ulong botOwnerId;
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var logger = new LoggerConfiguration()
|
var logger = new LoggerConfiguration()
|
||||||
.MinimumLevel.Debug()
|
.MinimumLevel.Debug()
|
||||||
.WriteTo.LiterateConsole()
|
.WriteTo.LiterateConsole()
|
||||||
.WriteTo.RollingFile("Logs/geekbot-{Hour}.txt", shared: true)
|
.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true)
|
||||||
.CreateLogger();
|
.CreateLogger();
|
||||||
|
|
||||||
var logo = new StringBuilder();
|
var logo = new StringBuilder();
|
||||||
logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____");
|
logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____");
|
||||||
logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
|
logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
|
||||||
logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |");
|
logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |");
|
||||||
|
@ -39,23 +39,27 @@ namespace Geekbot.net
|
||||||
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
||||||
logo.AppendLine("=========================================");
|
logo.AppendLine("=========================================");
|
||||||
Console.WriteLine(logo.ToString());
|
Console.WriteLine(logo.ToString());
|
||||||
logger.Information("* Starting...");
|
logger.Information("[Geekbot] Starting...");
|
||||||
new Program().MainAsync(logger).GetAwaiter().GetResult();
|
new Program().MainAsync(logger).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task MainAsync(ILogger logger)
|
private async Task MainAsync(ILogger logger)
|
||||||
{
|
{
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
logger.Information("* Initing Stuff");
|
logger.Information("[Geekbot] Initing Stuff");
|
||||||
|
|
||||||
client = new DiscordSocketClient();
|
client = new DiscordSocketClient(new DiscordSocketConfig
|
||||||
|
{
|
||||||
|
LogLevel = LogSeverity.Verbose
|
||||||
|
});
|
||||||
|
client.Log += DiscordLogger;
|
||||||
commands = new CommandService();
|
commands = new CommandService();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
||||||
redis = redisMultiplexer.GetDatabase(6);
|
redis = redisMultiplexer.GetDatabase(6);
|
||||||
logger.Information($"-- Connected to Redis ({redis.Database})");
|
logger.Information($"[Redis] Connected to db {redis.Database}");
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
@ -70,17 +74,24 @@ namespace Geekbot.net
|
||||||
var newToken = Console.ReadLine();
|
var newToken = Console.ReadLine();
|
||||||
redis.StringSet("discordToken", newToken);
|
redis.StringSet("discordToken", newToken);
|
||||||
token = newToken;
|
token = newToken;
|
||||||
|
|
||||||
Console.Write("Bot Owner User ID: ");
|
|
||||||
var ownerId = Console.ReadLine();
|
|
||||||
redis.StringSet("botOwner", ownerId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var botOwner = redis.StringGet("botOwner");
|
||||||
|
if (botOwner.IsNullOrEmpty)
|
||||||
|
{
|
||||||
|
Console.Write("Bot Owner User ID: ");
|
||||||
|
botOwner = Console.ReadLine();
|
||||||
|
redis.StringSet("botOwner", botOwner);
|
||||||
|
}
|
||||||
|
botOwnerId = (ulong) botOwner;
|
||||||
|
|
||||||
services = new ServiceCollection();
|
services = new ServiceCollection();
|
||||||
var RandomClient = new Random();
|
var RandomClient = new Random();
|
||||||
var fortunes = new FortunesProvider(RandomClient, logger);
|
var fortunes = new FortunesProvider(RandomClient, logger);
|
||||||
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
||||||
var pandaImages = new PandaProvider(RandomClient, logger);
|
var pandaImages = new PandaProvider(RandomClient, logger);
|
||||||
|
IErrorHandler errorHandler = new ErrorHandler(logger);
|
||||||
|
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||||
services.AddSingleton(redis);
|
services.AddSingleton(redis);
|
||||||
services.AddSingleton(RandomClient);
|
services.AddSingleton(RandomClient);
|
||||||
services.AddSingleton<ILogger>(logger);
|
services.AddSingleton<ILogger>(logger);
|
||||||
|
@ -88,14 +99,14 @@ namespace Geekbot.net
|
||||||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||||
services.AddSingleton<IPandaProvider>(pandaImages);
|
services.AddSingleton<IPandaProvider>(pandaImages);
|
||||||
|
|
||||||
logger.Information("* Connecting to Discord", Color.Teal);
|
logger.Information("[Geekbot] Connecting to Discord");
|
||||||
|
|
||||||
await Login();
|
await Login();
|
||||||
|
|
||||||
await Task.Delay(-1);
|
await Task.Delay(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Login()
|
private async Task Login()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -105,18 +116,17 @@ namespace Geekbot.net
|
||||||
if (isConneted)
|
if (isConneted)
|
||||||
{
|
{
|
||||||
await client.SetGameAsync("Ping Pong");
|
await client.SetGameAsync("Ping Pong");
|
||||||
logger.Information($"* Now Connected to {client.Guilds.Count} Servers");
|
logger.Information($"[Geekbot] Now Connected to {client.Guilds.Count} Servers");
|
||||||
|
|
||||||
logger.Information("* Registering Stuff", Color.Teal);
|
logger.Information("[Geekbot] Registering Stuff");
|
||||||
|
|
||||||
client.MessageReceived += HandleCommand;
|
client.MessageReceived += HandleCommand;
|
||||||
client.MessageReceived += HandleMessageReceived;
|
client.MessageReceived += HandleMessageReceived;
|
||||||
client.UserJoined += HandleUserJoined;
|
client.UserJoined += HandleUserJoined;
|
||||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||||
services.AddSingleton(commands);
|
services.AddSingleton(commands);
|
||||||
servicesProvider = services.BuildServiceProvider();
|
this.servicesProvider = services.BuildServiceProvider();
|
||||||
|
logger.Information("[Geekbot] Done and ready for use\n");
|
||||||
logger.Information("* Done and ready for use\n", Color.Teal);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (AggregateException)
|
catch (AggregateException)
|
||||||
|
@ -126,14 +136,14 @@ namespace Geekbot.net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> isConnected()
|
private async Task<bool> isConnected()
|
||||||
{
|
{
|
||||||
while (!client.ConnectionState.Equals(ConnectionState.Connected))
|
while (!client.ConnectionState.Equals(ConnectionState.Connected))
|
||||||
await Task.Delay(25);
|
await Task.Delay(25);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task HandleCommand(SocketMessage messageParam)
|
private async Task HandleCommand(SocketMessage messageParam)
|
||||||
{
|
{
|
||||||
var message = messageParam as SocketUserMessage;
|
var message = messageParam as SocketUserMessage;
|
||||||
if (message == null) return;
|
if (message == null) return;
|
||||||
|
@ -156,24 +166,23 @@ namespace Geekbot.net
|
||||||
var commandExec = commands.ExecuteAsync(context, argPos, servicesProvider);
|
var commandExec = commands.ExecuteAsync(context, argPos, servicesProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task HandleMessageReceived(SocketMessage messsageParam)
|
private async Task HandleMessageReceived(SocketMessage messsageParam)
|
||||||
{
|
{
|
||||||
var message = messsageParam;
|
var message = messsageParam;
|
||||||
if (message == null) return;
|
if (message == null) return;
|
||||||
|
|
||||||
var statsRecorder = new StatsRecorder(message, redis);
|
var statsRecorder = new StatsRecorder(message, redis);
|
||||||
var userRec = statsRecorder.UpdateUserRecordAsync();
|
var userRec = statsRecorder.UpdateUserRecordAsync();
|
||||||
var guildRec = statsRecorder.UpdateGuildRecordAsync();
|
var guildRec = statsRecorder.UpdateGuildRecordAsync();
|
||||||
|
|
||||||
if (message.Author.Id == client.CurrentUser.Id) return;
|
if (message.Author.Id == client.CurrentUser.Id) return;
|
||||||
var channel = (SocketGuildChannel) message.Channel;
|
var channel = (SocketGuildChannel) message.Channel;
|
||||||
logger.Information(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " +
|
logger.Information($"[Message] {channel.Guild.Name} - {message.Channel} - {message.Author.Username} - {message.Content}");
|
||||||
message.Content);
|
|
||||||
await userRec;
|
await userRec;
|
||||||
await guildRec;
|
await guildRec;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task HandleUserJoined(SocketGuildUser user)
|
private async Task HandleUserJoined(SocketGuildUser user)
|
||||||
{
|
{
|
||||||
if (!user.IsBot)
|
if (!user.IsBot)
|
||||||
{
|
{
|
||||||
|
@ -185,5 +194,31 @@ namespace Geekbot.net
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Task DiscordLogger(LogMessage message)
|
||||||
|
{
|
||||||
|
var logMessage = $"[{message.Source}] {message.Message}";
|
||||||
|
switch (message.Severity)
|
||||||
|
{
|
||||||
|
case LogSeverity.Verbose:
|
||||||
|
logger.Verbose(logMessage);
|
||||||
|
break;
|
||||||
|
case LogSeverity.Debug:
|
||||||
|
logger.Debug(logMessage);
|
||||||
|
break;
|
||||||
|
case LogSeverity.Info:
|
||||||
|
logger.Information(logMessage);
|
||||||
|
break;
|
||||||
|
case LogSeverity.Critical:
|
||||||
|
case LogSeverity.Error:
|
||||||
|
case LogSeverity.Warning:
|
||||||
|
logger.Error(message.Exception, logMessage);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.Information($"{logMessage} --- {message.Severity.ToString()}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue