mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 09:06:09 +03:00
Add logging to Program and CommandRouter
This commit is contained in:
parent
66190a1eb0
commit
cc82c02e44
@ -1,4 +1,5 @@
|
|||||||
using JetKarmaBot.Commands;
|
using JetKarmaBot.Commands;
|
||||||
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -10,6 +11,7 @@ namespace JetKarmaBot
|
|||||||
class ChatCommandRouter
|
class ChatCommandRouter
|
||||||
{
|
{
|
||||||
User BotUser { get; }
|
User BotUser { get; }
|
||||||
|
private static Logger log = LogManager.GetCurrentClassLogger();
|
||||||
|
|
||||||
public ChatCommandRouter(User botUser)
|
public ChatCommandRouter(User botUser)
|
||||||
{
|
{
|
||||||
@ -18,21 +20,29 @@ namespace JetKarmaBot
|
|||||||
|
|
||||||
public bool Execute(object sender, MessageEventArgs args)
|
public bool Execute(object sender, MessageEventArgs args)
|
||||||
{
|
{
|
||||||
|
log.Debug("Message received");
|
||||||
var text = args.Message.Text;
|
var text = args.Message.Text;
|
||||||
|
|
||||||
if (CommandString.TryParse(text, out var cmd))
|
if (CommandString.TryParse(text, out var cmd))
|
||||||
{
|
{
|
||||||
if (cmd.UserName != null && cmd.UserName != BotUser.Username) // directed not at us!
|
if (cmd.UserName != null && cmd.UserName != BotUser.Username)
|
||||||
|
{
|
||||||
|
// directed not at us!
|
||||||
|
log.Debug("Message not directed at us");
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (commands.ContainsKey(cmd.Command))
|
if (commands.ContainsKey(cmd.Command))
|
||||||
|
{
|
||||||
|
log.Debug($"Handling message via {commands[cmd.Command].GetType().Name}");
|
||||||
return commands[cmd.Command].Execute(cmd, args);
|
return commands[cmd.Command].Execute(cmd, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Console.WriteLine(e);
|
log.Error($"Error while handling command {cmd.Command}!");
|
||||||
|
log.Error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,8 +52,10 @@ namespace JetKarmaBot
|
|||||||
|
|
||||||
public void Add(IChatCommand c)
|
public void Add(IChatCommand c)
|
||||||
{
|
{
|
||||||
|
log.ConditionalTrace($"Adding command {c.GetType().Name}");
|
||||||
foreach (var name in c.Names)
|
foreach (var name in c.Names)
|
||||||
{
|
{
|
||||||
|
log.ConditionalTrace($"Mounting {c.GetType().Name} to {name}");
|
||||||
if (commands.ContainsKey(name))
|
if (commands.ContainsKey(name))
|
||||||
throw new Exception($"command collision for name {name}, commands {commands[name].GetType()} and {c.GetType()}");
|
throw new Exception($"command collision for name {name}, commands {commands[name].GetType()} and {c.GetType()}");
|
||||||
commands[name] = c;
|
commands[name] = c;
|
||||||
|
|||||||
@ -2,12 +2,14 @@
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using JetKarmaBot.Models;
|
using JetKarmaBot.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NLog;
|
||||||
using Perfusion;
|
using Perfusion;
|
||||||
|
|
||||||
namespace JetKarmaBot
|
namespace JetKarmaBot
|
||||||
{
|
{
|
||||||
public static class Program
|
public static class Program
|
||||||
{
|
{
|
||||||
|
private static Logger log = LogManager.GetCurrentClassLogger();
|
||||||
public enum ExitCode : int
|
public enum ExitCode : int
|
||||||
{
|
{
|
||||||
Ok = 0,
|
Ok = 0,
|
||||||
@ -17,13 +19,9 @@ namespace JetKarmaBot
|
|||||||
ErrorInvalidCommandLine = 0x100
|
ErrorInvalidCommandLine = 0x100
|
||||||
};
|
};
|
||||||
|
|
||||||
#if DEBUG
|
|
||||||
public const bool Debug = true;
|
|
||||||
#else
|
|
||||||
public const bool Debug = false;
|
|
||||||
#endif
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
{
|
{
|
||||||
|
log.Info("Starting JetKarmaBot.");
|
||||||
Container c = new Container();
|
Container c = new Container();
|
||||||
var cfg = new Config("karma.cfg.json");
|
var cfg = new Config("karma.cfg.json");
|
||||||
c.AddInstance(cfg);
|
c.AddInstance(cfg);
|
||||||
@ -39,12 +37,12 @@ namespace JetKarmaBot
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
bot.Init().Wait();
|
bot.Init().Wait();
|
||||||
Console.WriteLine("JetKarmaBot started. Press Ctrl-C to exit...");
|
log.Info("JetKarmaBot started. Press Ctrl-C to exit...");
|
||||||
Environment.ExitCode = (int)ExitCode.ErrorRunning;
|
Environment.ExitCode = (int)ExitCode.ErrorRunning;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Exception: {ex.Message}");
|
log.Error(ex);
|
||||||
Environment.ExitCode = (int)ExitCode.ErrorException;
|
Environment.ExitCode = (int)ExitCode.ErrorException;
|
||||||
}
|
}
|
||||||
ManualResetEvent quitEvent = new ManualResetEvent(false);
|
ManualResetEvent quitEvent = new ManualResetEvent(false);
|
||||||
@ -59,7 +57,7 @@ namespace JetKarmaBot
|
|||||||
catch { }
|
catch { }
|
||||||
|
|
||||||
quitEvent.WaitOne(Timeout.Infinite);
|
quitEvent.WaitOne(Timeout.Infinite);
|
||||||
Console.WriteLine("Waiting for exit...");
|
log.Info("Waiting for exit...");
|
||||||
bot?.Stop()?.Wait();
|
bot?.Stop()?.Wait();
|
||||||
|
|
||||||
return (int)ExitCode.Ok;
|
return (int)ExitCode.Ok;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user