diff --git a/Geekbot.net/Commands/Randomness/Slap.cs b/Geekbot.net/Commands/Randomness/Slap.cs index 20e83c3..3165581 100644 --- a/Geekbot.net/Commands/Randomness/Slap.cs +++ b/Geekbot.net/Commands/Randomness/Slap.cs @@ -6,12 +6,13 @@ using Discord; using Discord.Commands; using Geekbot.net.Database; using Geekbot.net.Database.Models; +using Geekbot.net.Lib; using Geekbot.net.Lib.ErrorHandling; using Geekbot.net.Lib.Extensions; namespace Geekbot.net.Commands.Randomness { - public class Slap : ModuleBase + public class Slap : GeekbotBase { private readonly IErrorHandler _errorHandler; private readonly DatabaseContext _database; @@ -24,7 +25,7 @@ namespace Geekbot.net.Commands.Randomness [Command("slap", RunMode = RunMode.Async)] [Summary("slap someone")] - public async Task Slapper([Summary("@someone")] IUser user) + public async Task Slapper([Summary("@someone")] IGuildUser user) { try { @@ -79,7 +80,7 @@ namespace Geekbot.net.Commands.Randomness "powerless banhammer" }; - await ReplyAsync($"{Context.User.Username} slapped {user.Username} with a {things[new Random().Next(things.Count - 1)]}"); + await ReplyAsync($"{Context.GuildUser.Nickname} slapped {user.Nickname} with a {things[new Random().Next(things.Count - 1)]}"); await UpdateRecieved(user.Id); await UpdateGiven(Context.User.Id); diff --git a/Geekbot.net/Handlers.cs b/Geekbot.net/Handlers.cs index a254c14..79390cc 100644 --- a/Geekbot.net/Handlers.cs +++ b/Geekbot.net/Handlers.cs @@ -9,7 +9,9 @@ using Discord.WebSocket; using Geekbot.net.Database; using Geekbot.net.Database.Models; using Geekbot.net.Lib.AlmostRedis; +using Geekbot.net.Lib.Context; using Geekbot.net.Lib.Extensions; +using Geekbot.net.Lib.Localization; using Geekbot.net.Lib.Logger; using Geekbot.net.Lib.ReactionListener; using Geekbot.net.Lib.UserRepository; @@ -27,11 +29,12 @@ namespace Geekbot.net private readonly CommandService _commands; private readonly IUserRepository _userRepository; private readonly IReactionListener _reactionListener; + private readonly ITranslationHandler _translationHandler; private readonly DatabaseContext _messageCounterDatabaseContext; public Handlers(DatabaseInitializer databaseInitializer, IDiscordClient client, IGeekbotLogger logger, IAlmostRedis redis, IServiceProvider servicesProvider, CommandService commands, IUserRepository userRepository, - IReactionListener reactionListener) + IReactionListener reactionListener, ITranslationHandler translationHandler) { _database = databaseInitializer.Initialize(); _messageCounterDatabaseContext = databaseInitializer.Initialize(); @@ -42,6 +45,7 @@ namespace Geekbot.net _commands = commands; _userRepository = userRepository; _reactionListener = reactionListener; + _translationHandler = translationHandler; } // @@ -79,7 +83,7 @@ namespace Geekbot.net if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return Task.CompletedTask; - var context = new CommandContext(_client, message); + var context = new GeekbotContext(_client, message, _translationHandler); var commandExec = _commands.ExecuteAsync(context, argPos, _servicesProvider); _logger.Information(LogSource.Command, context.Message.Content.Split(" ")[0].Replace("!", ""), diff --git a/Geekbot.net/Lib/Context/GeekbotContext.cs b/Geekbot.net/Lib/Context/GeekbotContext.cs new file mode 100644 index 0000000..dfc4281 --- /dev/null +++ b/Geekbot.net/Lib/Context/GeekbotContext.cs @@ -0,0 +1,56 @@ +using Discord; +using Geekbot.net.Lib.Localization; + +namespace Geekbot.net.Lib.Context +{ + /// The context of a command which may contain the client, user, guild, channel, and message. + public class GeekbotContext : IGeekbotContext + { + /// + public IDiscordClient Client { get; } + + /// + public IGuild Guild { get; } + + /// + public IMessageChannel Channel { get; } + + /// + public IUser User { get; } + + /// + public IUserMessage Message { get; } + + /// + public IGuildUser GuildUser { get; } + + /// + public TranslationGuildContext Translations { get; } + + /// Indicates whether the channel that the command is executed in is a private channel. + public bool IsPrivate + { + get + { + return this.Channel is IPrivateChannel; + } + } + + /// + /// Initializes a new class with the provided client and message. + /// + /// The underlying client. + /// The underlying message. + /// the translation handler + 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; + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Context/IGeekbotContext.cs b/Geekbot.net/Lib/Context/IGeekbotContext.cs new file mode 100644 index 0000000..61884e1 --- /dev/null +++ b/Geekbot.net/Lib/Context/IGeekbotContext.cs @@ -0,0 +1,19 @@ +using Discord; +using Discord.Commands; +using Geekbot.net.Lib.Localization; + +namespace Geekbot.net.Lib.Context +{ + public interface IGeekbotContext : ICommandContext + { + /// + /// Gets the who executed the command. + /// + IGuildUser GuildUser { get; } + + /// + /// Gets the containing the necessary tools for command localization. + /// + TranslationGuildContext Translations { get; } + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/GeekbotBase.cs b/Geekbot.net/Lib/GeekbotBase.cs new file mode 100644 index 0000000..e92f212 --- /dev/null +++ b/Geekbot.net/Lib/GeekbotBase.cs @@ -0,0 +1,9 @@ +using Discord.Commands; +using Geekbot.net.Lib.Context; + +namespace Geekbot.net.Lib +{ + public abstract class GeekbotBase : ModuleBase + { + } +} \ No newline at end of file diff --git a/Geekbot.net/Lib/Localization/ITranslationHandler.cs b/Geekbot.net/Lib/Localization/ITranslationHandler.cs index 9cd0680..3672c26 100644 --- a/Geekbot.net/Lib/Localization/ITranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/ITranslationHandler.cs @@ -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> GetDict(ICommandContext context, string command); Task GetGuildContext(ICommandContext context); + Task GetGuildContext(IGuild guild, IUserMessage message); Task SetLanguage(ulong guildId, string language); List SupportedLanguages { get; } } diff --git a/Geekbot.net/Lib/Localization/TranslationHandler.cs b/Geekbot.net/Lib/Localization/TranslationHandler.cs index 3d1ee3a..f1a30e5 100644 --- a/Geekbot.net/Lib/Localization/TranslationHandler.cs +++ b/Geekbot.net/Lib/Localization/TranslationHandler.cs @@ -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> GetDict(ICommandContext context) + private Task> GetDict(ICommandContext context) + { + return GetDict(context.Guild, context.Message); + } + + private async Task> 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 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> GetDict(ICommandContext context, string command) { try diff --git a/Geekbot.net/Program.cs b/Geekbot.net/Program.cs index b7251a8..9833b3a 100755 --- a/Geekbot.net/Program.cs +++ b/Geekbot.net/Program.cs @@ -170,7 +170,7 @@ namespace Geekbot.net _servicesProvider = _services.BuildServiceProvider(); await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _servicesProvider); - var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener); + var handlers = new Handlers(_databaseInitializer, _client, _logger, _redis, _servicesProvider, _commands, _userRepository, reactionListener, translationHandler); _client.MessageReceived += handlers.RunCommand; _client.MessageDeleted += handlers.MessageDeleted;