2017-10-27 00:18:58 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using Geekbot.net.Lib;
|
|
|
|
|
using Serilog;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
|
|
|
|
namespace Geekbot.net.Commands
|
|
|
|
|
{
|
|
|
|
|
[Group("owner")]
|
|
|
|
|
[RequireUserPermission(GuildPermission.Administrator)]
|
|
|
|
|
public class Owner : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2018-01-20 01:38:49 +01:00
|
|
|
|
private readonly IGeekbotLogger _logger;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IDatabase _redis;
|
2017-10-27 00:18:58 +02:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2018-01-20 01:38:49 +01:00
|
|
|
|
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry,
|
2017-12-29 01:53:50 +01:00
|
|
|
|
IErrorHandler errorHandler)
|
2017-10-27 00:18:58 +02:00
|
|
|
|
{
|
|
|
|
|
_redis = redis;
|
|
|
|
|
_client = client;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_userRepository = userRepositry;
|
|
|
|
|
_errorHandler = errorHandler;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-27 00:18:58 +02:00
|
|
|
|
[Command("youtubekey", RunMode = RunMode.Async)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[Summary("Set the youtube api key")]
|
|
|
|
|
public async Task SetYoutubeKey([Summary("API Key")] string key)
|
|
|
|
|
{
|
|
|
|
|
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result;
|
|
|
|
|
if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString()))
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_redis.StringSet("youtubeKey", key);
|
|
|
|
|
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)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[Summary("Set the game that the bot is playing")]
|
|
|
|
|
public async Task SetGame([Remainder] [Summary("Game")] string key)
|
|
|
|
|
{
|
|
|
|
|
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result;
|
|
|
|
|
if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString()))
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_redis.StringSet("Game", key);
|
|
|
|
|
await _client.SetGameAsync(key);
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Information("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)]
|
|
|
|
|
[Remarks(CommandCategories.Admin)]
|
|
|
|
|
[Summary("Populate user cache")]
|
|
|
|
|
public async Task popUserRepoCommand()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result;
|
|
|
|
|
if (!Context.User.Id.ToString().Equals(botOwner.Id.ToString()))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"Sorry, only the botowner can do this");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-29 01:53:50 +01:00
|
|
|
|
|
2017-10-27 00:18:58 +02:00
|
|
|
|
var success = 0;
|
|
|
|
|
var failed = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-01-20 01:38:49 +01:00
|
|
|
|
_logger.Warning("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-01-20 01:38:49 +01:00
|
|
|
|
_logger.Information("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-01-20 01:38:49 +01:00
|
|
|
|
_logger.Warning("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)
|
|
|
|
|
{
|
2017-12-29 01:53:50 +01:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context,
|
|
|
|
|
"Couldn't complete User Repository, see console for more info");
|
2017-10-27 00:18:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|