NadekoBot/src/NadekoBot/NadekoBot.cs

94 lines
2.8 KiB
C#
Raw Normal View History

2016-08-15 14:57:40 +00:00
using Discord;
using Discord.Commands;
2016-08-13 18:45:08 +00:00
using Discord.WebSocket;
2016-08-15 14:57:40 +00:00
using NadekoBot.Services;
using NadekoBot.Services.Impl;
2016-08-18 21:00:54 +00:00
using NLog;
using NLog.Config;
using NLog.Targets;
2016-08-13 18:45:08 +00:00
using System;
2016-08-25 15:05:24 +00:00
using System.Diagnostics;
2016-08-13 18:45:08 +00:00
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace NadekoBot
{
public class NadekoBot
{
2016-08-18 21:00:54 +00:00
private Logger _log;
2016-08-13 18:45:08 +00:00
public static CommandService Commands { get; private set; }
public static CommandHandler CommandHandler { get; private set; }
2016-08-13 18:45:08 +00:00
public static DiscordSocketClient Client { get; private set; }
2016-08-15 23:38:28 +00:00
public static Localization Localizer { get; private set; }
public static BotCredentials Credentials { get; private set; }
2016-08-13 18:45:08 +00:00
public static GoogleApiService Google { get; private set; }
2016-08-18 21:00:54 +00:00
public static StatsService Stats { get; private set; }
2016-08-13 18:45:08 +00:00
public async Task RunAsync(string[] args)
{
2016-08-18 21:00:54 +00:00
SetupLogger();
2016-08-15 14:57:40 +00:00
//create client
Client = new DiscordSocketClient(new DiscordSocketConfig
2016-08-13 18:45:08 +00:00
{
2016-08-26 01:25:55 +00:00
AudioMode = Discord.Audio.AudioMode.Outgoing,
2016-08-13 18:45:08 +00:00
LargeThreshold = 200,
2016-08-15 14:57:40 +00:00
LogLevel = LogSeverity.Warning,
2016-08-13 18:45:08 +00:00
});
2016-08-15 14:57:40 +00:00
//initialize Services
Credentials = new BotCredentials();
2016-08-13 18:45:08 +00:00
Commands = new CommandService();
2016-08-15 14:57:40 +00:00
Localizer = new Localization();
Google = new GoogleApiService();
2016-08-18 21:00:54 +00:00
Stats = new StatsService(Client);
CommandHandler = new CommandHandler(Client, Commands);
2016-08-18 21:00:54 +00:00
_log = LogManager.GetCurrentClassLogger();
2016-08-15 14:57:40 +00:00
//setup DI
var depMap = new DependencyMap();
depMap.Add<ILocalization>(Localizer);
depMap.Add<DiscordSocketClient>(Client);
2016-08-15 14:57:40 +00:00
depMap.Add<CommandService>(Commands);
depMap.Add<IGoogleApiService>(Google);
2016-08-13 18:45:08 +00:00
2016-08-15 14:57:40 +00:00
//connect
2016-08-18 23:28:26 +00:00
await Client.LoginAsync(TokenType.Bot, Credentials.Token);
2016-08-13 18:45:08 +00:00
await Client.ConnectAsync();
2016-08-18 21:00:54 +00:00
_log.Info("Connected");
2016-08-15 14:57:40 +00:00
//load commands
await Commands.LoadAssembly(Assembly.GetEntryAssembly(), depMap);
2016-08-13 18:45:08 +00:00
2016-08-18 21:00:54 +00:00
Console.WriteLine(await Stats.Print());
2016-08-13 18:45:08 +00:00
await Task.Delay(-1);
}
2016-08-15 14:57:40 +00:00
2016-08-18 21:00:54 +00:00
private void SetupLogger()
{
try
{
var logConfig = new LoggingConfiguration();
var consoleTarget = new ColoredConsoleTarget();
consoleTarget.Layout = @"${date:format=HH\:mm\:ss} ${logger} | ${message}";
logConfig.AddTarget("Console", consoleTarget);
logConfig.LoggingRules.Add(new LoggingRule("*", LogLevel.Debug, consoleTarget));
LogManager.Configuration = logConfig;
}
catch (Exception ex)
{
2016-08-18 21:00:54 +00:00
Console.WriteLine(ex);
}
}
2016-08-13 18:45:08 +00:00
}
}