Use Owner Attribute instead of manual inline checking

This commit is contained in:
runebaas 2018-05-06 00:59:06 +02:00
parent 26a0c89cf7
commit 0fe273151c
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6

View file

@ -12,7 +12,7 @@ using StackExchange.Redis;
namespace Geekbot.net.Commands.Admin
{
[Group("owner")]
[RequireUserPermission(GuildPermission.Administrator)]
[RequireOwner]
public class Owner : ModuleBase
{
private readonly DiscordSocketClient _client;
@ -21,8 +21,7 @@ namespace Geekbot.net.Commands.Admin
private readonly IDatabase _redis;
private readonly IUserRepository _userRepository;
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry,
IErrorHandler errorHandler)
public Owner(IDatabase redis, DiscordSocketClient client, IGeekbotLogger logger, IUserRepository userRepositry, IErrorHandler errorHandler)
{
_redis = redis;
_client = client;
@ -36,14 +35,6 @@ namespace Geekbot.net.Commands.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()))
{
await ReplyAsync(
$"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})");
return;
}
_redis.StringSet("youtubeKey", key);
await ReplyAsync("Apikey has been set");
}
@ -53,14 +44,6 @@ namespace Geekbot.net.Commands.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()))
{
await ReplyAsync(
$"Sorry, only the botowner can do this ({botOwner.Username}#{botOwner.Discriminator})");
return;
}
_redis.StringSet("Game", key);
await _client.SetGameAsync(key);
_logger.Information("Geekbot", $"Changed game to {key}");
@ -72,22 +55,6 @@ namespace Geekbot.net.Commands.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;
}
var success = 0;
var failed = 0;
try
@ -114,5 +81,14 @@ namespace Geekbot.net.Commands.Admin
"Couldn't complete User Repository, see console for more info");
}
}
[Command("error", RunMode = RunMode.Async)]
[Remarks(CommandCategories.Admin)]
[Summary("Throw an error un purpose")]
public void PurposefulError()
{
var e = new Exception("Error Generated by !owner error");
_errorHandler.HandleCommandException(e, Context);
}
}
}