2024-06-16 20:19:31 -04:00
using CommonSocketLibrary.Abstract ;
using CommonSocketLibrary.Common ;
using Microsoft.Extensions.DependencyInjection ;
using Serilog ;
2024-07-16 00:48:55 -04:00
using TwitchChatTTS.Hermes.Socket ;
2024-07-19 12:56:41 -04:00
using TwitchChatTTS.OBS.Socket ;
2024-06-24 18:11:36 -04:00
using TwitchChatTTS.OBS.Socket.Data ;
2024-08-04 19:46:10 -04:00
using TwitchChatTTS.Twitch.Socket.Messages ;
2024-07-19 12:56:41 -04:00
using static TwitchChatTTS . Chat . Commands . TTSCommands ;
2024-06-16 20:19:31 -04:00
namespace TwitchChatTTS.Chat.Commands
{
2024-07-19 12:56:41 -04:00
public class OBSCommand : IChatCommand
2024-06-16 20:19:31 -04:00
{
2024-07-19 12:56:41 -04:00
private readonly OBSSocketClient _obs ;
2024-06-24 18:11:36 -04:00
private readonly ILogger _logger ;
2024-06-16 20:19:31 -04:00
2024-07-19 12:56:41 -04:00
public string Name = > "obs" ;
2024-06-16 20:19:31 -04:00
public OBSCommand (
2024-07-19 12:56:41 -04:00
[FromKeyedServices("obs")] SocketClient < WebSocketMessage > obs ,
2024-06-16 20:19:31 -04:00
ILogger logger
2024-07-19 12:56:41 -04:00
)
2024-06-16 20:19:31 -04:00
{
2024-07-19 12:56:41 -04:00
_obs = ( obs as OBSSocketClient ) ! ;
2024-06-16 20:19:31 -04:00
_logger = logger ;
2024-07-19 12:56:41 -04:00
}
public void Build ( ICommandBuilder builder )
{
builder . CreateCommandTree ( Name , b = >
{
b . CreateStaticInputParameter ( "get_scene_item_id" , b = >
{
b . CreateUnvalidatedParameter ( "sceneName" )
. CreateCommand ( new OBSGetSceneItemId ( _obs , _logger ) ) ;
} )
. CreateStaticInputParameter ( "transform" , b = >
{
b . CreateUnvalidatedParameter ( "sceneName" )
. CreateUnvalidatedParameter ( "sourceName" )
. CreateObsTransformationParameter ( "propertyName" )
. CreateUnvalidatedParameter ( "value" )
. CreateCommand ( new OBSTransform ( _obs , _logger ) ) ;
} )
. CreateStaticInputParameter ( "visibility" , b = >
{
b . CreateUnvalidatedParameter ( "sceneName" )
. CreateUnvalidatedParameter ( "sourceName" )
. CreateStateParameter ( "state" )
. CreateCommand ( new OBSVisibility ( _obs , _logger ) ) ;
} ) ;
} ) ;
}
private sealed class OBSGetSceneItemId : IChatPartialCommand
{
private readonly OBSSocketClient _obs ;
private readonly ILogger _logger ;
2024-06-16 20:19:31 -04:00
2024-07-19 12:56:41 -04:00
public string Name = > "obs" ;
public bool AcceptCustomPermission { get = > true ; }
public OBSGetSceneItemId (
[FromKeyedServices("obs")] SocketClient < WebSocketMessage > obs ,
ILogger logger
)
{
_obs = ( obs as OBSSocketClient ) ! ;
_logger = logger ;
}
2024-08-04 19:46:10 -04:00
public async Task Execute ( IDictionary < string , string > values , ChannelChatMessage message , HermesSocketClient client )
2024-07-19 12:56:41 -04:00
{
string sceneName = values [ "sceneName" ] ;
string sourceName = values [ "sourceName" ] ;
_logger . Debug ( $"Getting scene item id via chat command [scene name: {sceneName}][source name: {sourceName}]" ) ;
await _obs . Send ( new RequestMessage ( "GetSceneItemId" , string . Empty , new Dictionary < string , object > ( ) { { "sceneName" , sceneName } , { "sourceName" , sourceName } } ) ) ;
}
2024-06-16 20:19:31 -04:00
}
2024-07-19 12:56:41 -04:00
private sealed class OBSTransform : IChatPartialCommand
2024-06-16 20:19:31 -04:00
{
2024-07-19 12:56:41 -04:00
private readonly OBSSocketClient _obs ;
private readonly ILogger _logger ;
public string Name = > "obs" ;
public bool AcceptCustomPermission { get = > true ; }
public OBSTransform (
[FromKeyedServices("obs")] SocketClient < WebSocketMessage > obs ,
ILogger logger
)
{
_obs = ( obs as OBSSocketClient ) ! ;
_logger = logger ;
}
2024-08-04 19:46:10 -04:00
public async Task Execute ( IDictionary < string , string > values , ChannelChatMessage message , HermesSocketClient client )
2024-07-19 12:56:41 -04:00
{
string sceneName = values [ "sceneName" ] ;
string sourceName = values [ "sourceName" ] ;
string propertyName = values [ "propertyName" ] ;
string value = values [ "value" ] ;
_logger . Debug ( $"Getting scene item transformation data via chat command [scene name: {sceneName}][source name: {sourceName}][property: {propertyName}][value: {value}]" ) ;
await _obs . UpdateTransformation ( sceneName , sourceName , ( d ) = >
{
if ( propertyName . ToLower ( ) = = "rotation" | | propertyName . ToLower ( ) = = "rotate" )
d . Rotation = int . Parse ( value ) ;
else if ( propertyName . ToLower ( ) = = "x" )
d . PositionX = int . Parse ( value ) ;
else if ( propertyName . ToLower ( ) = = "y" )
d . PositionY = int . Parse ( value ) ;
} ) ;
}
2024-06-16 20:19:31 -04:00
}
2024-07-19 12:56:41 -04:00
private sealed class OBSVisibility : IChatPartialCommand
2024-06-16 20:19:31 -04:00
{
2024-07-19 12:56:41 -04:00
private readonly OBSSocketClient _obs ;
private readonly ILogger _logger ;
public string Name = > "obs" ;
public bool AcceptCustomPermission { get = > true ; }
public OBSVisibility (
[FromKeyedServices("obs")] SocketClient < WebSocketMessage > obs ,
ILogger logger
)
{
_obs = ( obs as OBSSocketClient ) ! ;
_logger = logger ;
}
2024-06-16 20:19:31 -04:00
2024-08-04 19:46:10 -04:00
public async Task Execute ( IDictionary < string , string > values , ChannelChatMessage message , HermesSocketClient client )
2024-06-24 18:11:36 -04:00
{
2024-07-19 12:56:41 -04:00
string sceneName = values [ "sceneName" ] ;
string sourceName = values [ "sourceName" ] ;
string state = values [ "state" ] ;
_logger . Debug ( $"Updating scene item visibility via chat command [scene name: {sceneName}][source name: {sourceName}][state: {state}]" ) ;
string stateLower = state . ToLower ( ) ;
bool stateBool = stateLower = = "true" | | stateLower = = "enable" | | stateLower = = "enabled" | | stateLower = = "yes" ;
await _obs . UpdateSceneItemVisibility ( sceneName , sourceName , stateBool ) ;
2024-06-16 20:19:31 -04:00
}
}
}
}