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
a9cca3ee59
commit
1159af1785
@ -1,4 +1,5 @@
|
||||
using JetKarmaBot.Commands;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@ -10,6 +11,7 @@ namespace JetKarmaBot
|
||||
class ChatCommandRouter
|
||||
{
|
||||
User BotUser { get; }
|
||||
private static Logger log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public ChatCommandRouter(User botUser)
|
||||
{
|
||||
@ -18,21 +20,29 @@ namespace JetKarmaBot
|
||||
|
||||
public bool Execute(object sender, MessageEventArgs args)
|
||||
{
|
||||
log.Debug("Message received");
|
||||
var text = args.Message.Text;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (commands.ContainsKey(cmd.Command))
|
||||
{
|
||||
log.Debug($"Handling message via {commands[cmd.Command].GetType().Name}");
|
||||
return commands[cmd.Command].Execute(cmd, args);
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
log.ConditionalTrace($"Adding command {c.GetType().Name}");
|
||||
foreach (var name in c.Names)
|
||||
{
|
||||
log.ConditionalTrace($"Mounting {c.GetType().Name} to {name}");
|
||||
if (commands.ContainsKey(name))
|
||||
throw new Exception($"command collision for name {name}, commands {commands[name].GetType()} and {c.GetType()}");
|
||||
commands[name] = c;
|
||||
|
||||
@ -2,12 +2,14 @@
|
||||
using System.Threading;
|
||||
using JetKarmaBot.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NLog;
|
||||
using Perfusion;
|
||||
|
||||
namespace JetKarmaBot
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
private static Logger log = LogManager.GetCurrentClassLogger();
|
||||
public enum ExitCode : int
|
||||
{
|
||||
Ok = 0,
|
||||
@ -17,13 +19,9 @@ namespace JetKarmaBot
|
||||
ErrorInvalidCommandLine = 0x100
|
||||
};
|
||||
|
||||
#if DEBUG
|
||||
public const bool Debug = true;
|
||||
#else
|
||||
public const bool Debug = false;
|
||||
#endif
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
log.Info("Starting JetKarmaBot.");
|
||||
Container c = new Container();
|
||||
var cfg = new Config("karma.cfg.json");
|
||||
c.AddInstance(cfg);
|
||||
@ -39,12 +37,12 @@ namespace JetKarmaBot
|
||||
try
|
||||
{
|
||||
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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Exception: {ex.Message}");
|
||||
log.Error(ex);
|
||||
Environment.ExitCode = (int)ExitCode.ErrorException;
|
||||
}
|
||||
ManualResetEvent quitEvent = new ManualResetEvent(false);
|
||||
@ -59,7 +57,7 @@ namespace JetKarmaBot
|
||||
catch { }
|
||||
|
||||
quitEvent.WaitOne(Timeout.Infinite);
|
||||
Console.WriteLine("Waiting for exit...");
|
||||
log.Info("Waiting for exit...");
|
||||
bot?.Stop()?.Wait();
|
||||
|
||||
return (int)ExitCode.Ok;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user