コード例 #1
0
 public void CustomCommandCollectionCanAddSubCommandDirectly()
 {
     collection.AddSubCommand("test", "sub", "data");
     Assert.That(collection.ContainsCommand("test"), Is.True);
     Assert.That(collection.ContainsSubCommand("test", "sub"), Is.True);
     Assert.That(collection.Commands["test"].Subcommands["sub"], Is.EqualTo("data"));
 }
コード例 #2
0
        public async Task CustomChatCommandCanAddSubCommands()
        {
            var data = new ChatCommandData("command", new[] { "add", "test", "!sub", "test_message" });
            await command.Process(chat, data);

            Assert.That(collection.ContainsSubCommand("test", "sub"), Is.True);
            Assert.That(collection.Commands["test"].Subcommands["sub"], Is.EqualTo("test_message"));
        }
コード例 #3
0
        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);
                }
            }
        }