/// <summary> /// executes a stream command received by a channel /// </summary> /// <param name="channel">channel the command was received from</param> /// <param name="command">received command</param> public void ExecuteCommand(IChatChannel channel, StreamCommand command) { IStreamCommandHandler handler = commandmanager.GetCommandHandlerOrDefault(command.Command); if (handler == null) { channel.SendMessage($"Unknown command '{command.Command}', try '!commands' for a list of commands."); } else { if ((channel.Flags & handler.RequiredFlags) != handler.RequiredFlags) { return; } try { handler.ExecuteCommand(channel, command); } catch (StreamCommandException e) { channel.SendMessage(e.Message); if (e.ProvideHelp) { channel.SendMessage($"Try '!help {command.Command}' to get an idea how to use the command."); } } catch (Exception e) { Logger.Error(this, $"Error processing command '{command}'", e); channel.SendMessage("Error processing command. You should probably inform the streamer that this tool is crap (also he can take a look at the logs what went wrong)."); } } Command?.Invoke(command); }
/// <summary> /// adds a command handler to management /// </summary> /// <param name="command">command under which handler is to be added</param> /// <param name="handler">handler to be added</param> public void AddCommandHandler(string command, IStreamCommandHandler handler) { lock (commandhandlerlock) { if (commandhandlers.ContainsKey(command)) { Logger.Warning(this, $"'{command}' already registered to '{commandhandlers[command].GetType().Name}'. Handler will be replaced by '{handler.GetType().Name}'"); } commandhandlers[command] = handler; } }
public void ExecuteCommand(IChatChannel channel, StreamCommand command) { string helpcommand = "help"; if (command.Arguments.Length > 0) { helpcommand = command.Arguments[0]; } IStreamCommandHandler handler = manager[helpcommand]; if (handler == null) { channel.SendMessage($"@{command.User}: Unknown command '{helpcommand}', try !commands for a list of commands."); } else { handler.ProvideHelp(channel, command.User); } }
/// <summary> /// registers a command handler for a command /// </summary> /// <param name="command">command for which to register handler</param> /// <param name="handler">handler used to process commands</param> public void RegisterCommandHandler(string command, IStreamCommandHandler handler) { commandmanager.AddCommandHandler(command, handler); }