2024-03-15 08:27:35 -04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-15 08:27:35 -04:00
|
|
|
using TwitchLib.Client.Models;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Chat.Commands
|
|
|
|
{
|
|
|
|
public class ChatCommandManager
|
|
|
|
{
|
|
|
|
private IDictionary<string, ChatCommand> _commands;
|
2024-06-16 20:19:31 -04:00
|
|
|
private TwitchBotAuth _token;
|
2024-03-15 08:27:35 -04:00
|
|
|
private IServiceProvider _serviceProvider;
|
2024-06-16 20:19:31 -04:00
|
|
|
private ILogger _logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
private string CommandStartSign { get; } = "!";
|
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public ChatCommandManager(TwitchBotAuth token, IServiceProvider serviceProvider, ILogger logger)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
_token = token;
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
_commands = new Dictionary<string, ChatCommand>();
|
|
|
|
GenerateCommands();
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
private void Add(ChatCommand command)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
_commands.Add(command.Name.ToLower(), command);
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
private void GenerateCommands()
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
var basetype = typeof(ChatCommand);
|
|
|
|
var assembly = GetType().Assembly;
|
|
|
|
var types = assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract && basetype.IsAssignableFrom(t) && t.AssemblyQualifiedName?.Contains(".Chat.") == true);
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
foreach (var type in types)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
var key = "command-" + type.Name.Replace("Commands", "Comm#ands")
|
|
|
|
.Replace("Command", "")
|
|
|
|
.Replace("Comm#ands", "Commands")
|
|
|
|
.ToLower();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var command = _serviceProvider.GetKeyedService<ChatCommand>(key);
|
2024-06-16 20:19:31 -04:00
|
|
|
if (command == null)
|
|
|
|
{
|
|
|
|
_logger.Error("Failed to add command: " + type.AssemblyQualifiedName);
|
2024-03-15 08:27:35 -04:00
|
|
|
continue;
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
_logger.Debug($"Added command {type.AssemblyQualifiedName}.");
|
2024-03-15 08:27:35 -04:00
|
|
|
Add(command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public async Task<ChatCommandResult> Execute(string arg, ChatMessage message)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
if (_token.BroadcasterId == null)
|
|
|
|
return ChatCommandResult.Unknown;
|
|
|
|
if (string.IsNullOrWhiteSpace(arg))
|
|
|
|
return ChatCommandResult.Unknown;
|
|
|
|
|
|
|
|
arg = arg.Trim();
|
|
|
|
|
|
|
|
if (!arg.StartsWith(CommandStartSign))
|
|
|
|
return ChatCommandResult.Unknown;
|
|
|
|
|
|
|
|
string[] parts = arg.Split(" ");
|
|
|
|
string com = parts.First().Substring(CommandStartSign.Length).ToLower();
|
|
|
|
string[] args = parts.Skip(1).ToArray();
|
|
|
|
long broadcasterId = long.Parse(_token.BroadcasterId);
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!_commands.TryGetValue(com, out ChatCommand? command) || command == null)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Failed to find command named '{com}'.");
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Missing;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!await command.CheckPermissions(message, broadcasterId) && message.UserId != "126224566" && !message.IsStaff)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Chatter is missing permission to execute command named '{com}'.");
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Permission;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (command.Parameters.Count(p => !p.Optional) > args.Length)
|
|
|
|
{
|
|
|
|
_logger.Warning($"Command syntax issue when executing command named '{com}' with the following args: {string.Join(" ", args)}");
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Syntax;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
for (int i = 0; i < Math.Min(args.Length, command.Parameters.Count); i++)
|
|
|
|
{
|
|
|
|
if (!command.Parameters[i].Validate(args[i]))
|
|
|
|
{
|
|
|
|
_logger.Warning($"Commmand '{com}' failed because of the #{i + 1} argument. Invalid value: {args[i]}");
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Syntax;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
try
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
await command.Execute(args, message, broadcasterId);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.Error(e, $"Command '{arg}' failed.");
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Fail;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Executed the {com} command with the following args: " + string.Join(" ", args));
|
2024-03-15 08:27:35 -04:00
|
|
|
return ChatCommandResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|