Translate common commands and fix bug in !urban

This commit is contained in:
Runebaas 2017-11-16 17:19:43 +01:00
parent 119ce579b7
commit 14fcf74aea
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
11 changed files with 182 additions and 44 deletions

View file

@ -121,7 +121,7 @@ namespace Geekbot.net.Commands
if (success)
{
var trans = _translation.GetDict(Context);
await ReplyAsync(trans["Confirm"]);
await ReplyAsync(trans["NewLanguageSet"]);
return;
}
await ReplyAsync(

View file

@ -9,11 +9,13 @@ namespace Geekbot.net.Commands
{
private readonly Random _rnd;
private readonly IErrorHandler _errorHandler;
private readonly ITranslationHandler _translation;
public Choose(Random RandomClient, IErrorHandler errorHandler)
public Choose(Random RandomClient, IErrorHandler errorHandler, ITranslationHandler translation)
{
_rnd = RandomClient;
_errorHandler = errorHandler;
_translation = translation;
}
[Command("choose", RunMode = RunMode.Async)]
@ -23,9 +25,10 @@ namespace Geekbot.net.Commands
{
try
{
var transDict = _translation.GetDict(Context);
var choicesArray = choices.Split(';');
var choice = _rnd.Next(choicesArray.Length);
await ReplyAsync($"I choose **{choicesArray[choice]}**");
await ReplyAsync(string.Format(transDict["Choice"], choicesArray[choice]));
}
catch (Exception e)
{

View file

@ -12,11 +12,13 @@ namespace Geekbot.net.Commands
{
private readonly IDatabase _redis;
private readonly IErrorHandler _errorHandler;
private readonly ITranslationHandler _translation;
public Counters(IDatabase redis, IErrorHandler errorHandler)
public Counters(IDatabase redis, IErrorHandler errorHandler, ITranslationHandler translation)
{
_redis = redis;
_errorHandler = errorHandler;
_translation = translation;
}
[Command("good", RunMode = RunMode.Async)]
@ -26,16 +28,16 @@ namespace Geekbot.net.Commands
{
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($"Sorry {Context.User.Username}, but you can't lower your own karma");
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
}
else if (TimeoutFinished(lastKarma))
{
await ReplyAsync(
$"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can give karma again...");
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, GetTimeLeft(lastKarma)));
}
else
{
@ -49,10 +51,10 @@ namespace Geekbot.net.Commands
.WithName(user.Username));
eb.WithColor(new Color(138, 219, 146));
eb.Title = "Karma Increased";
eb.AddInlineField("By", Context.User.Username);
eb.AddInlineField("amount", "+1");
eb.AddInlineField("Current Karma", newKarma);
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());
}
}
@ -69,16 +71,16 @@ namespace Geekbot.net.Commands
{
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($"Sorry {Context.User.Username}, but you can't lower your own karma");
await ReplyAsync(string.Format(transDict["CannotChangeOwn"], Context.User.Username));
}
else if (TimeoutFinished(lastKarma))
{
await ReplyAsync(
$"Sorry {Context.User.Username}, but you have to wait {GetTimeLeft(lastKarma)} before you can take karma again...");
await ReplyAsync(string.Format(transDict["WaitUntill"], Context.User.Username, GetTimeLeft(lastKarma)));
}
else
{
@ -92,10 +94,10 @@ namespace Geekbot.net.Commands
.WithName(user.Username));
eb.WithColor(new Color(138, 219, 146));
eb.Title = "Karma Decreased";
eb.AddInlineField("By", Context.User.Username);
eb.AddInlineField("amount", "-1");
eb.AddInlineField("Current Karma", newKarma);
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());
}
}

View file

@ -8,13 +8,17 @@ namespace Geekbot.net.Commands
{
public class Roll : ModuleBase
{
private readonly IDatabase redis;
private readonly Random rnd;
private readonly IDatabase _redis;
private readonly Random _rnd;
private readonly ITranslationHandler _translation;
private readonly IErrorHandler _errorHandler;
public Roll(IDatabase redis, Random RandomClient)
public Roll(IDatabase redis, Random RandomClient, IErrorHandler errorHandler, ITranslationHandler translation)
{
this.redis = redis;
rnd = RandomClient;
_redis = redis;
_rnd = RandomClient;
_translation = translation;
_errorHandler = errorHandler;
}
[Command("roll", RunMode = RunMode.Async)]
@ -22,21 +26,29 @@ namespace Geekbot.net.Commands
[Summary("Guess which number the bot will roll (1-100")]
public async Task RollCommand([Remainder] [Summary("guess")] string stuff = "noGuess")
{
var number = rnd.Next(1, 100);
try
{
var number = _rnd.Next(1, 100);
var guess = 1000;
int.TryParse(stuff, out guess);
var transDict = _translation.GetDict(Context);
if (guess <= 100 && guess > 0)
{
await ReplyAsync($"{Context.Message.Author.Mention} you rolled {number}, your guess was {guess}");
await ReplyAsync(string.Format(transDict["Rolled"], Context.Message.Author.Mention, number, guess));
if (guess == number)
{
await ReplyAsync($"Congratulations {Context.User.Username}, your guess was correct!");
redis.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
await ReplyAsync(string.Format(transDict["Gratz"], Context.Message.Author));
_redis.HashIncrement($"{Context.Guild.Id}:Rolls", Context.User.Id.ToString());
}
}
else
{
await ReplyAsync(Context.Message.Author.Mention + ", you rolled " + number);
await ReplyAsync(string.Format(transDict["RolledNoGuess"], Context.Message.Author.Mention, number));
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}

View file

@ -39,7 +39,7 @@ namespace Geekbot.net.Commands
await ReplyAsync("That word hasn't been defined...");
return;
}
var definition = definitions.list.First();
var definition = definitions.list.First(e => !string.IsNullOrWhiteSpace(e.example));
var eb = new EmbedBuilder();
eb.WithAuthor(new EmbedAuthorBuilder()

View file

@ -62,7 +62,7 @@ namespace Geekbot.net.Commands
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context, "Something went wrong...");
_errorHandler.HandleCommandException(e, Context);
}
}
@ -111,7 +111,7 @@ namespace Geekbot.net.Commands
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context, "Something went wrong...");
_errorHandler.HandleCommandException(e, Context);
}
}
}

View file

@ -41,4 +41,30 @@
</PackageReference>
<PackageReference Include="Utf8Json" Version="1.3.1" />
</ItemGroup>
<ItemGroup>
<None Update="Storage\checkEmPics">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\croissant">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\fortunes">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\pandas">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\pumpkin">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\squirrel">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\Translations.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Storage\turtles">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -8,16 +8,19 @@ namespace Geekbot.net.Lib
public class ErrorHandler : IErrorHandler
{
private readonly ILogger _logger;
private readonly ITranslationHandler _translation;
public ErrorHandler(ILogger logger)
public ErrorHandler(ILogger logger, ITranslationHandler translation)
{
_logger = logger;
_translation = translation;
}
public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:")
public void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def")
{
try
{
var errorString = errorMessage == "def" ? _translation.GetString(Context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage;
var errorObj = new ErrorObject()
{
Message = new ErrorMessage()
@ -50,7 +53,7 @@ namespace Geekbot.net.Lib
_logger.Error(e, errorJson);
if (!string.IsNullOrEmpty(errorMessage))
{
Context.Channel.SendMessageAsync(errorMessage);
Context.Channel.SendMessageAsync(errorString);
}
}
catch (Exception ex)
@ -87,6 +90,6 @@ namespace Geekbot.net.Lib
public interface IErrorHandler
{
void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "Something went wrong :confused:");
void HandleCommandException(Exception e, ICommandContext Context, string errorMessage = "def");
}
}

View file

@ -118,6 +118,19 @@ namespace Geekbot.net.Lib
}
}
public Dictionary<string, string> GetDict(ICommandContext context, string command)
{
try
{
return _translations[_serverLanguages[context.Guild.Id]][command];
}
catch (Exception e)
{
_logger.Error(e, "lol nope");
return new Dictionary<string, string>();
}
}
public bool SetLanguage(ulong guildId, string language)
{
try
@ -144,6 +157,7 @@ namespace Geekbot.net.Lib
{
string GetString(ulong guildId, string command, string stringName);
Dictionary<string, string> GetDict(ICommandContext context);
Dictionary<string, string> GetDict(ICommandContext context, string command);
bool SetLanguage(ulong guildId, string language);
List<string> GetSupportedLanguages();
}

View file

@ -94,7 +94,6 @@ namespace Geekbot.net
services = new ServiceCollection();
userRepository = new UserRepository(redis, logger);
var errorHandler = new ErrorHandler(logger);
var randomClient = new Random();
var fortunes = new FortunesProvider(randomClient, logger);
var mediaProvider = new MediaProvider(randomClient, logger);
@ -103,7 +102,6 @@ namespace Geekbot.net
var emojiConverter = new EmojiConverter();
var audioUtils = new AudioUtils();
services.AddSingleton<IErrorHandler>(errorHandler);
services.AddSingleton(redis);
services.AddSingleton<ILogger>(logger);
services.AddSingleton<IUserRepository>(userRepository);
@ -136,8 +134,10 @@ namespace Geekbot.net
logger.Information("[Geekbot] Registering Stuff");
var translationHandler = new TranslationHandler(client.Guilds, redis, logger);
var errorHandler = new ErrorHandler(logger, translationHandler);
await commands.AddModulesAsync(Assembly.GetEntryAssembly());
services.AddSingleton(commands);
services.AddSingleton<IErrorHandler>(errorHandler);
services.AddSingleton<ITranslationHandler>(translationHandler);
services.AddSingleton<DiscordSocketClient>(client);
servicesProvider = services.BuildServiceProvider();

View file

@ -1,12 +1,90 @@
{
"admin": {
"NewLanguageSet": {
"EN": "I will in english from now on",
"EN": "I will reply in english from now on",
"CHDE": "I werd ab jetzt uf schwiizerdüütsch antworte, äuuä"
},
"GetLanguage": {
"EN": "I'm talking english",
"CHDE": "I red schwiizerdüütsch"
}
},
"errorHandler": {
"SomethingWentWrong": {
"EN": "Something went wrong :confused:",
"CHDE": "Öppis isch schief gange :confused:"
}
},
"choose": {
"Choice": {
"EN": "I Choose **{0}**",
"CHDE": "I nimme **{0}**"
}
},
"good": {
"CannotChangeOwn": {
"EN": "Sorry {0}, but you can't give yourself karma",
"CHDE": "Sorry {0}, aber du chasch dr selber kei karma geh"
},
"WaitUntill": {
"EN": "Sorry {0}, but you have to wait {1} before you can give karma again...",
"CHDE": "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch geh..."
},
"Increased": {
"EN": "Karma gained",
"CHDE": "Karma becho"
},
"By": {
"EN": "By",
"CHDE": "Vo"
},
"Amount": {
"EN": "Amount",
"CHDE": "Mengi"
},
"Current": {
"EN": "Current",
"CHDE": "Jetzt"
}
},
"bad": {
"CannotChangeOwn": {
"EN": "Sorry {0}, but you can't lower your own karma",
"CHDE": "Sorry {0}, aber du chasch dr din eigete karma nid weg neh"
},
"WaitUntill": {
"EN": "Sorry {0}, but you have to wait {1} before you can lower karma again...",
"CHDE": "Sorry {0}, aber du musch no {1} warte bisch d wieder karma chasch senke..."
},
"Decreased": {
"EN": "Karma lowered",
"CHDE": "Karma gsenkt"
},
"By": {
"EN": "By",
"CHDE": "Vo"
},
"Amount": {
"EN": "Amount",
"CHDE": "Mengi"
},
"Current": {
"EN": "Current",
"CHDE": "Jetzt"
}
},
"roll": {
"Rolled": {
"EN": "{0}, you rolled {1}, your guess was {2}",
"CHDE": "{0}, du hesch {1} grollt und hesch {2} grate"
},
"Gratz": {
"EN": "Congratulations {0}, your guess was correct!",
"CHDE": "Gratuliere {0}, du hesch richtig grate!"
},
"RolledNoGuess": {
"EN": "{0}, you rolled {1}",
"CHDE": "{0}, du hesch {1} grollt"
}
}
}