Add GeekbotBase as an extension of ModuleBase

This commit is contained in:
runebaas 2019-05-23 23:23:16 +02:00
parent ced287e492
commit 8d9c436cfc
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
8 changed files with 113 additions and 11 deletions

View file

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
namespace Geekbot.net.Lib.Localization
@ -10,6 +11,7 @@ namespace Geekbot.net.Lib.Localization
string GetString(string language, string command, string stringName);
Task<Dictionary<string, string>> GetDict(ICommandContext context, string command);
Task<TranslationGuildContext> GetGuildContext(ICommandContext context);
Task<TranslationGuildContext> GetGuildContext(IGuild guild, IUserMessage message);
Task<bool> SetLanguage(ulong guildId, string language);
List<string> SupportedLanguages { get; }
}

View file

@ -3,13 +3,12 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Geekbot.net.Database;
using Geekbot.net.Database.Models;
using Geekbot.net.Lib.Extensions;
using Geekbot.net.Lib.Logger;
using Utf8Json;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
namespace Geekbot.net.Lib.Localization
@ -137,12 +136,17 @@ namespace Geekbot.net.Lib.Localization
return translation;
}
private async Task<Dictionary<string, string>> GetDict(ICommandContext context)
private Task<Dictionary<string, string>> GetDict(ICommandContext context)
{
return GetDict(context.Guild, context.Message);
}
private async Task<Dictionary<string, string>> GetDict(IGuild guild, IUserMessage message)
{
try
{
var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower();
var serverLanguage = await GetServerLanguage(context.Guild?.Id ?? 0);
var command = message.Content.Split(' ').First().TrimStart('!').ToLower();
var serverLanguage = await GetServerLanguage(guild?.Id ?? 0);
return _translations[serverLanguage][command];
}
catch (Exception e)
@ -159,6 +163,13 @@ namespace Geekbot.net.Lib.Localization
return new TranslationGuildContext(this, language, dict);
}
public async Task<TranslationGuildContext> GetGuildContext(IGuild guild, IUserMessage message)
{
var dict = await GetDict(guild, message);
var language = await GetServerLanguage(guild?.Id ?? 0);
return new TranslationGuildContext(this, language, dict);
}
public async Task<Dictionary<string, string>> GetDict(ICommandContext context, string command)
{
try