2017-10-27 00:18:58 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
using Geekbot.net.Database;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
using Geekbot.net.Lib.AlmostRedis;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.ErrorHandling;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
using Geekbot.net.Lib.GlobalSettings;
|
2018-05-03 00:56:06 +02:00
|
|
|
|
using Geekbot.net.Lib.Logger;
|
|
|
|
|
using Geekbot.net.Lib.UserRepository;
|
2017-10-27 00:18:58 +02:00
|
|
|
|
|
2018-09-05 21:15:13 +02:00
|
|
|
|
namespace Geekbot.net.Commands.Admin.Owner
|
2017-10-27 00:18:58 +02:00
|
|
|
|
{
|
|
|
|
|
[Group("owner")]
|
2018-05-06 00:59:06 +02:00
|
|
|
|
[RequireOwner]
|
2017-10-27 00:18:58 +02:00
|
|
|
|
public class Owner : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
private readonly DatabaseContext _database;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
private readonly IGlobalSettings _globalSettings;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2018-05-26 02:33:45 +02:00
|
|
|
|
private readonly IAlmostRedis _redis;
|
2017-10-27 00:18:58 +02:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-26 02:33:45 +02:00
|
|
|
|
public Owner(IAlmostRedis redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, DatabaseContext database, IGlobalSettings globalSettings)
|
2017-10-27 00:18:58 +02:00
|
|
|
|
{
|
|
|
|
|
_redis = redis;
|
|
|
|
|
_client = client;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_userRepository = userRepositry;
|
|
|
|
|
_errorHandler = errorHandler;
|
2018-05-10 02:00:26 +02:00
|
|
|
|
_database = database;
|
2018-05-13 17:49:13 +02:00
|
|
|
|
_globalSettings = globalSettings;
|
2017-10-27 00:18:58 +02:00
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-11 00:36:29 +02:00
|
|
|
|
[Command("migrate", RunMode = RunMode.Async)]
|
2018-09-05 21:15:13 +02:00
|
|
|
|
public async Task Migrate(MigrationMethods method, string force = "")
|
2018-05-11 00:36:29 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-09-05 21:15:13 +02:00
|
|
|
|
switch (method)
|
2018-05-23 23:50:11 +02:00
|
|
|
|
{
|
2018-09-05 21:15:13 +02:00
|
|
|
|
case MigrationMethods.redis:
|
|
|
|
|
var status = _globalSettings.GetKey("MigrationStatus");
|
|
|
|
|
if (status.Equals("Running"))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("Migration already running");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (status.Equals("Done") && !force.Equals("force"))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("Migration already ran, write `!owner migrate redis force` to run again");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-23 23:50:11 +02:00
|
|
|
|
|
2018-09-05 21:15:13 +02:00
|
|
|
|
await ReplyAsync("starting migration");
|
|
|
|
|
await _globalSettings.SetKey("MigrationStatus", "Running");
|
|
|
|
|
var redisMigration = new RedisMigration(_database, _redis, _logger, _client);
|
|
|
|
|
await redisMigration.Migrate();
|
|
|
|
|
await _globalSettings.SetKey("MigrationStatus", "Done");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MigrationMethods.messages:
|
|
|
|
|
await ReplyAsync("Migrating Messages to postgres...");
|
|
|
|
|
var messageMigration = new MessageMigration(_database, _redis, _logger);
|
|
|
|
|
await messageMigration.Migrate();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
await ReplyAsync("No Migration Method specified...");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-05-11 00:36:29 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context);
|
2018-05-11 00:36:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ReplyAsync("done");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 00:18:58 +02:00
|
|
|
|
[Command("youtubekey", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Set the youtube api key")]
|
|
|
|
|
public async Task SetYoutubeKey([Summary("API Key")] string key)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _globalSettings.SetKey("YoutubeKey", key);
|
2017-10-27 00:18:58 +02:00
|
|
|
|
await ReplyAsync("Apikey has been set");
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-27 00:18:58 +02:00
|
|
|
|
[Command("game", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Set the game that the bot is playing")]
|
|
|
|
|
public async Task SetGame([Remainder] [Summary("Game")] string key)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _globalSettings.SetKey("Game", key);
|
2017-10-27 00:18:58 +02:00
|
|
|
|
await _client.SetGameAsync(key);
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.Geekbot, $"Changed game to {key}");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
await ReplyAsync($"Now Playing {key}");
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-27 00:18:58 +02:00
|
|
|
|
[Command("popuserrepo", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Populate user cache")]
|
2018-04-30 23:44:19 +02:00
|
|
|
|
public async Task PopUserRepoCommand()
|
2017-10-27 00:18:58 +02:00
|
|
|
|
{
|
|
|
|
|
var success = 0;
|
|
|
|
|
var failed = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.UserRepository, "Populating User Repositry");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
await ReplyAsync("Starting Population of User Repository");
|
|
|
|
|
foreach (var guild in _client.Guilds)
|
|
|
|
|
{
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Information(LogSource.UserRepository, $"Populating users from {guild.Name}");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
foreach (var user in guild.Users)
|
|
|
|
|
{
|
|
|
|
|
var succeded = await _userRepository.Update(user);
|
|
|
|
|
var inc = succeded ? success++ : failed++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-05-06 01:47:13 +02:00
|
|
|
|
_logger.Warning(LogSource.UserRepository, "Finished Updating User Repositry");
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Successfully Populated User Repository with {success} Users in {_client.Guilds.Count} Guilds (Failed: {failed})");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context,
|
2017-12-29 01:53:50 +01:00
|
|
|
|
"Couldn't complete User Repository, see console for more info");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-06 00:59:06 +02:00
|
|
|
|
|
|
|
|
|
[Command("error", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Throw an error un purpose")]
|
2018-06-13 22:18:57 +02:00
|
|
|
|
public async Task PurposefulError()
|
2018-05-06 00:59:06 +02:00
|
|
|
|
{
|
2018-05-11 00:36:29 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Error Generated by !owner error");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-06-13 22:18:57 +02:00
|
|
|
|
await _errorHandler.HandleCommandException(e, Context);
|
2018-05-11 00:36:29 +02:00
|
|
|
|
}
|
2018-05-06 00:59:06 +02:00
|
|
|
|
}
|
2017-10-27 00:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|