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;
|
2016-09-10 19:40:25 +00:00
|
|
|
|
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;
|
2016-09-16 17:20:37 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2016-09-16 17:20:37 +00:00
|
|
|
|
public static CommandService CommandService { get; private set; }
|
2016-08-29 22:47:04 +00:00
|
|
|
|
public static CommandHandler CommandHandler { get; private set; }
|
2016-09-30 03:58:44 +00:00
|
|
|
|
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
|
|
|
|
|
2016-08-29 22:47:04 +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-16 12:11:45 +00:00
|
|
|
|
|
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();
|
2016-09-10 21:02:09 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
|
|
_log.Info("Starting NadekoBot v" + typeof(NadekoBot).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2016-10-01 03:49:05 +00:00
|
|
|
|
|
|
|
|
|
Credentials = new BotCredentials();
|
|
|
|
|
|
2016-08-15 14:57:40 +00:00
|
|
|
|
//create client
|
2016-09-30 03:58:44 +00:00
|
|
|
|
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,
|
2016-10-01 03:49:05 +00:00
|
|
|
|
TotalShards = Credentials.TotalShards,
|
|
|
|
|
ConnectionTimeout = 60000
|
2016-08-13 18:45:08 +00:00
|
|
|
|
});
|
|
|
|
|
|
2016-08-15 14:57:40 +00:00
|
|
|
|
//initialize Services
|
2016-09-16 17:20:37 +00:00
|
|
|
|
CommandService = new CommandService();
|
2016-08-15 14:57:40 +00:00
|
|
|
|
Localizer = new Localization();
|
2016-08-21 22:37:39 +00:00
|
|
|
|
Google = new GoogleApiService();
|
2016-09-16 17:20:37 +00:00
|
|
|
|
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);
|
2016-09-30 03:58:44 +00:00
|
|
|
|
depMap.Add<ShardedDiscordClient >(Client);
|
2016-09-16 17:20:37 +00:00
|
|
|
|
depMap.Add<CommandService>(CommandService);
|
2016-08-21 22:37:39 +00:00
|
|
|
|
depMap.Add<IGoogleApiService>(Google);
|
2016-08-13 18:45:08 +00:00
|
|
|
|
|
2016-09-16 17:20:37 +00:00
|
|
|
|
|
|
|
|
|
//setup typereaders
|
|
|
|
|
CommandService.AddTypeReader<PermissionAction>(new PermissionActionTypeReader());
|
|
|
|
|
CommandService.AddTypeReader<Command>(new CommandTypeReader());
|
|
|
|
|
CommandService.AddTypeReader<Module>(new ModuleTypeReader());
|
2016-09-30 02:20:09 +00:00
|
|
|
|
CommandService.AddTypeReader<IGuild>(new GuildTypeReader());
|
2016-09-16 17:20:37 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
2016-10-10 08:06:06 +00:00
|
|
|
|
// start handling messages received in commandhandler
|
|
|
|
|
await CommandHandler.StartHandling();
|
|
|
|
|
|
2016-09-16 17:20:37 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2016-08-29 22:47:04 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-08-18 21:00:54 +00:00
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-13 18:45:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|