2017-04-26 14:12:42 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2017-09-26 13:02:38 +02:00
|
|
|
|
using System.Text;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2018-05-02 20:19:11 +02:00
|
|
|
|
using CommandLine;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
2018-05-09 01:21:39 +02:00
|
|
|
|
using Geekbot.net.Database;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
using Geekbot.net.Lib.AlmostRedis;
|
2018-05-04 00:54:01 +02:00
|
|
|
|
using Geekbot.net.Lib.Audio;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Clients;
|
|
|
|
|
using Geekbot.net.Lib.Converters;
|
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Lib.GlobalSettings;
|
2018-09-05 22:55:45 +02:00
|
|
|
|
using Geekbot.net.Lib.Highscores;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Levels;
|
|
|
|
|
using Geekbot.net.Lib.Localization;
|
|
|
|
|
using Geekbot.net.Lib.Logger;
|
2017-09-27 17:18:31 +02:00
|
|
|
|
using Geekbot.net.Lib.Media;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ReactionListener;
|
|
|
|
|
using Geekbot.net.Lib.UserRepository;
|
2018-08-29 21:16:01 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
using WikipediaApi;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
|
|
|
|
namespace Geekbot.net
|
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
internal class Program
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private DiscordSocketClient _client;
|
|
|
|
|
private CommandService _commands;
|
2018-08-29 21:16:01 +02:00
|
|
|
|
private DatabaseInitializer _databaseInitializer;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
private IGlobalSettings _globalSettings;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private IServiceCollection _services;
|
|
|
|
|
private IServiceProvider _servicesProvider;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
private string _token;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
private GeekbotLogger _logger;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private IUserRepository _userRepository;
|
2018-05-02 20:19:11 +02:00
|
|
|
|
private RunParameters _runParameters;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
private IAlmostRedis _redis;
|
2018-09-05 22:55:45 +02:00
|
|
|
|
private IHighscoreManager _highscoreManager;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
2017-09-26 13:02:38 +02:00
|
|
|
|
private static void Main(string[] args)
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
2018-05-02 20:19:11 +02:00
|
|
|
|
RunParameters runParameters = null;
|
|
|
|
|
Parser.Default.ParseArguments<RunParameters>(args)
|
|
|
|
|
.WithParsed(e => runParameters = e)
|
2018-05-06 02:00:45 +02:00
|
|
|
|
.WithNotParsed(_ => Environment.Exit(GeekbotExitCode.InvalidArguments.GetHashCode()));
|
2018-05-02 20:19:11 +02:00
|
|
|
|
|
2017-09-26 22:09:57 +02:00
|
|
|
|
var logo = new StringBuilder();
|
2017-09-26 13:02:38 +02:00
|
|
|
|
logo.AppendLine(@" ____ _____ _____ _ ______ ___ _____");
|
|
|
|
|
logo.AppendLine(@" / ___| ____| ____| |/ / __ ) / _ \\_ _|");
|
|
|
|
|
logo.AppendLine(@"| | _| _| | _| | ' /| _ \| | | || |");
|
|
|
|
|
logo.AppendLine(@"| |_| | |___| |___| . \| |_) | |_| || |");
|
|
|
|
|
logo.AppendLine(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
2018-05-26 02:33:45 +02:00
|
|
|
|
logo.AppendLine($"Version {Constants.BotVersion()} ".PadRight(41, '='));
|
2017-09-26 13:02:38 +02:00
|
|
|
|
Console.WriteLine(logo.ToString());
|
2018-05-01 16:50:48 +02:00
|
|
|
|
var sumologicActive = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GEEKBOT_SUMO"));
|
2018-05-02 20:19:11 +02:00
|
|
|
|
var logger = new GeekbotLogger(runParameters, sumologicActive);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Information(LogSource.Geekbot, "Starting...");
|
2017-10-26 00:55:04 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-02 20:19:11 +02:00
|
|
|
|
new Program().MainAsync(runParameters, logger).GetAwaiter().GetResult();
|
2017-10-26 00:55:04 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Error(LogSource.Geekbot, "RIP", e);
|
2017-10-26 00:55:04 +02:00
|
|
|
|
}
|
2017-04-26 14:12:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-03 00:56:06 +02:00
|
|
|
|
private async Task MainAsync(RunParameters runParameters, GeekbotLogger logger)
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_logger = logger;
|
2018-05-02 20:19:11 +02:00
|
|
|
|
_runParameters = runParameters;
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Information(LogSource.Geekbot, "Initing Stuff");
|
2018-05-03 00:56:06 +02:00
|
|
|
|
var discordLogger = new DiscordLogger(logger);
|
2017-09-26 22:09:57 +02:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_client = new DiscordSocketClient(new DiscordSocketConfig
|
2017-09-26 22:09:57 +02:00
|
|
|
|
{
|
2017-09-30 17:26:24 +02:00
|
|
|
|
LogLevel = LogSeverity.Verbose,
|
2017-11-10 21:37:39 +01:00
|
|
|
|
MessageCacheSize = 1000
|
2017-09-26 22:09:57 +02:00
|
|
|
|
});
|
2018-05-03 00:56:06 +02:00
|
|
|
|
_client.Log += discordLogger.Log;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_commands = new CommandService();
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
2018-08-29 21:16:01 +02:00
|
|
|
|
_databaseInitializer = new DatabaseInitializer(runParameters, logger);
|
|
|
|
|
var database = _databaseInitializer.Initialize();
|
|
|
|
|
database.Database.EnsureCreated();
|
|
|
|
|
if(!_runParameters.InMemory) database.Database.Migrate();
|
|
|
|
|
|
|
|
|
|
_globalSettings = new GlobalSettings(database);
|
2018-05-13 18:47:01 +02:00
|
|
|
|
|
2017-09-14 22:11:19 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-26 02:33:45 +02:00
|
|
|
|
_redis = new AlmostRedis(logger, runParameters);
|
|
|
|
|
_redis.Connect();
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
2017-09-27 17:18:31 +02:00
|
|
|
|
catch (Exception e)
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Error(LogSource.Redis, "Redis Connection Failed", e);
|
2018-05-06 02:00:45 +02:00
|
|
|
|
Environment.Exit(GeekbotExitCode.RedisConnectionFailed.GetHashCode());
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
2017-09-27 22:48:09 +02:00
|
|
|
|
|
2018-05-13 18:47:01 +02:00
|
|
|
|
_token = runParameters.Token ?? _globalSettings.GetKey("DiscordToken");
|
2018-05-26 02:33:45 +02:00
|
|
|
|
if (string.IsNullOrEmpty(_token))
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
|
|
|
|
Console.Write("Your bot Token: ");
|
|
|
|
|
var newToken = Console.ReadLine();
|
2018-07-28 16:31:18 +02:00
|
|
|
|
await _globalSettings.SetKey("DiscordToken", newToken);
|
|
|
|
|
await _globalSettings.SetKey("Game", "Ping Pong");
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_token = newToken;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_services = new ServiceCollection();
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-08-29 21:16:01 +02:00
|
|
|
|
_userRepository = new UserRepository(_databaseInitializer.Initialize(), logger);
|
2018-02-14 23:01:28 +01:00
|
|
|
|
var fortunes = new FortunesProvider(logger);
|
|
|
|
|
var mediaProvider = new MediaProvider(logger);
|
2018-05-15 01:18:26 +02:00
|
|
|
|
var malClient = new MalClient(_globalSettings, logger);
|
2017-10-25 00:58:59 +02:00
|
|
|
|
var levelCalc = new LevelCalc();
|
2017-11-06 23:55:28 +01:00
|
|
|
|
var emojiConverter = new EmojiConverter();
|
2018-02-19 21:35:45 +01:00
|
|
|
|
var mtgManaConverter = new MtgManaConverter();
|
2018-04-28 01:01:48 +02:00
|
|
|
|
var wikipediaClient = new WikipediaClient();
|
2018-05-04 00:54:01 +02:00
|
|
|
|
var audioUtils = new AudioUtils();
|
2018-09-05 22:55:45 +02:00
|
|
|
|
_highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository);
|
2017-09-27 22:48:09 +02:00
|
|
|
|
|
2018-05-26 02:33:45 +02:00
|
|
|
|
_services.AddSingleton<IAlmostRedis>(_redis);
|
2018-05-04 01:14:43 +02:00
|
|
|
|
_services.AddSingleton<IUserRepository>(_userRepository);
|
|
|
|
|
_services.AddSingleton<IGeekbotLogger>(logger);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_services.AddSingleton<ILevelCalc>(levelCalc);
|
|
|
|
|
_services.AddSingleton<IEmojiConverter>(emojiConverter);
|
|
|
|
|
_services.AddSingleton<IFortunesProvider>(fortunes);
|
|
|
|
|
_services.AddSingleton<IMediaProvider>(mediaProvider);
|
|
|
|
|
_services.AddSingleton<IMalClient>(malClient);
|
|
|
|
|
_services.AddSingleton<IMtgManaConverter>(mtgManaConverter);
|
|
|
|
|
_services.AddSingleton<IWikipediaClient>(wikipediaClient);
|
2018-05-04 00:54:01 +02:00
|
|
|
|
_services.AddSingleton<IAudioUtils>(audioUtils);
|
2018-09-05 22:55:45 +02:00
|
|
|
|
_services.AddSingleton<IHighscoreManager>(_highscoreManager);
|
2018-05-13 17:49:13 +02:00
|
|
|
|
_services.AddSingleton<IGlobalSettings>(_globalSettings);
|
2018-08-29 21:16:01 +02:00
|
|
|
|
_services.AddTransient<DatabaseContext>((e) => _databaseInitializer.Initialize());
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Information(LogSource.Geekbot, "Connecting to Discord");
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
|
|
|
|
await Login();
|
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-26 22:09:57 +02:00
|
|
|
|
private async Task Login()
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
await _client.LoginAsync(TokenType.Bot, _token);
|
|
|
|
|
await _client.StartAsync();
|
|
|
|
|
var isConneted = await IsConnected();
|
2017-04-27 19:50:03 +02:00
|
|
|
|
if (isConneted)
|
|
|
|
|
{
|
2018-05-13 17:49:13 +02:00
|
|
|
|
await _client.SetGameAsync(_globalSettings.GetKey("Game"));
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, $"Now Connected as {_client.CurrentUser.Username} to {_client.Guilds.Count} Servers");
|
2018-04-30 23:44:19 +02:00
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, "Registering Stuff");
|
2018-08-29 21:16:01 +02:00
|
|
|
|
var translationHandler = new TranslationHandler(_databaseInitializer.Initialize(), _logger);
|
2018-09-11 00:13:54 +02:00
|
|
|
|
var errorHandler = new ErrorHandler(_logger, translationHandler, _globalSettings, _runParameters.ExposeErrors);
|
2018-05-26 02:33:45 +02:00
|
|
|
|
var reactionListener = new ReactionListener(_redis.Db);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_services.AddSingleton<IErrorHandler>(errorHandler);
|
|
|
|
|
_services.AddSingleton<ITranslationHandler>(translationHandler);
|
|
|
|
|
_services.AddSingleton(_client);
|
|
|
|
|
_services.AddSingleton<IReactionListener>(reactionListener);
|
|
|
|
|
_servicesProvider = _services.BuildServiceProvider();
|
2018-08-29 22:04:27 +02:00
|
|
|
|
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider);
|
|
|
|
|
|
2018-08-29 21:16:01 +02:00
|
|
|
|
var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener);
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_client.MessageReceived += handlers.RunCommand;
|
2018-09-05 21:22:56 +02:00
|
|
|
|
_client.MessageReceived += handlers.UpdateStats;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_client.MessageDeleted += handlers.MessageDeleted;
|
|
|
|
|
_client.UserJoined += handlers.UserJoined;
|
|
|
|
|
_client.UserUpdated += handlers.UserUpdated;
|
|
|
|
|
_client.UserLeft += handlers.UserLeft;
|
|
|
|
|
_client.ReactionAdded += handlers.ReactionAdded;
|
|
|
|
|
_client.ReactionRemoved += handlers.ReactionRemoved;
|
2018-05-15 01:18:26 +02:00
|
|
|
|
|
|
|
|
|
var webserver = _runParameters.DisableApi ? Task.Delay(10) : StartWebApi();
|
2017-09-29 20:30:00 +02:00
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, "Done and ready for use");
|
2018-05-15 01:18:26 +02:00
|
|
|
|
|
|
|
|
|
await webserver;
|
2017-04-27 19:50:03 +02:00
|
|
|
|
}
|
2017-04-26 14:12:42 +02:00
|
|
|
|
}
|
2017-09-27 22:48:09 +02:00
|
|
|
|
catch (Exception e)
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
2018-05-26 02:33:45 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Could not connect to Discord", e);
|
2018-05-06 02:00:45 +02:00
|
|
|
|
Environment.Exit(GeekbotExitCode.CouldNotLogin.GetHashCode());
|
2017-04-26 14:12:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private async Task<bool> IsConnected()
|
2017-04-27 19:50:03 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
while (!_client.ConnectionState.Equals(ConnectionState.Connected))
|
2017-04-27 19:50:03 +02:00
|
|
|
|
await Task.Delay(25);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 01:18:26 +02:00
|
|
|
|
private Task StartWebApi()
|
2017-10-12 16:34:10 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Api, "Starting Webserver");
|
2018-09-05 22:55:45 +02:00
|
|
|
|
WebApi.WebApiStartup.StartWebApi(_logger, _runParameters, _commands, _databaseInitializer.Initialize(), _client, _globalSettings, _highscoreManager);
|
2018-05-15 01:18:26 +02:00
|
|
|
|
return Task.CompletedTask;
|
2017-10-12 16:34:10 +02:00
|
|
|
|
}
|
2017-04-26 14:12:42 +02:00
|
|
|
|
}
|
2017-09-15 22:56:03 +02:00
|
|
|
|
}
|