Files
SPT-Server-Build/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommandoCommands.cs
T
Jesse b6692fead4 Chatbot async improvements (#446)
* Add better chatbot handling by making them asynchronous

Removes the need for having RunInTimespan as await Task.Delay now can handle this

* Remove now unused classes

* Handle commando's commands with ValueTask

* Set values as not nullable, client sends all of these
2025-07-04 18:04:37 +01:00

77 lines
2.3 KiB
C#

using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Helpers.Dialog.Commando.SptCommands;
using SPTarkov.Server.Core.Models.Eft.Dialog;
using SPTarkov.Server.Core.Models.Eft.Profile;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Servers;
using SPTarkov.Server.Core.Services;
namespace SPTarkov.Server.Core.Helpers.Dialog.Commando;
[Injectable]
public class SptCommandoCommands : IChatCommand
{
protected readonly ServerLocalisationService _serverLocalisationService;
protected readonly IDictionary<string, ISptCommand> _sptCommands;
public SptCommandoCommands(
ConfigServer configServer,
ServerLocalisationService localisationService,
IEnumerable<ISptCommand> sptCommands
)
{
_sptCommands = sptCommands.ToDictionary(command => command.GetCommand());
_serverLocalisationService = localisationService;
var coreConfigs = configServer.GetConfig<CoreConfig>();
var commandoId = coreConfigs.Features?.ChatbotFeatures.Ids.GetValueOrDefault("commando");
if (
!(
coreConfigs.Features.ChatbotFeatures.CommandoFeatures.GiveCommandEnabled
&& coreConfigs.Features.ChatbotFeatures.EnabledBots.ContainsKey(commandoId)
)
)
{
_sptCommands.Remove("give");
}
}
public string GetCommandPrefix()
{
return "spt";
}
public string GetCommandHelp(string command)
{
return _sptCommands.TryGetValue(command, out var value) ? value.GetCommandHelp() : "";
}
public List<string> GetCommands()
{
return _sptCommands.Keys.ToList();
}
public async ValueTask<string> Handle(
string command,
UserDialogInfo commandHandler,
string sessionId,
SendMessageRequest request
)
{
return await _sptCommands[command].PerformAction(commandHandler, sessionId, request);
}
public void RegisterSptCommandoCommand(ISptCommand command)
{
var key = command.GetCommand();
if (!_sptCommands.TryAdd(key, command))
{
throw new Exception(
_serverLocalisationService.GetText(
"chat-unable_to_register_command_already_registered",
key
)
);
}
}
}