public void CustomCommandCollectionCanAddCommand() { collection.AddCommand("test", "data"); Assert.That(collection.ContainsCommand("test"), Is.True); Assert.That(collection.Commands.ContainsKey("test")); Assert.That(collection.Commands["test"].Text, Is.EqualTo("data")); }
public async Task CustomChatCommandCanAddCommands() { var data = new ChatCommandData("command", new[] { "add", "test", "test_message" }); await command.Process(chat, data); Assert.That(chat.PreviousMessage, Contains.Substring("Added")); Assert.That(collection.ContainsCommand("test"), Is.True); Assert.That(collection.Commands["test"].Text, Is.EqualTo("test_message")); }
void HandleCommand(TwitchChatMessage message) { string text = message.Text.Trim(); if (string.IsNullOrEmpty(text)) { return; } if (!text.StartsWith("!")) { return; } // Get command name. string command = ""; int position = 1; int nextSpace = text.IndexOf(' ', position); if (nextSpace < 0) { command = text.Substring(position); } else { command = text.Substring(position, nextSpace - position); } if (string.IsNullOrEmpty(command)) { return; } command = command.ToLowerInvariant(); // Get command arguments. List <string> arguments = new List <string>(); if (nextSpace >= 0) { string argumentsText = text.Substring(nextSpace).Trim(); if (!string.IsNullOrEmpty(argumentsText)) { arguments = argumentsText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); } } ChatCommandData commandData = new ChatCommandData(command, arguments); IChatCommand chatCommand; if (chatCommands.TryGetValue(command, out chatCommand)) { // Restrict moderator commands. if (chatCommand.IsModeratorCommand && !message.User.IsModerator) { return; } // Found the chat command, all valid command have a cooldown. if (!HandleCommandCooldown(message)) { return; } // Do invidual chat command handling. var chatWriter = new TwitchChatWriter(client.Connection); chatCommand.Process(chatWriter, commandData); } else if (commandCollection.ContainsCommand(command)) { // Found the chat command, all valid command have a cooldown. if (!HandleCommandCooldown(message)) { return; } // Fall back to custom commands if no hard-coded commands are found. if (arguments.Count > 0 && commandCollection.ContainsSubCommand(command, arguments[0])) { client.Connection.Send(commandCollection.Commands[command].Subcommands[arguments[0]]); } else { client.Connection.Send(commandCollection.Commands[command].Text); } } }