Fixed command permissions. Moved to using Twitch's EventSub via websockets. Cleaned some code up. Added detection for subscription messages (no TTS), message deletion, full or partial chat clear. Removes messages from TTS queue if applicable. Added command aliases for static parameters. Word filters use compiled regex if possible. Fixed TTS voice deletion.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
using Serilog;
|
||||
using TwitchChatTTS.Hermes.Socket;
|
||||
using TwitchLib.Client.Models;
|
||||
using TwitchChatTTS.Twitch.Socket.Messages;
|
||||
using static TwitchChatTTS.Chat.Commands.TTSCommands;
|
||||
|
||||
namespace TwitchChatTTS.Chat.Commands
|
||||
@ -8,6 +8,8 @@ namespace TwitchChatTTS.Chat.Commands
|
||||
public class VoiceCommand : IChatCommand
|
||||
{
|
||||
private readonly User _user;
|
||||
// TODO: get permissions
|
||||
// TODO: validated parameter for username by including '@' and regex for username
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public VoiceCommand(User user, ILogger logger)
|
||||
@ -23,7 +25,10 @@ namespace TwitchChatTTS.Chat.Commands
|
||||
builder.CreateCommandTree(Name, b =>
|
||||
{
|
||||
b.CreateVoiceNameParameter("voiceName", true)
|
||||
.CreateCommand(new TTSVoiceSelector(_user, _logger));
|
||||
.CreateCommand(new TTSVoiceSelector(_user, _logger))
|
||||
.CreateUnvalidatedParameter("chatter", optional: true)
|
||||
.AddPermission("tts.command.voice.admin")
|
||||
.CreateCommand(new TTSVoiceSelectorAdmin(_user, _logger));
|
||||
});
|
||||
}
|
||||
|
||||
@ -40,18 +45,12 @@ namespace TwitchChatTTS.Chat.Commands
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public bool CheckDefaultPermissions(ChatMessage message)
|
||||
{
|
||||
return message.IsModerator || message.IsBroadcaster || message.IsSubscriber || message.Bits >= 100;
|
||||
}
|
||||
|
||||
public async Task Execute(IDictionary<string, string> values, ChatMessage message, HermesSocketClient client)
|
||||
public async Task Execute(IDictionary<string, string> values, ChannelChatMessage message, HermesSocketClient client)
|
||||
{
|
||||
if (_user == null || _user.VoicesSelected == null)
|
||||
return;
|
||||
|
||||
long chatterId = long.Parse(message.UserId);
|
||||
long chatterId = long.Parse(message.ChatterUserId);
|
||||
var voiceName = values["voiceName"];
|
||||
var voiceNameLower = voiceName.ToLower();
|
||||
var voice = _user.VoicesAvailable.First(v => v.Value.ToLower() == voiceNameLower);
|
||||
@ -59,12 +58,56 @@ namespace TwitchChatTTS.Chat.Commands
|
||||
if (_user.VoicesSelected.ContainsKey(chatterId))
|
||||
{
|
||||
await client.UpdateTTSUser(chatterId, voice.Key);
|
||||
_logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {message.Username}][reason: command]");
|
||||
_logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]");
|
||||
}
|
||||
else
|
||||
{
|
||||
await client.CreateTTSUser(chatterId, voice.Key);
|
||||
_logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {message.Username}][reason: command]");
|
||||
_logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {message.ChatterUserLogin}][reason: command]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TTSVoiceSelectorAdmin : IChatPartialCommand
|
||||
{
|
||||
private readonly User _user;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public bool AcceptCustomPermission { get => true; }
|
||||
|
||||
public TTSVoiceSelectorAdmin(User user, ILogger logger)
|
||||
{
|
||||
_user = user;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IDictionary<string, string> values, ChannelChatMessage message, HermesSocketClient client)
|
||||
{
|
||||
if (_user == null || _user.VoicesSelected == null)
|
||||
return;
|
||||
|
||||
var chatterLogin = values["chatter"].Substring(1);
|
||||
var mention = message.Message.Fragments.FirstOrDefault(f => f.Mention != null && f.Mention.UserLogin == chatterLogin)?.Mention;
|
||||
if (mention == null)
|
||||
{
|
||||
_logger.Warning("Failed to find the chatter to apply voice command to.");
|
||||
return;
|
||||
}
|
||||
|
||||
long chatterId = long.Parse(mention.UserId);
|
||||
var voiceName = values["voiceName"];
|
||||
var voiceNameLower = voiceName.ToLower();
|
||||
var voice = _user.VoicesAvailable.First(v => v.Value.ToLower() == voiceNameLower);
|
||||
|
||||
if (_user.VoicesSelected.ContainsKey(chatterId))
|
||||
{
|
||||
await client.UpdateTTSUser(chatterId, voice.Key);
|
||||
_logger.Debug($"Sent request to create chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]");
|
||||
}
|
||||
else
|
||||
{
|
||||
await client.CreateTTSUser(chatterId, voice.Key);
|
||||
_logger.Debug($"Sent request to update chat TTS voice [voice: {voice.Value}][username: {mention.UserLogin}][reason: command]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user