TwitchUtils/TTSMe/ExtensionMethods.cs

20 lines
1.3 KiB
C#
Raw Permalink Normal View History

using System.Security.Cryptography;
using System.Text;
using EmojiCSharp;
using TwitchLib.Client.Models;
namespace TTSMe;
public static class ExtensionMethods {
private static byte[] ToBytes(this string str) => Encoding.UTF8.GetBytes(str);
private static string ToHexString(this byte[] bytes) => Convert.ToHexString(bytes);
private static string ToSHA256HashedString(this string str) => SHA256.HashData(str.ToBytes()).ToHexString();
internal static string? GetStoredVoice(this ChatMessage chatMessage) => Voices.Instance().GetVoice(chatMessage.Username);
internal static bool HasStoredVoice(this ChatMessage chatMessage) => Voices.Instance().HasVoice(chatMessage.Username);
internal static string GetMessageText(this ChatMessage chatMessage) => EmojiParser.ParseToAliases(chatMessage.Message).Replace(":", " colon ").Replace("_", " underscore ");
private static string GetMessageHash(this ChatMessage chatMessage) => chatMessage.GetMessageText().ToSHA256HashedString();
internal static string GetAudioFilePath(this ChatMessage chatMessage) => Path.Combine(TTSClient.AudioBasePath, $"{chatMessage.GetStoredVoice()!}-{chatMessage.GetMessageHash()}.mp3");
internal static TextToSpeechRequest ToTTSRequest(this ChatMessage chatMessage) => new(chatMessage.GetMessageText(), chatMessage.GetStoredVoice()!);
}