Start Using resource files (.resx) for translations. Create GeekbotCommandBase to reduce command boilerplate. Convert admin, choose, cookies, karma, quote, rank, roll and ship to the new localization method.

This commit is contained in:
runebaas 2020-08-14 03:30:54 +02:00
parent 12388fd7d0
commit 90af781c7b
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
40 changed files with 1935 additions and 327 deletions

View file

@ -1,8 +1,11 @@
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.Core;
using Geekbot.Core.CommandPreconditions;
using Geekbot.Core.Database;
using Geekbot.Core.Database.Models;
@ -13,17 +16,15 @@ using Geekbot.Core.Localization;
namespace Geekbot.Bot.Commands.User
{
[DisableInDirectMessage]
public class Karma : ModuleBase
public class Karma : GeekbotCommandBase
{
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
private readonly ITranslationHandler _translation;
private readonly ITranslationHandler _translations;
public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation)
public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translations) : base(errorHandler, translations)
{
_database = database;
_errorHandler = errorHandler;
_translation = translation;
_translations = translations;
}
[Command("good", RunMode = RunMode.Async)]
@ -32,16 +33,17 @@ namespace Geekbot.Bot.Commands.User
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var transContext = await _translations.GetGuildContext(Context);
var actor = await GetUser(Context.User.Id);
if (user.Id == Context.User.Id)
{
await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username));
await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnUp, Context.User.Username));
}
else if (TimeoutFinished(actor.TimeOut))
{
var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3));
await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime));
await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime));
}
else
{
@ -60,16 +62,16 @@ namespace Geekbot.Bot.Commands.User
.WithName(user.Username));
eb.WithColor(new Color(138, 219, 146));
eb.Title = transContext.GetString("Increased");
eb.AddInlineField(transContext.GetString("By"), Context.User.Username);
eb.AddInlineField(transContext.GetString("Amount"), "+1");
eb.AddInlineField(transContext.GetString("Current"), target.Karma);
eb.Title = Localization.Karma.Increased;
eb.AddInlineField(Localization.Karma.By, Context.User.Username);
eb.AddInlineField(Localization.Karma.Amount, "+1");
eb.AddInlineField(Localization.Karma.Current, target.Karma);
await ReplyAsync("", false, eb.Build());
}
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
await ErrorHandler.HandleCommandException(e, Context);
}
}
@ -79,16 +81,17 @@ namespace Geekbot.Bot.Commands.User
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var transContext = await _translations.GetGuildContext(Context);
var actor = await GetUser(Context.User.Id);
if (user.Id == Context.User.Id)
{
await ReplyAsync(transContext.GetString("CannotChangeOwn", Context.User.Username));
await ReplyAsync(string.Format(Localization.Karma.CannotChangeOwnDown, Context.User.Username));
}
else if (TimeoutFinished(actor.TimeOut))
{
var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3));
await ReplyAsync(transContext.GetString("WaitUntill", Context.User.Username, formatedWaitTime));
await ReplyAsync(string.Format(Localization.Karma.WaitUntill, Context.User.Username, formatedWaitTime));
}
else
{
@ -107,16 +110,16 @@ namespace Geekbot.Bot.Commands.User
.WithName(user.Username));
eb.WithColor(new Color(138, 219, 146));
eb.Title = transContext.GetString("Decreased");
eb.AddInlineField(transContext.GetString("By"), Context.User.Username);
eb.AddInlineField(transContext.GetString("Amount"), "-1");
eb.AddInlineField(transContext.GetString("Current"), target.Karma);
eb.Title = Localization.Karma.Decreased;
eb.AddInlineField(Localization.Karma.By, Context.User.Username);
eb.AddInlineField(Localization.Karma.Amount, "-1");
eb.AddInlineField(Localization.Karma.Current, target.Karma);
await ReplyAsync("", false, eb.Build());
}
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
await ErrorHandler.HandleCommandException(e, Context);
}
}

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.Core;
using Geekbot.Core.CommandPreconditions;
using Geekbot.Core.Converters;
using Geekbot.Core.Database;
@ -11,28 +12,20 @@ using Geekbot.Core.ErrorHandling;
using Geekbot.Core.Extensions;
using Geekbot.Core.Highscores;
using Geekbot.Core.Localization;
using Geekbot.Core.UserRepository;
namespace Geekbot.Bot.Commands.User.Ranking
{
public class Rank : ModuleBase
public class Rank : GeekbotCommandBase
{
private readonly IEmojiConverter _emojiConverter;
private readonly IHighscoreManager _highscoreManager;
private readonly ITranslationHandler _translationHandler;
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
private readonly IUserRepository _userRepository;
public Rank(DatabaseContext database, IErrorHandler errorHandler, IUserRepository userRepository,
IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translationHandler)
public Rank(DatabaseContext database, IErrorHandler errorHandler, IEmojiConverter emojiConverter, IHighscoreManager highscoreManager, ITranslationHandler translations): base(errorHandler, translations)
{
_database = database;
_errorHandler = errorHandler;
_userRepository = userRepository;
_emojiConverter = emojiConverter;
_highscoreManager = highscoreManager;
_translationHandler = translationHandler;
}
[Command("rank", RunMode = RunMode.Async)]
@ -42,7 +35,6 @@ namespace Geekbot.Bot.Commands.User.Ranking
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
HighscoreTypes type;
try
{
@ -51,14 +43,14 @@ namespace Geekbot.Bot.Commands.User.Ranking
}
catch
{
await ReplyAsync(transContext.GetString("InvalidType"));
await ReplyAsync(Localization.Rank.InvalidType);
return;
}
var replyBuilder = new StringBuilder();
if (amount > 20)
{
await ReplyAsync(transContext.GetString("LimitingTo20Warning"));
await ReplyAsync(Localization.Rank.LimitingTo20Warning);
amount = 20;
}
@ -70,7 +62,7 @@ namespace Geekbot.Bot.Commands.User.Ranking
}
catch (HighscoreListEmptyException)
{
await ReplyAsync(transContext.GetString("NoTypeFoundForServer", type));
await ReplyAsync(string.Format(Localization.Rank.NoTypeFoundForServer, type));
return;
}
@ -85,22 +77,24 @@ namespace Geekbot.Bot.Commands.User.Ranking
var failedToRetrieveUser = highscoreUsers.Any(e => string.IsNullOrEmpty(e.Key.Username));
if (failedToRetrieveUser) replyBuilder.AppendLine(transContext.GetString("FailedToResolveAllUsernames"));
replyBuilder.AppendLine(transContext.GetString("HighscoresFor", type.ToString().CapitalizeFirst(), Context.Guild.Name));
if (failedToRetrieveUser) replyBuilder.AppendLine(Localization.Rank.FailedToResolveAllUsernames).AppendLine();
replyBuilder.AppendLine(string.Format(Localization.Rank.HighscoresFor, type.ToString().CapitalizeFirst(), Context.Guild.Name));
var highscorePlace = 1;
foreach (var user in highscoreUsers)
foreach (var (user, value) 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}**");
replyBuilder.Append(user.Username != null
? $"**{user.Username}#{user.Discriminator}**"
: $"**{user.Id}**");
replyBuilder.Append(type == HighscoreTypes.messages
? $" - {user.Value} {type} - {Math.Round((double) (100 * user.Value) / guildMessages, 2)}%\n"
: $" - {user.Value} {type}\n");
? $" - {value} {type} - {Math.Round((double) (100 * value) / guildMessages, 2)}%\n"
: $" - {value} {type}\n");
highscorePlace++;
}
@ -109,7 +103,7 @@ namespace Geekbot.Bot.Commands.User.Ranking
}
catch (Exception e)
{
await _errorHandler.HandleCommandException(e, Context);
await ErrorHandler.HandleCommandException(e, Context);
}
}
}