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;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-09-15 15:31:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-09-16 17:20:37 +00:00
|
|
|
|
using NadekoBot.Modules.Permissions;
|
|
|
|
|
using NadekoBot.TypeReaders;
|
2016-10-12 02:47:28 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2016-10-12 04:29:15 +00:00
|
|
|
|
using NadekoBot.Modules.Music;
|
2016-10-26 14:30:49 +00:00
|
|
|
|
using NadekoBot.Services.Database.Models;
|
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-11-30 01:28:51 +00:00
|
|
|
|
|
2017-01-18 20:56:31 +00:00
|
|
|
|
public static Color OkColor { get; }
|
|
|
|
|
public static Color ErrorColor { get; }
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
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; }
|
2017-01-15 01:08:14 +00:00
|
|
|
|
public static DiscordShardedClient Client { get; private set; }
|
2016-08-15 23:38:28 +00:00
|
|
|
|
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-10-12 22:32:11 +00:00
|
|
|
|
public static bool Ready { get; private set; }
|
2016-09-15 15:31:02 +00:00
|
|
|
|
|
2016-10-26 14:30:49 +00:00
|
|
|
|
public static IEnumerable<GuildConfig> AllGuildConfigs { get; }
|
2017-01-12 00:21:32 +00:00
|
|
|
|
public static BotConfig BotConfig { get; }
|
2016-10-26 14:30:49 +00:00
|
|
|
|
|
|
|
|
|
static NadekoBot()
|
|
|
|
|
{
|
2016-11-15 09:54:56 +00:00
|
|
|
|
SetupLogger();
|
|
|
|
|
Credentials = new BotCredentials();
|
|
|
|
|
|
2016-10-26 14:30:49 +00:00
|
|
|
|
using (var uow = DbHandler.UnitOfWork())
|
|
|
|
|
{
|
2016-12-21 08:39:19 +00:00
|
|
|
|
AllGuildConfigs = uow.GuildConfigs.GetAllGuildConfigs();
|
2017-01-12 00:21:32 +00:00
|
|
|
|
BotConfig = uow.BotConfig.GetOrCreate();
|
2017-01-18 20:56:31 +00:00
|
|
|
|
OkColor = new Color(Convert.ToUInt32(BotConfig.OkColor, 16));
|
|
|
|
|
ErrorColor = new Color(Convert.ToUInt32(BotConfig.ErrorColor, 16));
|
2016-10-26 14:30:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 00:31:41 +00:00
|
|
|
|
public async Task RunAsync(params string[] args)
|
2016-08-13 18:45:08 +00:00
|
|
|
|
{
|
2016-09-10 21:02:09 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2016-10-24 10:40:18 +00:00
|
|
|
|
_log.Info("Starting NadekoBot v" + StatsService.BotVersion);
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
2016-08-15 14:57:40 +00:00
|
|
|
|
//create client
|
2017-01-15 01:08:14 +00:00
|
|
|
|
Client = new DiscordShardedClient(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,
|
2016-10-12 03:22:14 +00:00
|
|
|
|
ConnectionTimeout = int.MaxValue
|
2016-08-13 18:45:08 +00:00
|
|
|
|
});
|
2017-01-15 20:41:35 +00:00
|
|
|
|
#if GLOBAL_NADEKO
|
2017-01-15 01:08:14 +00:00
|
|
|
|
Client.Log += Client_Log;
|
2017-01-15 20:41:35 +00:00
|
|
|
|
#endif
|
2017-01-15 01:08:14 +00:00
|
|
|
|
|
2016-08-15 14:57:40 +00:00
|
|
|
|
//initialize Services
|
2016-12-31 16:34:21 +00:00
|
|
|
|
CommandService = new CommandService(new CommandServiceConfig() {
|
2017-01-01 11:48:07 +00:00
|
|
|
|
CaseSensitiveCommands = false
|
2016-12-31 16:34:21 +00:00
|
|
|
|
});
|
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-12-08 17:35:34 +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
|
|
|
|
|
2016-09-16 17:20:37 +00:00
|
|
|
|
|
|
|
|
|
//setup typereaders
|
|
|
|
|
CommandService.AddTypeReader<PermissionAction>(new PermissionActionTypeReader());
|
2016-12-16 18:43:57 +00:00
|
|
|
|
CommandService.AddTypeReader<CommandInfo>(new CommandTypeReader());
|
2016-12-18 00:52:53 +00:00
|
|
|
|
CommandService.AddTypeReader<ModuleInfo>(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);
|
2017-01-16 14:25:50 +00:00
|
|
|
|
//await Client.DownloadAllUsersAsync().ConfigureAwait(false);
|
2017-01-15 17:56:18 +00:00
|
|
|
|
Stats.Initialize();
|
2016-08-18 21:00:54 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Connected");
|
|
|
|
|
|
2016-09-15 15:31:02 +00:00
|
|
|
|
//load commands and prefixes
|
2017-01-12 00:21:32 +00:00
|
|
|
|
|
|
|
|
|
ModulePrefixes = new ConcurrentDictionary<string, string>(NadekoBot.BotConfig.ModulePrefixes.OrderByDescending(mp => mp.Prefix.Length).ToDictionary(m => m.ModuleName, m => m.Prefix));
|
|
|
|
|
|
2016-10-10 08:06:06 +00:00
|
|
|
|
// start handling messages received in commandhandler
|
2017-01-15 17:56:18 +00:00
|
|
|
|
|
2016-10-22 19:30:53 +00:00
|
|
|
|
await CommandHandler.StartHandling().ConfigureAwait(false);
|
2016-10-10 08:06:06 +00:00
|
|
|
|
|
2016-12-16 18:43:57 +00:00
|
|
|
|
await CommandService.AddModulesAsync(this.GetType().GetTypeInfo().Assembly).ConfigureAwait(false);
|
2016-10-12 04:29:15 +00:00
|
|
|
|
#if !GLOBAL_NADEKO
|
2016-12-16 18:43:57 +00:00
|
|
|
|
await CommandService.AddModuleAsync<Music>().ConfigureAwait(false);
|
2016-10-12 04:29:15 +00:00
|
|
|
|
#endif
|
2016-10-12 22:32:11 +00:00
|
|
|
|
Ready = true;
|
2016-09-06 01:59:00 +00:00
|
|
|
|
Console.WriteLine(await Stats.Print().ConfigureAwait(false));
|
2016-11-15 00:31:41 +00:00
|
|
|
|
}
|
2016-08-13 18:45:08 +00:00
|
|
|
|
|
2017-01-15 01:08:14 +00:00
|
|
|
|
private Task Client_Log(LogMessage arg)
|
|
|
|
|
{
|
2017-01-15 02:18:13 +00:00
|
|
|
|
_log.Warn(arg.Source + " | " + arg.Message);
|
2017-01-15 01:08:14 +00:00
|
|
|
|
if (arg.Exception != null)
|
|
|
|
|
_log.Warn(arg.Exception);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 00:31:41 +00:00
|
|
|
|
public async Task RunAndBlockAsync(params string[] args)
|
|
|
|
|
{
|
|
|
|
|
await RunAsync(args).ConfigureAwait(false);
|
|
|
|
|
await Task.Delay(-1).ConfigureAwait(false);
|
2016-08-13 18:45:08 +00:00
|
|
|
|
}
|
2016-08-15 14:57:40 +00:00
|
|
|
|
|
2016-11-15 09:54:56 +00:00
|
|
|
|
private static void SetupLogger()
|
2016-08-18 21:00:54 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|