Add GeekbotBase as an extension of ModuleBase
This commit is contained in:
parent
ced287e492
commit
8d9c436cfc
8 changed files with 113 additions and 11 deletions
56
Geekbot.net/Lib/Context/GeekbotContext.cs
Normal file
56
Geekbot.net/Lib/Context/GeekbotContext.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
using Discord;
|
||||
using Geekbot.net.Lib.Localization;
|
||||
|
||||
namespace Geekbot.net.Lib.Context
|
||||
{
|
||||
/// <summary> The context of a command which may contain the client, user, guild, channel, and message. </summary>
|
||||
public class GeekbotContext : IGeekbotContext
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public IDiscordClient Client { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGuild Guild { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IMessageChannel Channel { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IUser User { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IUserMessage Message { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public IGuildUser GuildUser { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public TranslationGuildContext Translations { get; }
|
||||
|
||||
/// <summary> Indicates whether the channel that the command is executed in is a private channel. </summary>
|
||||
public bool IsPrivate
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Channel is IPrivateChannel;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new <see cref="T:Discord.Commands.CommandContext" /> class with the provided client and message.
|
||||
/// </summary>
|
||||
/// <param name="client">The underlying client.</param>
|
||||
/// <param name="msg">The underlying message.</param>
|
||||
/// <param name="translationHandler">the translation handler</param>
|
||||
public GeekbotContext(IDiscordClient client, IUserMessage msg, ITranslationHandler translationHandler)
|
||||
{
|
||||
this.Client = client;
|
||||
this.Guild = (msg.Channel as IGuildChannel)?.Guild;
|
||||
this.Channel = msg.Channel;
|
||||
this.User = msg.Author;
|
||||
this.GuildUser = msg.Author as IGuildUser;
|
||||
this.Message = msg;
|
||||
this.Translations = translationHandler.GetGuildContext(this.Guild, this.Message).Result;
|
||||
}
|
||||
}
|
||||
}
|
19
Geekbot.net/Lib/Context/IGeekbotContext.cs
Normal file
19
Geekbot.net/Lib/Context/IGeekbotContext.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Geekbot.net.Lib.Localization;
|
||||
|
||||
namespace Geekbot.net.Lib.Context
|
||||
{
|
||||
public interface IGeekbotContext : ICommandContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="T:Discord:IGuildUser"/> who executed the command.
|
||||
/// </summary>
|
||||
IGuildUser GuildUser { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="T:Geekbot:net:Lib:Localization:TranslationGuildContext"/> containing the necessary tools for command localization.
|
||||
/// </summary>
|
||||
TranslationGuildContext Translations { get; }
|
||||
}
|
||||
}
|
9
Geekbot.net/Lib/GeekbotBase.cs
Normal file
9
Geekbot.net/Lib/GeekbotBase.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
using Discord.Commands;
|
||||
using Geekbot.net.Lib.Context;
|
||||
|
||||
namespace Geekbot.net.Lib
|
||||
{
|
||||
public abstract class GeekbotBase : ModuleBase<GeekbotContext>
|
||||
{
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue