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");
|
||||
totalCheckEmImages = checkEmImageArray.Length;
|
||||
this.rnd = rnd;
|
||||
logger.Information($"-- Loaded {totalCheckEmImages} CheckEm Images");
|
||||
logger.Information($"[Geekbot] [CheckEm] Loaded {totalCheckEmImages} CheckEm Images");
|
||||
}
|
||||
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("%");
|
||||
totalFortunes = fortuneArray.Length;
|
||||
this.rnd = rnd;
|
||||
logger.Information($"-- Loaded {totalFortunes} Fortunes");
|
||||
logger.Information($"[Geekbot] [Fortunes] Loaded {totalFortunes} Fortunes");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Geekbot.net.Lib
|
|||
PandaArray = rawFortunes.Split("\n");
|
||||
totalPandas = PandaArray.Length;
|
||||
this.rnd = rnd;
|
||||
logger.Information($"-- Loaded {totalPandas} Panda Images");
|
||||
logger.Information($"[Geekbot] [Pandas] Loaded {totalPandas} Panda Images");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Serilog;
|
||||
|
||||
namespace Geekbot.net.Modules
|
||||
{
|
||||
|
@ -11,11 +12,15 @@ namespace Geekbot.net.Modules
|
|||
{
|
||||
private readonly ICheckEmImageProvider checkEmImages;
|
||||
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.logger = logger;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
[Command("checkem", RunMode = RunMode.Async)]
|
||||
|
@ -50,7 +55,7 @@ namespace Geekbot.net.Modules
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ using Discord.WebSocket;
|
|||
using Geekbot.net.Lib;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Serilog;
|
||||
using Serilog.Sinks.SystemConsole.Themes;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net
|
||||
|
@ -22,13 +21,14 @@ namespace Geekbot.net
|
|||
private IServiceProvider servicesProvider;
|
||||
private RedisValue token;
|
||||
private ILogger logger;
|
||||
private ulong botOwnerId;
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
var logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Debug()
|
||||
.WriteTo.LiterateConsole()
|
||||
.WriteTo.RollingFile("Logs/geekbot-{Hour}.txt", shared: true)
|
||||
.WriteTo.RollingFile("Logs/geekbot-{Date}.txt", shared: true)
|
||||
.CreateLogger();
|
||||
|
||||
var logo = new StringBuilder();
|
||||
|
@ -39,23 +39,27 @@ namespace Geekbot.net
|
|||
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
||||
logo.AppendLine("=========================================");
|
||||
Console.WriteLine(logo.ToString());
|
||||
logger.Information("* Starting...");
|
||||
logger.Information("[Geekbot] Starting...");
|
||||
new Program().MainAsync(logger).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
private async Task MainAsync(ILogger 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();
|
||||
|
||||
try
|
||||
{
|
||||
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
||||
redis = redisMultiplexer.GetDatabase(6);
|
||||
logger.Information($"-- Connected to Redis ({redis.Database})");
|
||||
logger.Information($"[Redis] Connected to db {redis.Database}");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
@ -70,17 +74,24 @@ namespace Geekbot.net
|
|||
var newToken = Console.ReadLine();
|
||||
redis.StringSet("discordToken", 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();
|
||||
var RandomClient = new Random();
|
||||
var fortunes = new FortunesProvider(RandomClient, logger);
|
||||
var checkEmImages = new CheckEmImageProvider(RandomClient, logger);
|
||||
var pandaImages = new PandaProvider(RandomClient, logger);
|
||||
IErrorHandler errorHandler = new ErrorHandler(logger);
|
||||
services.AddSingleton<IErrorHandler>(errorHandler);
|
||||
services.AddSingleton(redis);
|
||||
services.AddSingleton(RandomClient);
|
||||
services.AddSingleton<ILogger>(logger);
|
||||
|
@ -88,14 +99,14 @@ namespace Geekbot.net
|
|||
services.AddSingleton<ICheckEmImageProvider>(checkEmImages);
|
||||
services.AddSingleton<IPandaProvider>(pandaImages);
|
||||
|
||||
logger.Information("* Connecting to Discord", Color.Teal);
|
||||
logger.Information("[Geekbot] Connecting to Discord");
|
||||
|
||||
await Login();
|
||||
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
||||
public async Task Login()
|
||||
private async Task Login()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -105,18 +116,17 @@ namespace Geekbot.net
|
|||
if (isConneted)
|
||||
{
|
||||
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 += HandleMessageReceived;
|
||||
client.UserJoined += HandleUserJoined;
|
||||
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
||||
services.AddSingleton(commands);
|
||||
servicesProvider = services.BuildServiceProvider();
|
||||
|
||||
logger.Information("* Done and ready for use\n", Color.Teal);
|
||||
this.servicesProvider = services.BuildServiceProvider();
|
||||
logger.Information("[Geekbot] Done and ready for use\n");
|
||||
}
|
||||
}
|
||||
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))
|
||||
await Task.Delay(25);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task HandleCommand(SocketMessage messageParam)
|
||||
private async Task HandleCommand(SocketMessage messageParam)
|
||||
{
|
||||
var message = messageParam as SocketUserMessage;
|
||||
if (message == null) return;
|
||||
|
@ -156,7 +166,7 @@ namespace Geekbot.net
|
|||
var commandExec = commands.ExecuteAsync(context, argPos, servicesProvider);
|
||||
}
|
||||
|
||||
public async Task HandleMessageReceived(SocketMessage messsageParam)
|
||||
private async Task HandleMessageReceived(SocketMessage messsageParam)
|
||||
{
|
||||
var message = messsageParam;
|
||||
if (message == null) return;
|
||||
|
@ -167,13 +177,12 @@ namespace Geekbot.net
|
|||
|
||||
if (message.Author.Id == client.CurrentUser.Id) return;
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
logger.Information(channel.Guild.Name + " - " + message.Channel + " - " + message.Author.Username + " - " +
|
||||
message.Content);
|
||||
logger.Information($"[Message] {channel.Guild.Name} - {message.Channel} - {message.Author.Username} - {message.Content}");
|
||||
await userRec;
|
||||
await guildRec;
|
||||
}
|
||||
|
||||
public async Task HandleUserJoined(SocketGuildUser user)
|
||||
private async Task HandleUserJoined(SocketGuildUser user)
|
||||
{
|
||||
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