Refaction all files into component based folders
This commit is contained in:
parent
55e152f4aa
commit
e3adf55742
102 changed files with 816 additions and 709 deletions
57
Geekbot.net/Commands/User/GuildInfo.cs
Normal file
57
Geekbot.net/Commands/User/GuildInfo.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Levels;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.User
|
||||
{
|
||||
public class GuildInfo : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly ILevelCalc _levelCalc;
|
||||
private readonly IDatabase _redis;
|
||||
|
||||
public GuildInfo(IDatabase redis, ILevelCalc levelCalc, IErrorHandler errorHandler)
|
||||
{
|
||||
_redis = redis;
|
||||
_levelCalc = levelCalc;
|
||||
_errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
[Command("serverstats", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Statistics)]
|
||||
[Summary("Show some info about the bot.")]
|
||||
public async Task GetInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(Context.Guild.IconUrl)
|
||||
.WithName(Context.Guild.Name));
|
||||
eb.WithColor(new Color(110, 204, 147));
|
||||
|
||||
var created = Context.Guild.CreatedAt;
|
||||
var age = Math.Floor((DateTime.Now - created).TotalDays);
|
||||
|
||||
var messages = _redis.HashGet($"{Context.Guild.Id}:Messages", 0.ToString());
|
||||
var level = _levelCalc.GetLevel((int) messages);
|
||||
|
||||
eb.AddField("Server Age", $"{created.Day}/{created.Month}/{created.Year} ({age} days)");
|
||||
eb.AddInlineField("Level", level)
|
||||
.AddInlineField("Messages", messages);
|
||||
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
129
Geekbot.net/Commands/User/Karma.cs
Normal file
129
Geekbot.net/Commands/User/Karma.cs
Normal file
|
@ -0,0 +1,129 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Localization;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.User
|
||||
{
|
||||
public class Karma : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly ITranslationHandler _translation;
|
||||
|
||||
public Karma(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation)
|
||||
{
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
_translation = translation;
|
||||
}
|
||||
|
||||
[Command("good", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Karma)]
|
||||
[Summary("Increase Someones Karma")]
|
||||
public async Task Good([Summary("@someone")] IUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transDict = _translation.GetDict(Context);
|
||||
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
|
||||
var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString());
|
||||
if (user.Id == Context.User.Id)
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
|
||||
}
|
||||
else if (TimeoutFinished(lastKarma))
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
||||
GetTimeLeft(lastKarma)));
|
||||
}
|
||||
else
|
||||
{
|
||||
var newKarma = _redis.HashIncrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
|
||||
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
|
||||
new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString("u"))});
|
||||
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(user.GetAvatarUrl())
|
||||
.WithName(user.Username));
|
||||
|
||||
eb.WithColor(new Color(138, 219, 146));
|
||||
eb.Title = transDict["Increased"];
|
||||
eb.AddInlineField(transDict["By"], Context.User.Username);
|
||||
eb.AddInlineField(transDict["Amount"], "+1");
|
||||
eb.AddInlineField(transDict["Current"], newKarma);
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
[Command("bad", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Karma)]
|
||||
[Summary("Decrease Someones Karma")]
|
||||
public async Task Bad([Summary("@someone")] IUser user)
|
||||
{
|
||||
try
|
||||
{
|
||||
var transDict = _translation.GetDict(Context);
|
||||
var lastKarmaFromRedis = _redis.HashGet($"{Context.Guild.Id}:KarmaTimeout", Context.User.Id.ToString());
|
||||
var lastKarma = ConvertToDateTimeOffset(lastKarmaFromRedis.ToString());
|
||||
if (user.Id == Context.User.Id)
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
|
||||
}
|
||||
else if (TimeoutFinished(lastKarma))
|
||||
{
|
||||
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username,
|
||||
GetTimeLeft(lastKarma)));
|
||||
}
|
||||
else
|
||||
{
|
||||
var newKarma = _redis.HashDecrement($"{Context.Guild.Id}:Karma", user.Id.ToString());
|
||||
_redis.HashSet($"{Context.Guild.Id}:KarmaTimeout",
|
||||
new[] {new HashEntry(Context.User.Id.ToString(), DateTimeOffset.Now.ToString())});
|
||||
|
||||
var eb = new EmbedBuilder();
|
||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
||||
.WithIconUrl(user.GetAvatarUrl())
|
||||
.WithName(user.Username));
|
||||
|
||||
eb.WithColor(new Color(138, 219, 146));
|
||||
eb.Title = transDict["Decreased"];
|
||||
eb.AddInlineField(transDict["By"], Context.User.Username);
|
||||
eb.AddInlineField(transDict["Amount"], "-1");
|
||||
eb.AddInlineField(transDict["Current"], newKarma);
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
private DateTimeOffset ConvertToDateTimeOffset(string dateTimeOffsetString)
|
||||
{
|
||||
return string.IsNullOrEmpty(dateTimeOffsetString) ? DateTimeOffset.Now.Subtract(new TimeSpan(7, 18, 0, 0)) : DateTimeOffset.Parse(dateTimeOffsetString);
|
||||
}
|
||||
|
||||
private bool TimeoutFinished(DateTimeOffset lastKarma)
|
||||
{
|
||||
return lastKarma.AddMinutes(3) > DateTimeOffset.Now;
|
||||
}
|
||||
|
||||
private string GetTimeLeft(DateTimeOffset lastKarma)
|
||||
{
|
||||
var dt = lastKarma.AddMinutes(3).Subtract(DateTimeOffset.Now);
|
||||
return $"{dt.Minutes} Minutes and {dt.Seconds} Seconds";
|
||||
}
|
||||
}
|
||||
}
|
150
Geekbot.net/Commands/User/Rank/Rank.cs
Normal file
150
Geekbot.net/Commands/User/Rank/Rank.cs
Normal file
|
@ -0,0 +1,150 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.Converters;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Logger;
|
||||
using Geekbot.net.Lib.UserRepository;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.User.Rank
|
||||
{
|
||||
public class Rank : ModuleBase
|
||||
{
|
||||
private readonly IEmojiConverter _emojiConverter;
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly IDatabase _redis;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly DiscordSocketClient _client;
|
||||
|
||||
public Rank(IDatabase redis, IErrorHandler errorHandler, IGeekbotLogger logger, IUserRepository userRepository,
|
||||
IEmojiConverter emojiConverter, DiscordSocketClient client)
|
||||
{
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
_logger = logger;
|
||||
_userRepository = userRepository;
|
||||
_emojiConverter = emojiConverter;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
[Command("rank", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Statistics)]
|
||||
[Summary("get user top 10 in messages or karma")]
|
||||
public async Task RankCmd([Summary("type")] string typeUnformated = "messages",
|
||||
[Summary("amount")] int amount = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var type = typeUnformated.ToCharArray().First().ToString().ToUpper() + typeUnformated.Substring(1);
|
||||
|
||||
if (!type.Equals("Messages") && !type.Equals("Karma") && !type.Equals("Rolls"))
|
||||
{
|
||||
await ReplyAsync("Valid types are '`messages`' '`karma`', '`rolls`'");
|
||||
return;
|
||||
}
|
||||
|
||||
var replyBuilder = new StringBuilder();
|
||||
|
||||
if (amount > 20)
|
||||
{
|
||||
replyBuilder.AppendLine(":warning: Limiting to 20");
|
||||
amount = 20;
|
||||
}
|
||||
|
||||
var messageList = _redis.HashGetAll($"{Context.Guild.Id}:{type}");
|
||||
if (messageList.Length == 0)
|
||||
{
|
||||
await ReplyAsync($"No {type.ToLowerInvariant()} found on this server");
|
||||
return;
|
||||
}
|
||||
var sortedList = messageList.OrderByDescending(e => e.Value).ToList();
|
||||
var guildMessages = (int) sortedList.First().Value;
|
||||
var theBot = sortedList.FirstOrDefault(e => e.Name.ToString().Equals(_client.CurrentUser.Id.ToString()));
|
||||
if (!string.IsNullOrEmpty(theBot.Name))
|
||||
{
|
||||
sortedList.Remove(theBot);
|
||||
}
|
||||
if (type == "Messages") sortedList.RemoveAt(0);
|
||||
|
||||
var highscoreUsers = new Dictionary<RankUserPolyfillDto, int>();
|
||||
var listLimiter = 1;
|
||||
var failedToRetrieveUser = false;
|
||||
foreach (var user in sortedList)
|
||||
{
|
||||
if (listLimiter > amount) break;
|
||||
try
|
||||
{
|
||||
var guildUser = _userRepository.Get((ulong) user.Name);
|
||||
if (guildUser.Username != null)
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfillDto
|
||||
{
|
||||
Username = guildUser.Username,
|
||||
Discriminator = guildUser.Discriminator
|
||||
}, (int) user.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
highscoreUsers.Add(new RankUserPolyfillDto
|
||||
{
|
||||
Id = user.Name
|
||||
}, (int) user.Value);
|
||||
failedToRetrieveUser = true;
|
||||
}
|
||||
|
||||
listLimiter++;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning("Geekbot", $"Could not retrieve user {user.Name}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if (failedToRetrieveUser) replyBuilder.AppendLine(":warning: Couldn't get all userdata\n");
|
||||
replyBuilder.AppendLine($":bar_chart: **{type} Highscore for {Context.Guild.Name}**");
|
||||
var highscorePlace = 1;
|
||||
foreach (var user in highscoreUsers)
|
||||
{
|
||||
replyBuilder.Append(highscorePlace < 11
|
||||
? $"{_emojiConverter.NumberToEmoji(highscorePlace)} "
|
||||
: $"`{highscorePlace}.` ");
|
||||
|
||||
replyBuilder.Append(user.Key.Username != null
|
||||
? $"**{user.Key.Username}#{user.Key.Discriminator}**"
|
||||
: $"**{user.Key.Id}**");
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case "Messages":
|
||||
var percent = Math.Round((double) (100 * user.Value) / guildMessages, 2);
|
||||
replyBuilder.Append($" - {percent}% of total - {user.Value} messages");
|
||||
break;
|
||||
case "Karma":
|
||||
replyBuilder.Append($" - {user.Value} Karma");
|
||||
break;
|
||||
case "Rolls":
|
||||
replyBuilder.Append($" - {user.Value} Guessed");
|
||||
break;
|
||||
}
|
||||
|
||||
replyBuilder.Append("\n");
|
||||
|
||||
highscorePlace++;
|
||||
}
|
||||
|
||||
await ReplyAsync(replyBuilder.ToString());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs
Normal file
9
Geekbot.net/Commands/User/Rank/RankUserPolyfillDto.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Geekbot.net.Commands.User.Rank
|
||||
{
|
||||
internal class RankUserPolyfillDto
|
||||
{
|
||||
public string Username { get; set; }
|
||||
public string Discriminator { get; set; }
|
||||
public string Id { get; set; }
|
||||
}
|
||||
}
|
74
Geekbot.net/Commands/User/Stats.cs
Normal file
74
Geekbot.net/Commands/User/Stats.cs
Normal file
|
@ -0,0 +1,74 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib;
|
||||
using Geekbot.net.Lib.ErrorHandling;
|
||||
using Geekbot.net.Lib.Levels;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace Geekbot.net.Commands.User
|
||||
{
|
||||
public class Stats : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
private readonly ILevelCalc _levelCalc;
|
||||
private readonly IDatabase _redis;
|
||||
|
||||
public Stats(IDatabase redis, IErrorHandler errorHandler, ILevelCalc levelCalc)
|
||||
{
|
||||
_redis = redis;
|
||||
_errorHandler = errorHandler;
|
||||
_levelCalc = levelCalc;
|
||||
}
|
||||
|
||||
[Command("stats", RunMode = RunMode.Async)]
|
||||
[Remarks(CommandCategories.Statistics)]
|
||||
[Summary("Get information about this user")]
|
||||
public async Task User([Summary("@someone")] IUser user = null)
|
||||
{
|
||||
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 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.GetLevel(messages);
|
||||
|
||||
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.ToString());
|
||||
var correctRolls = _redis.HashGet($"{Context.Guild.Id}:Rolls", userInfo.Id.ToString());
|
||||
|
||||
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}%");
|
||||
|
||||
if (!correctRolls.IsNullOrEmpty)
|
||||
eb.AddInlineField("Guessed Rolls", correctRolls);
|
||||
|
||||
await ReplyAsync("", false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue