2017-04-26 14:12:42 +02:00
|
|
|
|
using System;
|
2018-05-09 01:21:39 +02:00
|
|
|
|
using System.Linq;
|
2017-09-29 20:30:00 +02:00
|
|
|
|
using System.Net;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
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-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;
|
|
|
|
|
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-05-09 01:21:39 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-10-02 21:57:48 +02:00
|
|
|
|
using Nancy.Hosting.Self;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
using StackExchange.Redis;
|
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;
|
|
|
|
|
private IDatabase _redis;
|
|
|
|
|
private IServiceCollection _services;
|
|
|
|
|
private IServiceProvider _servicesProvider;
|
|
|
|
|
private RedisValue _token;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
private GeekbotLogger _logger;
|
2018-04-30 23:44:19 +02:00
|
|
|
|
private IUserRepository _userRepository;
|
|
|
|
|
private bool _firstStart;
|
2018-05-02 20:19:11 +02:00
|
|
|
|
private RunParameters _runParameters;
|
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(@" \____|_____|_____|_|\_\____/ \___/ |_|");
|
|
|
|
|
logo.AppendLine("=========================================");
|
|
|
|
|
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
|
|
|
|
|
2017-09-14 22:11:19 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var redisMultiplexer = ConnectionMultiplexer.Connect("127.0.0.1:6379");
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_redis = redisMultiplexer.GetDatabase(6);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
logger.Information(LogSource.Redis, $"Connected to db {_redis.Database}");
|
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-06 02:00:45 +02:00
|
|
|
|
_token = runParameters.Token ?? _redis.StringGet("discordToken");
|
2018-04-30 23:44:19 +02:00
|
|
|
|
if (_token.IsNullOrEmpty)
|
2017-04-26 14:12:42 +02:00
|
|
|
|
{
|
|
|
|
|
Console.Write("Your bot Token: ");
|
|
|
|
|
var newToken = Console.ReadLine();
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_redis.StringSet("discordToken", newToken);
|
|
|
|
|
_redis.StringSet("Game", "Ping Pong");
|
|
|
|
|
_token = newToken;
|
|
|
|
|
_firstStart = true;
|
2017-09-26 22:09:57 +02:00
|
|
|
|
}
|
2017-04-26 14:12:42 +02:00
|
|
|
|
|
2018-05-09 18:51:53 +02:00
|
|
|
|
var database = new DatabaseInitializer(runParameters, logger).Initzialize();
|
2018-05-09 01:21:39 +02:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_services = new ServiceCollection();
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-05-10 02:00:26 +02:00
|
|
|
|
_userRepository = new UserRepository(database, logger);
|
2018-02-14 23:01:28 +01:00
|
|
|
|
var fortunes = new FortunesProvider(logger);
|
|
|
|
|
var mediaProvider = new MediaProvider(logger);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var malClient = new MalClient(_redis, 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();
|
2017-09-27 22:48:09 +02:00
|
|
|
|
|
2018-05-04 01:14:43 +02:00
|
|
|
|
_services.AddSingleton<IDatabase>(_redis);
|
|
|
|
|
_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-05-09 01:21:39 +02:00
|
|
|
|
_services.AddSingleton<DatabaseContext>(database);
|
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-04-30 23:44:19 +02:00
|
|
|
|
await _client.SetGameAsync(_redis.StringGet("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-04-30 23:44:19 +02:00
|
|
|
|
var translationHandler = new TranslationHandler(_client.Guilds, _redis, _logger);
|
2018-05-02 20:19:11 +02:00
|
|
|
|
var errorHandler = new ErrorHandler(_logger, translationHandler, _runParameters.ExposeErrors);
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var reactionListener = new ReactionListener(_redis);
|
|
|
|
|
await _commands.AddModulesAsync(Assembly.GetEntryAssembly());
|
|
|
|
|
_services.AddSingleton(_commands);
|
|
|
|
|
_services.AddSingleton<IErrorHandler>(errorHandler);
|
|
|
|
|
_services.AddSingleton<ITranslationHandler>(translationHandler);
|
|
|
|
|
_services.AddSingleton(_client);
|
|
|
|
|
_services.AddSingleton<IReactionListener>(reactionListener);
|
|
|
|
|
_servicesProvider = _services.BuildServiceProvider();
|
2017-09-30 01:38:10 +02:00
|
|
|
|
|
2018-04-30 23:44:19 +02:00
|
|
|
|
var handlers = new Handlers(_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;
|
|
|
|
|
_client.MessageReceived += handlers.UpdateStats;
|
|
|
|
|
_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-02 20:19:11 +02:00
|
|
|
|
if (_firstStart || _runParameters.Reset)
|
2017-09-29 20:30:00 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, "Finishing setup");
|
2017-09-29 20:30:00 +02:00
|
|
|
|
await FinishSetup();
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, "Setup finished");
|
2017-09-29 20:30:00 +02:00
|
|
|
|
}
|
2018-05-02 20:19:11 +02:00
|
|
|
|
if (!_runParameters.DisableApi)
|
2017-10-02 21:57:48 +02:00
|
|
|
|
{
|
2018-04-30 23:44:19 +02:00
|
|
|
|
StartWebApi();
|
2017-10-02 21:57:48 +02:00
|
|
|
|
}
|
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");
|
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-06 01:47:13 +02:00
|
|
|
|
_logger.Error(LogSource.Geekbot, "Could not connect...", 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-04-30 23:44:19 +02:00
|
|
|
|
private void StartWebApi()
|
2017-10-12 16:34:10 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Api, "Starting Webserver");
|
2017-10-12 16:34:10 +02:00
|
|
|
|
var webApiUrl = new Uri("http://localhost:12995");
|
|
|
|
|
new NancyHost(webApiUrl).Start();
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Api, $"Webserver now running on {webApiUrl}");
|
2017-10-12 16:34:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 20:30:00 +02:00
|
|
|
|
private async Task<Task> FinishSetup()
|
|
|
|
|
{
|
2017-11-10 21:36:41 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
// ToDo: Set bot avatar
|
|
|
|
|
var appInfo = await _client.GetApplicationInfoAsync();
|
2018-04-30 23:44:19 +02:00
|
|
|
|
_redis.StringSet("botOwner", appInfo.Owner.Id);
|
2017-11-10 21:36:41 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2017-09-29 20:30:00 +02:00
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.Geekbot, "Setup Failed, couldn't retrieve discord application data", e);
|
2017-09-29 20:30:00 +02:00
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
2017-04-26 14:12:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-15 22:56:03 +02:00
|
|
|
|
}
|