Set guild language when executing interaction

This commit is contained in:
Daan Boerlage 2021-10-31 22:33:31 +01:00
parent 177c773451
commit 772557978b
Signed by: daan
GPG key ID: FCE070E1E4956606
4 changed files with 32 additions and 10 deletions

View file

@ -227,7 +227,7 @@ namespace Geekbot.Bot
private void StartWebApi() private void StartWebApi()
{ {
var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository); var highscoreManager = new HighscoreManager(_databaseInitializer.Initialize(), _userRepository);
WebApiStartup.StartWebApi(_servicesProvider, _logger, _runParameters, _commands, _databaseInitializer.Initialize(), _globalSettings, highscoreManager); WebApiStartup.StartWebApi(_servicesProvider, _logger, _runParameters, _commands, _databaseInitializer.Initialize(), _globalSettings, highscoreManager, _guildSettingsManager);
} }
private void RegisterSentry() private void RegisterSentry()

View file

@ -8,8 +8,6 @@ namespace Geekbot.Core.Interactions
{ {
public abstract class InteractionBase : IInteractionBase public abstract class InteractionBase : IInteractionBase
{ {
public InteractionBase() {}
public virtual void BeforeExecute() public virtual void BeforeExecute()
{ {
@ -32,7 +30,7 @@ namespace Geekbot.Core.Interactions
Type = InteractionResponseType.ChannelMessageWithSource, Type = InteractionResponseType.ChannelMessageWithSource,
Data = new() Data = new()
{ {
Content = "Something went wrong :confused:" Content = Localization.Internal.SomethingWentWrong
} }
}; };
} }

View file

@ -1,8 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Geekbot.Core.GuildSettingsManager;
using Geekbot.Core.Interactions.ApplicationCommand; using Geekbot.Core.Interactions.ApplicationCommand;
using Geekbot.Core.Interactions.Request; using Geekbot.Core.Interactions.Request;
using Geekbot.Core.Interactions.Response; using Geekbot.Core.Interactions.Response;
@ -14,13 +17,16 @@ namespace Geekbot.Core.Interactions
{ {
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
private readonly IGuildSettingsManager _guildSettingsManager;
private readonly Dictionary<string, Type> _commands = new(); private readonly Dictionary<string, Type> _commands = new();
public Dictionary<string, Command> CommandsInfo { get; init; } public Dictionary<string, Command> CommandsInfo { get; init; }
public InteractionCommandManager(IServiceProvider provider) public InteractionCommandManager(IServiceProvider provider, IGuildSettingsManager guildSettingsManager)
{ {
_provider = provider; _provider = provider;
_guildSettingsManager = guildSettingsManager;
var interactions = Assembly.GetCallingAssembly() var interactions = Assembly.GetCallingAssembly()
.GetTypes() .GetTypes()
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InteractionBase))) .Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(InteractionBase)))
@ -42,10 +48,26 @@ namespace Geekbot.Core.Interactions
var type = _commands[interaction.Data.Name]; var type = _commands[interaction.Data.Name];
var command = ActivatorUtilities.CreateInstance(_provider, type) as InteractionBase; var command = ActivatorUtilities.CreateInstance(_provider, type) as InteractionBase;
if (command == null)
{
return new InteractionResponse()
{
Type = InteractionResponseType.ChannelMessageWithSource,
Data = new InteractionResponseData()
{
Content = "Command not found..."
}
};
}
var guildSettings = _guildSettingsManager.GetSettings(ulong.Parse(interaction.GuildId));
var language = guildSettings.Language;
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
InteractionResponse response; InteractionResponse response;
command.BeforeExecute();
try try
{ {
command.BeforeExecute();
response = await command.Exec(interaction); response = await command.Exec(interaction);
} }
catch (Exception e) catch (Exception e)
@ -57,6 +79,7 @@ namespace Geekbot.Core.Interactions
{ {
command.AfterExecute(); command.AfterExecute();
} }
return response; return response;
} }
} }

View file

@ -6,6 +6,7 @@ using Discord.WebSocket;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.Database; using Geekbot.Core.Database;
using Geekbot.Core.GlobalSettings; using Geekbot.Core.GlobalSettings;
using Geekbot.Core.GuildSettingsManager;
using Geekbot.Core.Highscores; using Geekbot.Core.Highscores;
using Geekbot.Core.Interactions; using Geekbot.Core.Interactions;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
@ -24,7 +25,7 @@ namespace Geekbot.Web
public static void Main() {} public static void Main() {}
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService, public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager) DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
{ {
WebHost.CreateDefaultBuilder() WebHost.CreateDefaultBuilder()
.UseKestrel(options => .UseKestrel(options =>
@ -44,7 +45,7 @@ namespace Geekbot.Web
}); });
services.AddSentry(); services.AddSentry();
var interactionCommandManager = new InteractionCommandManager(commandProvider); var interactionCommandManager = new InteractionCommandManager(commandProvider, guildSettingsManager);
services.AddSingleton(databaseContext); services.AddSingleton(databaseContext);
services.AddSingleton(globalSettings); services.AddSingleton(globalSettings);