updated discord.net, moved command stuff to commandhandler

This commit is contained in:
Kwoth 2016-08-30 00:47:04 +02:00
parent 5dd55bbc0a
commit c2ed70c1ac
3 changed files with 84 additions and 52 deletions

@ -1 +1 @@
Subproject commit e8550fa462f86e0338925547dfe99242bfc1fdcc Subproject commit 7d4f54ee17ae0b5c962036501b46d986c741cb29

View File

@ -19,11 +19,12 @@ namespace NadekoBot
private Logger _log; private Logger _log;
public static CommandService Commands { get; private set; } public static CommandService Commands { get; private set; }
public static CommandHandler CommandHandler { get; private set; }
public static DiscordSocketClient Client { get; private set; } public static DiscordSocketClient Client { get; private set; }
public static Localization Localizer { get; private set; } public static Localization Localizer { get; private set; }
public static BotCredentials Credentials { get; private set; } public static BotCredentials Credentials { get; private set; }
public static GoogleApiService Google { get; set; } public static GoogleApiService Google { get; private set; }
public static StatsService Stats { get; private set; } public static StatsService Stats { get; private set; }
public async Task RunAsync(string[] args) public async Task RunAsync(string[] args)
@ -44,6 +45,7 @@ namespace NadekoBot
Localizer = new Localization(); Localizer = new Localization();
Google = new GoogleApiService(); Google = new GoogleApiService();
Stats = new StatsService(Client); Stats = new StatsService(Client);
CommandHandler = new CommandHandler(Client, Commands);
_log = LogManager.GetCurrentClassLogger(); _log = LogManager.GetCurrentClassLogger();
//setup DI //setup DI
@ -61,7 +63,6 @@ namespace NadekoBot
//load commands //load commands
await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap); await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap);
Client.MessageReceived += Client_MessageReceived;
Console.WriteLine(await Stats.Print()); Console.WriteLine(await Stats.Print());
@ -83,57 +84,10 @@ namespace NadekoBot
LogManager.Configuration = logConfig; LogManager.Configuration = logConfig;
} }
catch (Exception ex) { catch (Exception ex)
{
Console.WriteLine(ex); Console.WriteLine(ex);
} }
} }
private Task Client_MessageReceived(IMessage umsg)
{
var usrMsg = umsg as IUserMessage;
if (usrMsg == null)
return Task.CompletedTask;
var throwaway = Task.Run(async () =>
{
var sw = new Stopwatch();
sw.Start();
var t = await Commands.Execute(usrMsg, usrMsg.Content);
sw.Stop();
var channel = (umsg.Channel as ITextChannel);
if (t.IsSuccess)
{
_log.Info("Command Executed after {4}s\n\t" +
"User: {0}\n\t" +
"Server: {1}\n\t" +
"Channel: {2}\n\t" +
"Message: {3}",
umsg.Author + " [" + umsg.Author.Id + "]", // {0}
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), //{2}
umsg.Content, // {3}
sw.Elapsed.TotalSeconds // {4}
);
}
else if (!t.IsSuccess && t.Error != CommandError.UnknownCommand)
{
_log.Warn("Command Errored after {5}s\n\t" +
"User: {0}\n\t" +
"Server: {1}\n\t" +
"Channel: {2}\n\t" +
"Message: {3}\n\t" +
"Error: {4}",
umsg.Author + " [" + umsg.Author.Id + "]", // {0}
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), //{2}
umsg.Content,// {3}
t.ErrorReason, // {4}
sw.Elapsed.TotalSeconds //{5}
);
}
});
return Task.CompletedTask;
}
} }
} }

View File

@ -0,0 +1,78 @@
using Discord.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using NLog;
using System.Diagnostics;
using Discord.Commands;
namespace NadekoBot.Services
{
public class CommandHandler
{
private DiscordSocketClient _client;
private CommandService _commandService;
private Logger _log;
public CommandHandler(DiscordSocketClient client, CommandService commandService)
{
_client = client;
_commandService = commandService;
_log = LogManager.GetCurrentClassLogger();
_client.MessageReceived += MessageReceivedHandler;
}
private Task MessageReceivedHandler(IMessage msg)
{
var usrMsg = msg as IUserMessage;
if (usrMsg == null)
return Task.CompletedTask;
var throwaway = Task.Run(async () =>
{
var sw = new Stopwatch();
sw.Start();
var t = await _commandService.Execute(usrMsg, usrMsg.Content, MultiMatchHandling.Best);
var command = t.Item1;
var result = t.Item2;
sw.Stop();
var channel = (usrMsg.Channel as ITextChannel);
if (result.IsSuccess)
{
_log.Info("Command Executed after {4}s\n\t" +
"User: {0}\n\t" +
"Server: {1}\n\t" +
"Channel: {2}\n\t" +
"Message: {3}",
usrMsg.Author + " [" + usrMsg.Author.Id + "]", // {0}
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
usrMsg.Content, // {3}
sw.Elapsed.TotalSeconds // {4}
);
}
else if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
{
_log.Warn("Command Errored after {5}s\n\t" +
"User: {0}\n\t" +
"Server: {1}\n\t" +
"Channel: {2}\n\t" +
"Message: {3}\n\t" +
"Error: {4}",
usrMsg.Author + " [" + usrMsg.Author.Id + "]", // {0}
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
usrMsg.Content,// {3}
result.ErrorReason, // {4}
sw.Elapsed.TotalSeconds // {5}
);
}
});
return Task.CompletedTask;
}
}
}