Fixed directory creation when no directory is mentioned for certain redeemable actions. Undid property name change for Twitch Redemption Id.

This commit is contained in:
Tom 2025-01-14 01:27:25 +00:00
parent 4099322ce2
commit 86590f1c7f
3 changed files with 35 additions and 17 deletions

View File

@ -29,7 +29,7 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
} }
_redemptions.Add(redemption); _redemptions.Add(redemption);
_logger.Information($"A new redemption has been created [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]"); _logger.Information($"A new redemption has been created [redemption id: {redemption.Id}][twitch redemption id: {redemption.RedemptionId}]");
} }
} }
} }

View File

@ -29,9 +29,9 @@ namespace TwitchChatTTS.Hermes.Socket.Requests
} }
if (_redemptions.Update(redemption)) if (_redemptions.Update(redemption))
_logger.Information($"A redemption has been updated [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]"); _logger.Information($"A redemption has been updated [redemption id: {redemption.Id}][twitch redemption id: {redemption.RedemptionId}]");
else else
_logger.Warning($"Failed to update an existing redemption [redemption id: {redemption.Id}][twitch redemption id: {redemption.TwitchRedemptionId}]"); _logger.Warning($"Failed to update an existing redemption [redemption id: {redemption.Id}][twitch redemption id: {redemption.RedemptionId}]");
} }
} }
} }

View File

@ -93,7 +93,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
public void Add(Redemption redemption) public void Add(Redemption redemption)
{ {
_redemptions.Add(redemption.Id, redemption); _redemptions.Add(redemption.Id, redemption);
Add(redemption.TwitchRedemptionId, redemption); Add(redemption.RedemptionId, redemption);
} }
private void Add(string twitchRedemptionId, string redemptionId) private void Add(string twitchRedemptionId, string redemptionId)
@ -171,15 +171,33 @@ namespace TwitchChatTTS.Twitch.Redemptions
switch (action.Type) switch (action.Type)
{ {
case "WRITE_TO_FILE": case "WRITE_TO_FILE":
Directory.CreateDirectory(Path.GetDirectoryName(action.Data["file_path"])!); {
await File.WriteAllTextAsync(action.Data["file_path"], ReplaceContentText(action.Data["file_content"], senderDisplayName)); string path = action.Data["file_path"];
if (string.IsNullOrWhiteSpace(path))
return;
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(directory))
Directory.CreateDirectory(directory);
await File.WriteAllTextAsync(path, ReplaceContentText(action.Data["file_content"], senderDisplayName));
_logger.Debug($"Overwritten text to file [file: {action.Data["file_path"]}][chatter: {senderDisplayName}][chatter id: {senderId}]"); _logger.Debug($"Overwritten text to file [file: {action.Data["file_path"]}][chatter: {senderDisplayName}][chatter id: {senderId}]");
break; break;
}
case "APPEND_TO_FILE": case "APPEND_TO_FILE":
Directory.CreateDirectory(Path.GetDirectoryName(action.Data["file_path"])!); {
await File.AppendAllTextAsync(action.Data["file_path"], ReplaceContentText(action.Data["file_content"], senderDisplayName)); string path = action.Data["file_path"];
if (string.IsNullOrWhiteSpace(path))
return;
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrWhiteSpace(directory))
Directory.CreateDirectory(directory);
await File.AppendAllTextAsync(path, ReplaceContentText(action.Data["file_content"], senderDisplayName));
_logger.Debug($"Appended text to file [file: {action.Data["file_path"]}][chatter: {senderDisplayName}][chatter id: {senderId}]"); _logger.Debug($"Appended text to file [file: {action.Data["file_path"]}][chatter: {senderDisplayName}][chatter id: {senderId}]");
break; break;
}
case "OBS_TRANSFORM": case "OBS_TRANSFORM":
var type = typeof(OBSTransformationData); var type = typeof(OBSTransformationData);
await _obs.UpdateTransformation(action.Data["scene_name"], action.Data["scene_item_name"], (d) => await _obs.UpdateTransformation(action.Data["scene_name"], action.Data["scene_item_name"], (d) =>
@ -392,7 +410,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
if (_actions.ContainsKey(redemption.ActionName)) if (_actions.ContainsKey(redemption.ActionName))
{ {
_logger.Debug($"Fetched a redeemable action [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}][order: {redemption.Order}]"); _logger.Debug($"Fetched a redeemable action [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}][order: {redemption.Order}]");
Add(redemption.TwitchRedemptionId, redemption.Id); Add(redemption.RedemptionId, redemption.Id);
} }
else else
_logger.Warning($"Could not find redeemable action [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}][order: {redemption.Order}]"); _logger.Warning($"Could not find redeemable action [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}][order: {redemption.Order}]");
@ -422,11 +440,11 @@ namespace TwitchChatTTS.Twitch.Redemptions
} }
_redemptions.Remove(redemptionId); _redemptions.Remove(redemptionId);
if (_redeems.TryGetValue(redemption.TwitchRedemptionId, out var redeem)) if (_redeems.TryGetValue(redemption.RedemptionId, out var redeem))
{ {
redeem.Remove(redemptionId); redeem.Remove(redemptionId);
if (!redeem.Any()) if (!redeem.Any())
_redeems.Remove(redemption.TwitchRedemptionId); _redeems.Remove(redemption.RedemptionId);
return true; return true;
} }
} }
@ -446,7 +464,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
{ {
if (_redemptions.TryGetValue(redemption.Id, out var r)) if (_redemptions.TryGetValue(redemption.Id, out var r))
{ {
if (r.Order != redemption.Order && _redeems.TryGetValue(redemption.TwitchRedemptionId, out var redeems) && redeems.Count > 1) if (r.Order != redemption.Order && _redeems.TryGetValue(redemption.RedemptionId, out var redeems) && redeems.Count > 1)
{ {
var redemptions = redeems.Select(r => _redemptions.TryGetValue(r, out var rr) ? rr : null).ToArray(); var redemptions = redeems.Select(r => _redemptions.TryGetValue(r, out var rr) ? rr : null).ToArray();
int index = redeems.IndexOf(redemption.Id), i; int index = redeems.IndexOf(redemption.Id), i;
@ -476,7 +494,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
{ {
r.ActionName = redemption.ActionName; r.ActionName = redemption.ActionName;
r.State = redemption.State; r.State = redemption.State;
r.TwitchRedemptionId = redemption.TwitchRedemptionId; r.RedemptionId = redemption.RedemptionId;
r.Order = redemption.Order; r.Order = redemption.Order;
} }
_logger.Debug($"Updated redemption in redemption manager [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}]"); _logger.Debug($"Updated redemption in redemption manager [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}]");