NadekoBot/src/NadekoBot/NadekoBot.cs

157 lines
5.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.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;
using NadekoBot.Modules.Permissions;
using NadekoBot.TypeReaders;
2016-10-12 02:47:28 +00:00
using System.Collections.Concurrent;
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-12-16 18:43:57 +00:00
public static Color OkColor { get; } = new Color(0x71cd40);
public static Color ErrorColor { get; } = new Color(0xee281f);
2016-08-18 21:00:54 +00:00
public static CommandService CommandService { get; private set; }
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
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-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();
2016-10-26 14:30:49 +00:00
}
}
public async Task RunAsync(params string[] args)
2016-08-13 18:45:08 +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,
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() {
CaseSensitiveCommands = false
2016-12-31 16:34:21 +00:00
});
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-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
//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());
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);
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));
// 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-12-16 18:43:57 +00:00
await CommandService.AddModulesAsync(this.GetType().GetTypeInfo().Assembly).ConfigureAwait(false);
#if !GLOBAL_NADEKO
2016-12-16 18:43:57 +00:00
await CommandService.AddModuleAsync<Music>().ConfigureAwait(false);
#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-08-13 18:45:08 +00:00
2017-01-15 01:08:14 +00:00
private Task Client_Log(LogMessage arg)
{
_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;
}
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;
}
catch (Exception ex)
{
2016-08-18 21:00:54 +00:00
Console.WriteLine(ex);
}
}
2016-08-13 18:45:08 +00:00
}
}