UserRepository, Handler now have own class, uptime in !info
This commit is contained in:
parent
a549950ad9
commit
6dcfeabfbd
6 changed files with 322 additions and 117 deletions
|
@ -1,7 +1,10 @@
|
|||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using Google.Apis.YouTube.v3.Data;
|
||||
using Serilog;
|
||||
using StackExchange.Redis;
|
||||
|
||||
|
@ -10,15 +13,19 @@ namespace Geekbot.net.Modules
|
|||
[Group("admin")]
|
||||
public class AdminCmd : ModuleBase
|
||||
{
|
||||
private readonly IDatabase redis;
|
||||
private readonly DiscordSocketClient client;
|
||||
private readonly ILogger logger;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
|
||||
public AdminCmd(IDatabase redis, DiscordSocketClient client, ILogger logger)
|
||||
public AdminCmd(IDatabase redis, DiscordSocketClient client, ILogger logger, IUserRepository userRepositry, IErrorHandler errorHandler)
|
||||
{
|
||||
this.redis = redis;
|
||||
this.client = client;
|
||||
this.logger = logger;
|
||||
_redis = redis;
|
||||
_client = client;
|
||||
_logger = logger;
|
||||
_userRepository = userRepositry;
|
||||
_errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
[RequireUserPermission(GuildPermission.Administrator)]
|
||||
|
@ -26,7 +33,7 @@ namespace Geekbot.net.Modules
|
|||
[Summary("Set a Welcome Message (use '$user' to mention the new joined user).")]
|
||||
public async Task SetWelcomeMessage([Remainder] [Summary("message")] string welcomeMessage)
|
||||
{
|
||||
redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] { new HashEntry("WelcomeMsg", welcomeMessage) });
|
||||
_redis.HashSet($"{Context.Guild.Id}:Settings", new HashEntry[] { new HashEntry("WelcomeMsg", welcomeMessage) });
|
||||
var formatedMessage = welcomeMessage.Replace("$user", Context.User.Mention);
|
||||
await ReplyAsync("Welcome message has been changed\r\nHere is an example of how it would look:\r\n" +
|
||||
formatedMessage);
|
||||
|
@ -36,14 +43,14 @@ namespace Geekbot.net.Modules
|
|||
[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;
|
||||
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);
|
||||
_redis.StringSet("youtubeKey", key);
|
||||
await ReplyAsync("Apikey has been set");
|
||||
}
|
||||
|
||||
|
@ -51,17 +58,52 @@ namespace Geekbot.net.Modules
|
|||
[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;
|
||||
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}");
|
||||
_redis.StringSet("Game", key);
|
||||
await _client.SetGameAsync(key);
|
||||
_logger.Information($"[Geekbot] Changed game to {key}");
|
||||
await ReplyAsync($"Now Playing {key}");
|
||||
}
|
||||
|
||||
[Command("popuserrepo", RunMode = RunMode.Async)]
|
||||
[Summary("Set the game that the bot is playing")]
|
||||
public async Task popUserRepoCommand()
|
||||
{
|
||||
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;
|
||||
}
|
||||
var success = 0;
|
||||
var failed = 0;
|
||||
try
|
||||
{
|
||||
_logger.Warning("[UserRepository] Populating User Repositry");
|
||||
await ReplyAsync("Starting Population of User Repository");
|
||||
foreach (var guild in _client.Guilds)
|
||||
{
|
||||
_logger.Information($"[UserRepository] Populating users from {guild.Name}");
|
||||
foreach (var user in guild.Users)
|
||||
{
|
||||
var succeded = await _userRepository.Update(user);
|
||||
var inc = succeded ? success++ : failed++;
|
||||
}
|
||||
}
|
||||
_logger.Warning("[UserRepository] Finished Updating User Repositry");
|
||||
await ReplyAsync($"Successfully Populated User Repository with {success} Users in {_client.Guilds.Count} Guilds (Failed: {failed})");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await ReplyAsync("Couldn't complete User Repository, see console for more info");
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,36 +1,49 @@
|
|||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Modules
|
||||
{
|
||||
public class Info : ModuleBase
|
||||
{
|
||||
private readonly IDatabase redis;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
|
||||
public Info(IDatabase redis)
|
||||
public Info(IDatabase redis, IErrorHandler errorHandler)
|
||||
{
|
||||
this.redis = redis;
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
[Command("info", RunMode = RunMode.Async)]
|
||||
[Summary("Get Information about the bot")]
|
||||
public async Task BotInfo()
|
||||
{
|
||||
var eb = new EmbedBuilder();
|
||||
try
|
||||
{
|
||||
var eb = new EmbedBuilder();
|
||||
|
||||
eb.WithTitle("Geekbot V3.2");
|
||||
eb.WithTitle("Geekbot V3.3");
|
||||
|
||||
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(redis.StringGet("botOwner"))).Result;
|
||||
var botOwner = Context.Guild.GetUserAsync(ulong.Parse(_redis.StringGet("botOwner"))).Result;
|
||||
var uptime = (DateTime.Now.Subtract(Process.GetCurrentProcess().StartTime));
|
||||
|
||||
eb.AddInlineField("Bot Name", Context.Client.CurrentUser.Username)
|
||||
.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count)
|
||||
.AddInlineField("Uptime", $"{uptime.Days}D {uptime.Hours}H {uptime.Minutes}M {uptime.Seconds}S")
|
||||
.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}")
|
||||
.AddInlineField("Website", "https://geekbot.pizzaandcoffee.rocks/");
|
||||
|
||||
eb.AddInlineField("Status", Context.Client.ConnectionState.ToString())
|
||||
.AddInlineField("Bot Name", Context.Client.CurrentUser.Username)
|
||||
.AddInlineField("Bot Owner", $"{botOwner.Username}#{botOwner.Discriminator}");
|
||||
|
||||
eb.AddInlineField("Servers", Context.Client.GetGuildsAsync().Result.Count);
|
||||
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,49 +16,60 @@ namespace Geekbot.net.Modules
|
|||
private readonly IDatabase redis;
|
||||
private readonly IErrorHandler errorHandler;
|
||||
private readonly ILogger logger;
|
||||
private readonly IUserRepository userRepository;
|
||||
|
||||
public UserInfo(IDatabase redis, IErrorHandler errorHandler, ILogger logger)
|
||||
public UserInfo(IDatabase redis, IErrorHandler errorHandler, ILogger logger, IUserRepository userRepository)
|
||||
{
|
||||
this.redis = redis;
|
||||
this.errorHandler = errorHandler;
|
||||
this.logger = logger;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
[Command("stats", RunMode = RunMode.Async)]
|
||||
[Summary("Get information about this user")]
|
||||
public async Task User([Summary("@someone")] IUser user = null)
|
||||
{
|
||||
var userInfo = user ?? Context.Message.Author;
|
||||
try
|
||||
{
|
||||
var userInfo = user ?? Context.Message.Author;
|
||||
var userGuildInfo = (IGuildUser) userInfo;
|
||||
var createdAt = userInfo.CreatedAt;
|
||||
var joinedAt = userGuildInfo.JoinedAt.Value;
|
||||
var age = Math.Floor((DateTime.Now - createdAt).TotalDays);
|
||||
var joinedDayAgo = Math.Floor((DateTime.Now - joinedAt).TotalDays);
|
||||
|
||||
var age = Math.Floor((DateTime.Now - userInfo.CreatedAt).TotalDays);
|
||||
var messages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
|
||||
var guildMessages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
||||
var level = LevelCalc.GetLevelAtExperience(messages);
|
||||
|
||||
var messages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", userInfo.Id.ToString());
|
||||
var guildMessages = (int) redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
||||
var level = LevelCalc.GetLevelAtExperience(messages);
|
||||
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);
|
||||
|
||||
var percent = Math.Round((double) (100 * messages) / guildMessages, 2);
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(userInfo.GetAvatarUrl())
|
||||
.WithName(userInfo.Username));
|
||||
eb.WithColor(new Color(221, 255, 119));
|
||||
|
||||
var karma = redis.HashGet($"{Context.Guild.Id}:Karma", userInfo.Id);
|
||||
var correctRolls = redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString());
|
||||
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(userInfo.GetAvatarUrl())
|
||||
.WithName(userInfo.Username));
|
||||
eb.WithColor(new Color(221, 255, 119));
|
||||
eb.AddInlineField("Discordian Since", $"{createdAt.Day}.{createdAt.Month}.{createdAt.Year} ({age} days)")
|
||||
.AddInlineField("Joined Server", $"{joinedAt.Day}.{joinedAt.Month}.{joinedAt.Year} ({joinedDayAgo} days)")
|
||||
.AddInlineField("Karma", karma.ToString() ?? "0")
|
||||
.AddInlineField("Level", level)
|
||||
.AddInlineField("Messages Sent", messages)
|
||||
.AddInlineField("Server Total", $"{percent}%");
|
||||
|
||||
eb.AddField("Discordian Since",
|
||||
$"{userInfo.CreatedAt.Day}/{userInfo.CreatedAt.Month}/{userInfo.CreatedAt.Year} ({age} days)");
|
||||
eb.AddInlineField("Level", level)
|
||||
.AddInlineField("Messages Sent", messages)
|
||||
.AddInlineField("Server Total", $"{percent}%");
|
||||
if (!correctRolls.IsNullOrEmpty)
|
||||
eb.AddInlineField("Guessed Rolls", correctRolls);
|
||||
|
||||
var karma = redis.HashGet($"{Context.Guild.Id}:Karma", userInfo.Id.ToString());
|
||||
if (!karma.IsNullOrEmpty)
|
||||
eb.AddInlineField("Karma", karma);
|
||||
|
||||
var correctRolls = redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString());
|
||||
if (!correctRolls.IsNullOrEmpty)
|
||||
eb.AddInlineField("Guessed Rolls", correctRolls);
|
||||
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("rank", RunMode = RunMode.Async)]
|
||||
|
@ -80,8 +91,8 @@ namespace Geekbot.net.Modules
|
|||
if (listLimiter > 10) break;
|
||||
try
|
||||
{
|
||||
var guildUser = Context.Guild.GetUserAsync((ulong) user.Name).Result;
|
||||
if (guildUser != null)
|
||||
var guildUser = userRepository.Get((ulong)user.Name);
|
||||
if (guildUser.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfill()
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue