Take advantage of the translations in the context object

This commit is contained in:
runebaas 2019-05-23 23:43:48 +02:00
parent 295a1d575d
commit 7fb88bd106
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
7 changed files with 62 additions and 98 deletions

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -11,7 +11,6 @@ using Geekbot.net.Lib;
using Geekbot.net.Lib.CommandPreconditions;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Localization;
using Geekbot.net.Lib.ReactionListener;
namespace Geekbot.net.Commands.Admin
@ -23,14 +22,12 @@ namespace Geekbot.net.Commands.Admin
private readonly DatabaseContext _database;
private readonly IErrorHandler _errorHandler;
private readonly IReactionListener _reactionListener;
private readonly ITranslationHandler _translationHandler;
public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener, ITranslationHandler translationHandler)
public Role(DatabaseContext database, IErrorHandler errorHandler, IReactionListener reactionListener)
{
_database = database;
_errorHandler = errorHandler;
_reactionListener = reactionListener;
_translationHandler = translationHandler;
}
[Command(RunMode = RunMode.Async)]
@ -39,17 +36,16 @@ namespace Geekbot.net.Commands.Admin
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
var roles = _database.RoleSelfService.Where(g => g.GuildId.Equals(Context.Guild.Id.AsLong())).ToList();
if (roles.Count == 0)
{
await ReplyAsync(transContext.GetString("NoRolesConfigured"));
await ReplyAsync(Context.Translations.GetString("NoRolesConfigured"));
return;
}
var sb = new StringBuilder();
sb.AppendLine(transContext.GetString("ListHeader", Context.Guild.Name));
sb.AppendLine(transContext.GetString("ListInstruction"));
sb.AppendLine(Context.Translations.GetString("ListHeader", Context.Guild.Name));
sb.AppendLine(Context.Translations.GetString("ListInstruction"));
foreach (var role in roles) sb.AppendLine($"- {role.WhiteListName}");
await ReplyAsync(sb.ToString());
}
@ -65,7 +61,6 @@ namespace Geekbot.net.Commands.Admin
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
var roleName = roleNameRaw.ToLower();
var roleFromDb = _database.RoleSelfService.FirstOrDefault(e =>
e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName));
@ -75,23 +70,23 @@ namespace Geekbot.net.Commands.Admin
var role = Context.Guild.Roles.First(r => r.Id == roleFromDb.RoleId.AsUlong());
if (role == null)
{
await ReplyAsync(transContext.GetString("RoleNotFound"));
await ReplyAsync(Context.Translations.GetString("RoleNotFound"));
return;
}
if (guildUser.RoleIds.Contains(role.Id))
{
await guildUser.RemoveRoleAsync(role);
await ReplyAsync(transContext.GetString("RemovedUserFromRole", role.Name));
await ReplyAsync(Context.Translations.GetString("RemovedUserFromRole", role.Name));
return;
}
await guildUser.AddRoleAsync(role);
await ReplyAsync(transContext.GetString("AddedUserFromRole", role.Name));
await ReplyAsync(Context.Translations.GetString("AddedUserFromRole", role.Name));
return;
}
await ReplyAsync(transContext.GetString("RoleNotFound"));
await ReplyAsync(Context.Translations.GetString("RoleNotFound"));
}
catch (HttpException e)
{
@ -110,10 +105,9 @@ namespace Geekbot.net.Commands.Admin
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
if (role.IsManaged)
{
await ReplyAsync(transContext.GetString("CannotAddManagedRole"));
await ReplyAsync(Context.Translations.GetString("CannotAddManagedRole"));
return;
}
@ -123,7 +117,7 @@ namespace Geekbot.net.Commands.Admin
|| role.Permissions.BanMembers
|| role.Permissions.KickMembers)
{
await ReplyAsync(transContext.GetString("CannotAddDangerousRole"));
await ReplyAsync(Context.Translations.GetString("CannotAddDangerousRole"));
return;
}
@ -134,7 +128,7 @@ namespace Geekbot.net.Commands.Admin
WhiteListName = roleName
});
await _database.SaveChangesAsync();
await ReplyAsync(transContext.GetString("CannotAddDangerousRole", role.Name));
await ReplyAsync(Context.Translations.GetString("CannotAddDangerousRole", role.Name));
}
catch (Exception e)
{
@ -149,18 +143,17 @@ namespace Geekbot.net.Commands.Admin
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
var roleFromDb = _database.RoleSelfService.FirstOrDefault(e =>
e.GuildId.Equals(Context.Guild.Id.AsLong()) && e.WhiteListName.Equals(roleName));
if (roleFromDb != null)
{
_database.RoleSelfService.Remove(roleFromDb);
await _database.SaveChangesAsync();
await ReplyAsync(transContext.GetString("RemovedRoleFromWhitelist", roleName));
await ReplyAsync(Context.Translations.GetString("RemovedRoleFromWhitelist", roleName));
return;
}
await ReplyAsync(transContext.GetString("RoleNotFound"));
await ReplyAsync(Context.Translations.GetString("RoleNotFound"));
}
catch (Exception e)
{

View file

@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
@ -18,14 +18,12 @@ namespace Geekbot.net.Commands.Games
{
private readonly IErrorHandler _errorHandler;
private readonly IAlmostRedis _redis;
private readonly ITranslationHandler _translation;
private readonly DatabaseContext _database;
private readonly IRandomNumberGenerator _randomNumberGenerator;
public Roll(IAlmostRedis redis, IErrorHandler errorHandler, ITranslationHandler translation, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
public Roll(IAlmostRedis redis, IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
{
_redis = redis;
_translation = translation;
_database = database;
_randomNumberGenerator = randomNumberGenerator;
_errorHandler = errorHandler;
@ -40,7 +38,6 @@ namespace Geekbot.net.Commands.Games
var number = _randomNumberGenerator.Next(1, 100);
var guess = 1000;
int.TryParse(stuff, out guess);
var transContext = await _translation.GetGuildContext(Context);
if (guess <= 100 && guess > 0)
{
var prevRoll = _redis.Db.HashGet($"{Context.Guild.Id}:RollsPrevious2", Context.Message.Author.Id).ToString()?.Split('|');
@ -48,17 +45,17 @@ namespace Geekbot.net.Commands.Games
{
if (prevRoll[0] == guess.ToString() && DateTime.Parse(prevRoll[1]) > DateTime.Now.AddDays(-1))
{
await ReplyAsync(transContext.GetString("NoPrevGuess", Context.Message.Author.Mention));
await ReplyAsync(Context.Translations.GetString("NoPrevGuess", Context.Message.Author.Mention));
return;
}
}
_redis.Db.HashSet($"{Context.Guild.Id}:RollsPrevious2", new[] {new HashEntry(Context.Message.Author.Id, $"{guess}|{DateTime.Now}")});
await ReplyAsync(transContext.GetString("Rolled", Context.Message.Author.Mention, number, guess));
await ReplyAsync(Context.Translations.GetString("Rolled", Context.Message.Author.Mention, number, guess));
if (guess == number)
{
await ReplyAsync(transContext.GetString("Gratz", Context.Message.Author));
await ReplyAsync(Context.Translations.GetString("Gratz", Context.Message.Author));
_redis.Db.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
var user = await GetUser(Context.User.Id);
user.Rolls += 1;
@ -68,7 +65,7 @@ namespace Geekbot.net.Commands.Games
}
else
{
await ReplyAsync(transContext.GetString("RolledNoGuess", Context.Message.Author.Mention, number));
await ReplyAsync(Context.Translations.GetString("RolledNoGuess", Context.Message.Author.Mention, number));
}
}
catch (Exception e)

View file

@ -20,14 +20,12 @@ namespace Geekbot.net.Commands.Rpg
{
private readonly DatabaseContext _database;
private readonly IErrorHandler _errorHandler;
private readonly ITranslationHandler _translation;
private readonly IRandomNumberGenerator _randomNumberGenerator;
public Cookies(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation , IRandomNumberGenerator randomNumberGenerator)
public Cookies(DatabaseContext database, IErrorHandler errorHandler, IRandomNumberGenerator randomNumberGenerator)
{
_database = database;
_errorHandler = errorHandler;
_translation = translation;
_randomNumberGenerator = randomNumberGenerator;
}
@ -37,18 +35,17 @@ namespace Geekbot.net.Commands.Rpg
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var actor = await GetUser(Context.User.Id);
if (actor.LastPayout.Value.AddHours(24) > DateTimeOffset.Now)
{
var formatedWaitTime = transContext.FormatDateTimeAsRemaining(actor.LastPayout.Value.AddHours(24));
await ReplyAsync(transContext.GetString("WaitForMoreCookies", formatedWaitTime));
var formatedWaitTime = Context.Translations.FormatDateTimeAsRemaining(actor.LastPayout.Value.AddHours(24));
await ReplyAsync(Context.Translations.GetString("WaitForMoreCookies", formatedWaitTime));
return;
}
actor.Cookies += 10;
actor.LastPayout = DateTimeOffset.Now;
await SetUser(actor);
await ReplyAsync(transContext.GetString("GetCookies", 10, actor.Cookies));
await ReplyAsync(Context.Translations.GetString("GetCookies", 10, actor.Cookies));
}
catch (Exception e)
@ -63,9 +60,8 @@ namespace Geekbot.net.Commands.Rpg
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var actor = await GetUser(Context.User.Id);
await ReplyAsync(transContext.GetString("InYourJar", actor.Cookies));
await ReplyAsync(Context.Translations.GetString("InYourJar", actor.Cookies));
}
catch (Exception e)
{
@ -79,12 +75,11 @@ namespace Geekbot.net.Commands.Rpg
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var giver = await GetUser(Context.User.Id);
if (giver.Cookies < amount)
{
await ReplyAsync(transContext.GetString("NotEnoughToGive"));
await ReplyAsync(Context.Translations.GetString("NotEnoughToGive"));
return;
}
@ -96,7 +91,7 @@ namespace Geekbot.net.Commands.Rpg
await SetUser(giver);
await SetUser(taker);
await ReplyAsync(transContext.GetString("Given", amount, user.Username));
await ReplyAsync(Context.Translations.GetString("Given", amount, user.Username));
}
catch (Exception e)
{
@ -110,12 +105,11 @@ namespace Geekbot.net.Commands.Rpg
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var actor = await GetUser(Context.User.Id);
if (actor.Cookies < 5)
{
await ReplyAsync(transContext.GetString("NotEnoughCookiesToEat"));
await ReplyAsync(Context.Translations.GetString("NotEnoughCookiesToEat"));
return;
}
@ -124,7 +118,7 @@ namespace Geekbot.net.Commands.Rpg
await SetUser(actor);
await ReplyAsync(transContext.GetString("AteCookies", amount, actor.Cookies));
await ReplyAsync(Context.Translations.GetString("AteCookies", amount, actor.Cookies));
}
catch (Exception e)
{

View file

@ -9,7 +9,6 @@ using Geekbot.net.Lib;
using Geekbot.net.Lib.CommandPreconditions;
using Geekbot.net.Lib.ErrorHandling;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Localization;
namespace Geekbot.net.Commands.User
{
@ -18,13 +17,11 @@ namespace Geekbot.net.Commands.User
{
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
private readonly ITranslationHandler _translation;
public Karma(DatabaseContext database, IErrorHandler errorHandler, ITranslationHandler translation)
public Karma(DatabaseContext database, IErrorHandler errorHandler)
{
_database = database;
_errorHandler = errorHandler;
_translation = translation;
}
[Command("good", RunMode = RunMode.Async)]
@ -33,16 +30,15 @@ namespace Geekbot.net.Commands.User
{
try
{
var transContext = await _translation.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(Context.Translations.GetString("CannotChangeOwn", 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));
var formatedWaitTime = Context.Translations.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3));
await ReplyAsync(Context.Translations.GetString("WaitUntill", Context.User.Username, formatedWaitTime));
}
else
{
@ -61,10 +57,10 @@ namespace Geekbot.net.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 = Context.Translations.GetString("Increased");
eb.AddInlineField(Context.Translations.GetString("By"), Context.User.Username);
eb.AddInlineField(Context.Translations.GetString("Amount"), "+1");
eb.AddInlineField(Context.Translations.GetString("Current"), target.Karma);
await ReplyAsync("", false, eb.Build());
}
}
@ -80,16 +76,15 @@ namespace Geekbot.net.Commands.User
{
try
{
var transContext = await _translation.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(Context.Translations.GetString("CannotChangeOwn", 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));
var formatedWaitTime = Context.Translations.FormatDateTimeAsRemaining(actor.TimeOut.AddMinutes(3));
await ReplyAsync(Context.Translations.GetString("WaitUntill", Context.User.Username, formatedWaitTime));
}
else
{
@ -108,10 +103,10 @@ namespace Geekbot.net.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 = Context.Translations.GetString("Decreased");
eb.AddInlineField(Context.Translations.GetString("By"), Context.User.Username);
eb.AddInlineField(Context.Translations.GetString("Amount"), "-1");
eb.AddInlineField(Context.Translations.GetString("Current"), target.Karma);
await ReplyAsync("", false, eb.Build());
}
}

View file

@ -20,20 +20,15 @@ namespace Geekbot.net.Commands.User.Ranking
{
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)
{
_database = database;
_errorHandler = errorHandler;
_userRepository = userRepository;
_emojiConverter = emojiConverter;
_highscoreManager = highscoreManager;
_translationHandler = translationHandler;
}
[Command("rank", RunMode = RunMode.Async)]
@ -43,7 +38,6 @@ namespace Geekbot.net.Commands.User.Ranking
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
HighscoreTypes type;
try
{
@ -52,14 +46,14 @@ namespace Geekbot.net.Commands.User.Ranking
}
catch
{
await ReplyAsync(transContext.GetString("InvalidType"));
await ReplyAsync(Context.Translations.GetString("InvalidType"));
return;
}
var replyBuilder = new StringBuilder();
if (amount > 20)
{
await ReplyAsync(transContext.GetString("LimitingTo20Warning"));
await ReplyAsync(Context.Translations.GetString("LimitingTo20Warning"));
amount = 20;
}
@ -71,7 +65,7 @@ namespace Geekbot.net.Commands.User.Ranking
}
catch (HighscoreListEmptyException)
{
await ReplyAsync(transContext.GetString("NoTypeFoundForServer", type));
await ReplyAsync(Context.Translations.GetString("NoTypeFoundForServer", type));
return;
}
@ -86,8 +80,8 @@ namespace Geekbot.net.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(Context.Translations.GetString("FailedToResolveAllUsernames"));
replyBuilder.AppendLine(Context.Translations.GetString("HighscoresFor", type.ToString().CapitalizeFirst(), Context.Guild.Name));
var highscorePlace = 1;
foreach (var user in highscoreUsers)
{

View file

@ -10,12 +10,10 @@ namespace Geekbot.net.Commands.Utils
public class Choose : GeekbotBase
{
private readonly IErrorHandler _errorHandler;
private readonly ITranslationHandler _translation;
public Choose(IErrorHandler errorHandler, ITranslationHandler translation)
public Choose(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
_translation = translation;
}
[Command("choose", RunMode = RunMode.Async)]
@ -25,10 +23,9 @@ namespace Geekbot.net.Commands.Utils
{
try
{
var transContext = await _translation.GetGuildContext(Context);
var choicesArray = choices.Split(';');
var choice = new Random().Next(choicesArray.Length);
await ReplyAsync(transContext.GetString("Choice", choicesArray[choice].Trim()));
await ReplyAsync(Context.Translations.GetString("Choice", choicesArray[choice].Trim()));
}
catch (Exception e)
{

View file

@ -22,14 +22,12 @@ namespace Geekbot.net.Commands.Utils.Quote
private readonly IErrorHandler _errorHandler;
private readonly DatabaseContext _database;
private readonly IRandomNumberGenerator _randomNumberGenerator;
private readonly ITranslationHandler _translationHandler;
public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator, ITranslationHandler translationHandler)
public Quote(IErrorHandler errorHandler, DatabaseContext database, IRandomNumberGenerator randomNumberGenerator)
{
_errorHandler = errorHandler;
_database = database;
_randomNumberGenerator = randomNumberGenerator;
_translationHandler = translationHandler;
}
[Command]
@ -42,8 +40,7 @@ namespace Geekbot.net.Commands.Utils.Quote
if (!s.Any())
{
var transContext = await _translationHandler.GetGuildContext(Context);
await ReplyAsync(transContext.GetString("NoQuotesFound"));
await ReplyAsync(Context.Translations.GetString("NoQuotesFound"));
return;
}
@ -65,16 +62,15 @@ namespace Geekbot.net.Commands.Utils.Quote
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
if (user.Id == Context.Message.Author.Id)
{
await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes"));
await ReplyAsync(Context.Translations.GetString("CannotSaveOwnQuotes"));
return;
}
if (user.IsBot)
{
await ReplyAsync(transContext.GetString("CannotQuoteBots"));
await ReplyAsync(Context.Translations.GetString("CannotQuoteBots"));
return;
}
@ -86,7 +82,7 @@ namespace Geekbot.net.Commands.Utils.Quote
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build());
await ReplyAsync(Context.Translations.GetString("QuoteAdded"), false, embed.Build());
}
catch (Exception e)
{
@ -101,17 +97,16 @@ namespace Geekbot.net.Commands.Utils.Quote
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
var message = await Context.Channel.GetMessageAsync(messageId);
if (message.Author.Id == Context.Message.Author.Id)
{
await ReplyAsync(transContext.GetString("CannotSaveOwnQuotes"));
await ReplyAsync(Context.Translations.GetString("CannotSaveOwnQuotes"));
return;
}
if (message.Author.IsBot)
{
await ReplyAsync(transContext.GetString("CannotQuoteBots"));
await ReplyAsync(Context.Translations.GetString("CannotQuoteBots"));
return;
}
@ -120,7 +115,7 @@ namespace Geekbot.net.Commands.Utils.Quote
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
await ReplyAsync(transContext.GetString("QuoteAdded"), false, embed.Build());
await ReplyAsync(Context.Translations.GetString("QuoteAdded"), false, embed.Build());
}
catch (Exception e)
{
@ -175,18 +170,17 @@ namespace Geekbot.net.Commands.Utils.Quote
{
try
{
var transContext = await _translationHandler.GetGuildContext(Context);
var quote = _database.Quotes.Where(e => e.GuildId == Context.Guild.Id.AsLong() && e.InternalId == id)?.FirstOrDefault();
if (quote != null)
{
_database.Quotes.Remove(quote);
await _database.SaveChangesAsync();
var embed = QuoteBuilder(quote);
await ReplyAsync(transContext.GetString("Removed", id), false, embed.Build());
await ReplyAsync(Context.Translations.GetString("Removed", id), false, embed.Build());
}
else
{
await ReplyAsync(transContext.GetString("NotFoundWithId"));
await ReplyAsync(Context.Translations.GetString("NotFoundWithId"));
}
}
catch (Exception e)