NadekoBot/src/NadekoBot/NadekoBot.cs

124 lines
4.4 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.Database;
2016-08-15 14:57:40 +00:00
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;
2016-09-15 15:31:02 +00:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using NadekoBot.Modules.Permissions;
using Module = Discord.Commands.Module;
using NadekoBot.TypeReaders;
2016-10-12 02:47:28 +00:00
using System.Collections.Concurrent;
2016-08-13 18:45:08 +00:00
namespace NadekoBot
{
public class NadekoBot
{
2016-08-18 21:00:54 +00:00
private Logger _log;
public static CommandService CommandService { get; private set; }
public static CommandHandler CommandHandler { get; private set; }
public static ShardedDiscordClient 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-10-12 02:47:28 +00:00
public static ConcurrentDictionary<string, string> ModulePrefixes { get; private set; }
2016-09-15 15:31:02 +00:00
2016-08-13 18:45:08 +00:00
public async Task RunAsync(string[] args)
{
2016-08-18 21:00:54 +00:00
SetupLogger();
_log = LogManager.GetCurrentClassLogger();
_log.Info("Starting NadekoBot v" + typeof(NadekoBot).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
2016-08-18 21:00:54 +00:00
Credentials = new BotCredentials();
2016-08-15 14:57:40 +00:00
//create client
Client = new ShardedDiscordClient (new DiscordSocketConfig
2016-08-13 18:45:08 +00:00
{
2016-08-26 01:25:55 +00:00
AudioMode = Discord.Audio.AudioMode.Outgoing,
2016-09-06 21:34:00 +00:00
MessageCacheSize = 10,
2016-08-15 14:57:40 +00:00
LogLevel = LogSeverity.Warning,
TotalShards = Credentials.TotalShards,
ConnectionTimeout = 60000
2016-08-13 18:45:08 +00:00
});
2016-08-15 14:57:40 +00:00
//initialize Services
CommandService = new CommandService();
2016-08-15 14:57:40 +00:00
Localizer = new Localization();
Google = new GoogleApiService();
CommandHandler = new CommandHandler(Client, CommandService);
2016-09-08 22:22:55 +00:00
Stats = new StatsService(Client, CommandHandler);
2016-08-18 21:00:54 +00:00
2016-08-15 14:57:40 +00:00
//setup DI
var depMap = new DependencyMap();
depMap.Add<ILocalization>(Localizer);
depMap.Add<ShardedDiscordClient >(Client);
depMap.Add<CommandService>(CommandService);
depMap.Add<IGoogleApiService>(Google);
2016-08-13 18:45:08 +00:00
//setup typereaders
CommandService.AddTypeReader<PermissionAction>(new PermissionActionTypeReader());
CommandService.AddTypeReader<Command>(new CommandTypeReader());
CommandService.AddTypeReader<Module>(new ModuleTypeReader());
CommandService.AddTypeReader<IGuild>(new GuildTypeReader());
2016-08-15 14:57:40 +00:00
//connect
2016-09-06 01:59:00 +00:00
await Client.LoginAsync(TokenType.Bot, Credentials.Token).ConfigureAwait(false);
await Client.ConnectAsync().ConfigureAwait(false);
await Client.DownloadAllUsersAsync().ConfigureAwait(false);
2016-08-18 21:00:54 +00:00
_log.Info("Connected");
2016-09-15 15:31:02 +00:00
//load commands and prefixes
using (var uow = DbHandler.UnitOfWork())
{
2016-10-12 02:47:28 +00:00
ModulePrefixes = new ConcurrentDictionary<string, string>(uow.BotConfig.GetOrCreate().ModulePrefixes.ToDictionary(m => m.ModuleName, m => m.Prefix));
2016-09-15 15:31:02 +00:00
}
// start handling messages received in commandhandler
await CommandHandler.StartHandling();
await CommandService.LoadAssembly(Assembly.GetEntryAssembly(), depMap).ConfigureAwait(false);
2016-08-13 18:45:08 +00:00
2016-09-06 01:59:00 +00:00
Console.WriteLine(await Stats.Print().ConfigureAwait(false));
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
}
}