geekbot/Geekbot.net/Commands/Admin/Owner/Owner.cs

95 lines
3.6 KiB
C#
Raw Normal View History

2019-05-16 22:00:28 +02:00
using System;
using System.Threading.Tasks;
2019-05-16 22:00:28 +02:00
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.GlobalSettings;
using Geekbot.net.Lib.Logger;
using Geekbot.net.Lib.UserRepository;
2018-09-05 21:15:13 +02:00
namespace Geekbot.net.Commands.Admin.Owner
{
[Group("owner")]
[RequireOwner]
public class Owner : ModuleBase
{
private readonly DiscordSocketClient _client;
2017-12-29 01:53:50 +01:00
private readonly IErrorHandler _errorHandler;
private readonly IGlobalSettings _globalSettings;
private readonly IGeekbotLogger _logger;
private readonly IUserRepository _userRepository;
2017-12-29 01:53:50 +01:00
2019-05-16 22:00:28 +02:00
public Owner(DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler, IGlobalSettings globalSettings)
{
_client = client;
_logger = logger;
_userRepository = userRepositry;
_errorHandler = errorHandler;
_globalSettings = globalSettings;
}
2017-12-29 01:53:50 +01:00
[Command("youtubekey", RunMode = RunMode.Async)]
[Summary("Set the youtube api key")]
public async Task SetYoutubeKey([Summary("API Key")] string key)
{
await _globalSettings.SetKey("YoutubeKey", key);
await ReplyAsync("Apikey has been set");
}
2017-12-29 01:53:50 +01:00
[Command("game", RunMode = RunMode.Async)]
[Summary("Set the game that the bot is playing")]
public async Task SetGame([Remainder] [Summary("Game")] string key)
{
await _globalSettings.SetKey("Game", key);
await _client.SetGameAsync(key);
_logger.Information(LogSource.Geekbot, $"Changed game to {key}");
await ReplyAsync($"Now Playing {key}");
}
2017-12-29 01:53:50 +01:00
[Command("popuserrepo", RunMode = RunMode.Async)]
[Summary("Populate user cache")]
2018-04-30 23:44:19 +02:00
public async Task PopUserRepoCommand()
{
var success = 0;
var failed = 0;
try
{
_logger.Warning(LogSource.UserRepository, "Populating User Repositry");
await ReplyAsync("Starting Population of User Repository");
foreach (var guild in _client.Guilds)
{
_logger.Information(LogSource.UserRepository, $"Populating users from {guild.Name}");
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
_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})");
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context,
2017-12-29 01:53:50 +01:00
"Couldn't complete User Repository, see console for more info");
}
}
[Command("error", RunMode = RunMode.Async)]
[Summary("Throw an error un purpose")]
public async Task PurposefulError()
{
2018-05-11 00:36:29 +02:00
try
{
throw new Exception("Error Generated by !owner error");
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
2018-05-11 00:36:29 +02:00
}
}
}
}